Skip to content

Commit 3f2fb34

Browse files
Paillat-devSoheab
andauthored
feat: ♻️ Better nameplates assets api (#2890)
♻️ Better nameplates assets api Co-authored-by: Soheab <[email protected]>
1 parent 5ef8a52 commit 3f2fb34

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

discord/asset.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,18 @@ def _from_cover_image(cls, state, object_id: int, cover_image_hash: str) -> Asse
274274
animated=False,
275275
)
276276

277+
@classmethod
278+
def _from_collectible(
279+
cls, state: ConnectionState, asset: str, animated: bool = False
280+
) -> Asset:
281+
name = "static.png" if not animated else "asset.webm"
282+
return cls(
283+
state,
284+
url=f"{cls.BASE}/assets/collectibles/{asset}{name}",
285+
key=asset,
286+
animated=animated,
287+
)
288+
277289
@classmethod
278290
def _from_guild_image(cls, state, guild_id: int, image: str, path: str) -> Asset:
279291
animated = False

discord/collectibles.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25+
from functools import cached_property
2526
from typing import TYPE_CHECKING
2627

2728
if TYPE_CHECKING:
@@ -55,21 +56,15 @@ def __init__(self, data: NameplatePayload, state: "ConnectionState") -> None:
5556
def __repr__(self) -> str:
5657
return f"<Nameplate sku_id={self.sku_id} palette={self.palette}>"
5758

58-
def get_asset(self, animated: bool = False) -> Asset:
59-
"""Returns the asset of the nameplate.
60-
61-
Parameters
62-
----------
63-
animated: :class:`bool`
64-
Whether to return the animated version of the asset, in webm version. Defaults to ``False``.
65-
"""
66-
fn = "static.png" if not animated else "asset.webm"
67-
return Asset(
68-
state=self._state,
69-
url=f"{Asset.BASE}/assets/collectibles/{self._asset}{fn}",
70-
key=self._asset.split("/")[-1],
71-
animated=animated,
72-
)
59+
@cached_property
60+
def static_asset(self) -> Asset:
61+
"""The static :class:`Asset` of this nameplate."""
62+
return Asset._from_collectible(self._state, self._asset)
63+
64+
@cached_property
65+
def animated_asset(self) -> Asset:
66+
"""The animated :class:`Asset` of this nameplate."""
67+
return Asset._from_collectible(self._state, self._asset, animated=True)
7368

7469

7570
__all__ = ("Nameplate",)

0 commit comments

Comments
 (0)