Skip to content

Asset

Asset

Represents a CDN asset on Discord.

Parameters:

Name Type Description Default
url str

The url of the asset.

required
hash str

The hash of the asset.

required
animated bool

Wheather this asset is animated.

required

Operations

  • str(x): Returns the CDN URL of the asset.
  • len(x): Checks the length of the asset's URL.
  • repr(x): Returns a string representation of the asset.
  • x == y: Checks if two Asset instances are equal.
  • x != y: Checks if two Asset instances are not equal.
Source code in dismake/models/asset.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class Asset:
    """Represents a CDN asset on Discord.

    Parameters
    ----------
    url: str
        The url of the asset.
    hash: str
        The hash of the asset.
    animated: bool
        Wheather this asset is animated.

    Operations
    ----------
    - ``str(x)``:
        Returns the CDN URL of the asset.
    - ``len(x)``:
        Checks the length of the asset's URL.
    - ``repr(x)``:
        Returns a string representation of the asset.
    - ``x == y``:
        Checks if two Asset instances are equal.
    - ``x != y``:
        Checks if two Asset instances are not equal.

    """

    def __init__(self, url: str, hash: str, animated: bool) -> None:
        self._url = url
        self._hash = hash
        self._animated = animated

    @classmethod
    def from_default_avatar(cls, index: int) -> Self:
        return cls(
            url=f"{BASE_URL}/embed/avatars/{index}.png",
            hash=str(index),
            animated=False,
        )

    @classmethod
    def from_avatar(cls, user_id: int, avatar: str) -> Self:
        animated = avatar.startswith("a_")
        format = "gif" if animated else "png"
        return cls(
            url=f"{BASE_URL}/avatars/{user_id}/{avatar}.{format}?size=1024",
            hash=avatar,
            animated=animated,
        )

    @classmethod
    def from_guild_avatar(cls, guild_id: int, member_id: int, avatar: str) -> Self:
        animated = avatar.startswith("a_")
        format = "gif" if animated else "png"
        return cls(
            url=f"{BASE_URL}/guilds/{guild_id}/users/{member_id}/avatars/{avatar}.{format}?size=1024",
            hash=avatar,
            animated=animated,
        )

    @classmethod
    def from_icon(cls, object_id: int, icon_hash: str, path: str) -> Self:
        return cls(
            url=f"{BASE_URL}/{path}-icons/{object_id}/{icon_hash}.png?size=1024",
            hash=icon_hash,
            animated=False,
        )

    @classmethod
    def from_app_icon(
        cls, object_id: int, icon_hash: str, asset_type: Literal["icon", "cover_image"]
    ) -> Self:
        return cls(
            url=f"{BASE_URL}/app-icons/{object_id}/{asset_type}.png?size=1024",
            hash=icon_hash,
            animated=False,
        )

    @classmethod
    def from_cover_image(cls, object_id: int, cover_image_hash: str) -> Self:
        return cls(
            url=f"{BASE_URL}/app-assets/{object_id}/store/{cover_image_hash}.png?size=1024",
            hash=cover_image_hash,
            animated=False,
        )

    @classmethod
    def from_scheduled_event_cover_image(
        cls, scheduled_event_id: int, cover_image_hash: str
    ) -> Self:
        return cls(
            url=f"{BASE_URL}/guild-events/{scheduled_event_id}/{cover_image_hash}.png?size=1024",
            hash=cover_image_hash,
            animated=False,
        )

    @classmethod
    def from_guild_image(cls, guild_id: int, image: str, path: str) -> Self:
        animated = image.startswith("a_")
        format = "gif" if animated else "png"
        return cls(
            url=f"{BASE_URL}/{path}/{guild_id}/{image}.{format}?size=1024",
            hash=image,
            animated=animated,
        )

    @classmethod
    def from_guild_icon(cls, guild_id: int, icon_hash: str) -> Self:
        animated = icon_hash.startswith("a_")
        format = "gif" if animated else "png"
        return cls(
            url=f"{BASE_URL}/icons/{guild_id}/{icon_hash}.{format}?size=1024",
            hash=icon_hash,
            animated=animated,
        )

    @classmethod
    def from_sticker_banner(cls, banner: int) -> Self:
        return cls(
            url=f"{BASE_URL}/app-assets/710982414301790216/store/{banner}.png",
            hash=str(banner),
            animated=False,
        )

    @classmethod
    def from_user_banner(cls, user_id: int, banner_hash: str) -> Self:
        animated = banner_hash.startswith("a_")
        format = "gif" if animated else "png"
        return cls(
            url=f"{BASE_URL}/banners/{user_id}/{banner_hash}.{format}?size=512",
            hash=banner_hash,
            animated=animated,
        )

    def __str__(self) -> str:
        return self._url

    def __len__(self) -> int:
        return len(self._url)

    def __repr__(self) -> str:
        shorten = self._url.replace(BASE_URL, "")
        return f"<Asset url={shorten!r}>"

    def __eq__(self, other: object) -> bool:
        return isinstance(other, Asset) and self._url == other._url

    def __hash__(self) -> int:
        return hash(self._url)

    @property
    def url(self) -> str:
        """Returns the underlying URL of the asset."""
        return self._url

    @property
    def hash(self) -> str:
        """Returns the identifying hash of the asset."""
        return self._hash

    @property
    def is_animated(self) -> bool:
        """Returns whether the asset is animated."""
        return self._animated

hash property

hash: str

Returns the identifying hash of the asset.

is_animated property

is_animated: bool

Returns whether the asset is animated.

url property

url: str

Returns the underlying URL of the asset.