Skip to content

Commit 89cd452

Browse files
ToothyDevpre-commit-ci[bot]Paillat-dev
authored
feat: ✨Add BaseEmoji.extension and fix animated emoji downloads (#3055)
* ✨🐛 Add extension property and fix animated emoji url * Update changelog accordingly * style(pre-commit): auto fixes from pre-commit.com hooks * 📝 Add versionadded * Apply change requests Co-authored-by: Paillat-dev <me@paillat.dev> * style(pre-commit): auto fixes from pre-commit.com hooks * Apply change requests 2 Co-authored-by: Paillat-dev <me@paillat.dev> * style(pre-commit): auto fixes from pre-commit.com hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat-dev <me@paillat.dev>
1 parent 02c82da commit 89cd452

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ These changes are available on the `master` branch, but have not yet been releas
1212

1313
### Added
1414

15+
- Added `.extension` attribute to emojis to get their file extension.
16+
([#3055](https://github.com/Pycord-Development/pycord/pull/3055))
17+
1518
### Changed
1619

1720
- Updated `Role.is_assignable()` to also check whether the bot has the `MANAGE_ROLES`
@@ -21,6 +24,9 @@ These changes are available on the `master` branch, but have not yet been releas
2124

2225
- Fixed `RawMessageUpdateEvent.cached_message` being always `None` even when the message
2326
was cached. ([#3038](https://github.com/Pycord-Development/pycord/pull/3038))
27+
- Fixed downloading animated emojis which were originally uploaded as WebP files by
28+
changing the `.url` extension of animated emojis from .gif to .webp.
29+
([#3055](https://github.com/Pycord-Development/pycord/pull/3055))
2430

2531
### Deprecated
2632

discord/emoji.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import TYPE_CHECKING, Any, Iterator
28+
from typing import TYPE_CHECKING, Any, Iterator, Literal
2929

3030
from .asset import Asset, AssetMixin
3131
from .partial_emoji import PartialEmoji, _EmojiTag
@@ -106,8 +106,10 @@ def created_at(self) -> datetime:
106106
@property
107107
def url(self) -> str:
108108
"""Returns the URL of the emoji."""
109-
fmt = "gif" if self.animated else "png"
110-
return f"{Asset.BASE}/emojis/{self.id}.{fmt}"
109+
url = f"{Asset.BASE}/emojis/{self.id}.{self.extension}"
110+
if self.animated:
111+
url += "?animated=true"
112+
return url
111113

112114
@property
113115
def mention(self) -> str:
@@ -116,6 +118,14 @@ def mention(self) -> str:
116118
return f"<a:{self.name}:{self.id}>"
117119
return f"<:{self.name}:{self.id}>"
118120

121+
@property
122+
def extension(self) -> Literal["webp", "png"]:
123+
"""Return the file extension of the emoji.
124+
125+
.. versionadded:: 2.7.1
126+
"""
127+
return "webp" if self.animated else "png"
128+
119129

120130
class GuildEmoji(BaseEmoji):
121131
"""Represents a custom emoji in a guild.

discord/partial_emoji.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from __future__ import annotations
2727

2828
import re
29-
from typing import TYPE_CHECKING, Any, TypedDict, TypeVar
29+
from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar
3030

3131
from . import utils
3232
from .asset import Asset, AssetMixin
@@ -245,8 +245,18 @@ def url(self) -> str:
245245
if self.is_unicode_emoji():
246246
return ""
247247

248-
fmt = "gif" if self.animated else "png"
249-
return f"{Asset.BASE}/emojis/{self.id}.{fmt}"
248+
url = f"{Asset.BASE}/emojis/{self.id}.{self.extension}"
249+
if self.animated:
250+
url += "?animated=true"
251+
return url
252+
253+
@property
254+
def extension(self) -> Literal["webp", "png"]:
255+
"""Return the file extension of the emoji.
256+
257+
.. versionadded:: 2.7.1
258+
"""
259+
return "webp" if self.animated else "png"
250260

251261
async def read(self) -> bytes:
252262
if self.is_unicode_emoji():

0 commit comments

Comments
 (0)