41
41
from .application_role_connection import ApplicationRoleConnectionMetadata
42
42
from .backoff import ExponentialBackoff
43
43
from .channel import PartialMessageable , _threaded_channel_factory
44
- from .emoji import Emoji
44
+ from .emoji import AppEmoji , GuildEmoji
45
45
from .enums import ChannelType , Status
46
46
from .errors import *
47
47
from .flags import ApplicationFlags , Intents
@@ -199,6 +199,16 @@ class Client:
199
199
To enable these events, this must be set to ``True``. Defaults to ``False``.
200
200
201
201
.. versionadded:: 2.0
202
+ cache_app_emojis: :class:`bool`
203
+ Whether to automatically fetch and cache the application's emojis on startup and when fetching. Defaults to ``False``.
204
+
205
+ .. warning::
206
+
207
+ There are no events related to application emojis - if any are created/deleted on the
208
+ Developer Dashboard while the client is running, the cache will not be updated until you manually
209
+ run :func:`fetch_emojis`.
210
+
211
+ .. versionadded:: 2.7
202
212
203
213
Attributes
204
214
-----------
@@ -330,10 +340,30 @@ def guilds(self) -> list[Guild]:
330
340
return self ._connection .guilds
331
341
332
342
@property
333
- def emojis (self ) -> list [Emoji ]:
334
- """The emojis that the connected client has."""
343
+ def emojis (self ) -> list [GuildEmoji | AppEmoji ]:
344
+ """The emojis that the connected client has.
345
+
346
+ .. note::
347
+
348
+ This only includes the application's emojis if `cache_app_emojis` is ``True``.
349
+ """
335
350
return self ._connection .emojis
336
351
352
+ @property
353
+ def guild_emojis (self ) -> list [GuildEmoji ]:
354
+ """The :class:`~discord.GuildEmoji` that the connected client has."""
355
+ return [e for e in self .emojis if isinstance (e , GuildEmoji )]
356
+
357
+ @property
358
+ def app_emojis (self ) -> list [AppEmoji ]:
359
+ """The :class:`~discord.AppEmoji` that the connected client has.
360
+
361
+ .. note::
362
+
363
+ This is only available if `cache_app_emojis` is ``True``.
364
+ """
365
+ return [e for e in self .emojis if isinstance (e , AppEmoji )]
366
+
337
367
@property
338
368
def stickers (self ) -> list [GuildSticker ]:
339
369
"""The stickers that the connected client has.
@@ -684,6 +714,7 @@ async def close(self) -> None:
684
714
if self ._closed :
685
715
return
686
716
717
+ await self .http .close ()
687
718
self ._closed = True
688
719
689
720
for voice in self .voice_clients :
@@ -696,7 +727,6 @@ async def close(self) -> None:
696
727
if self .ws is not None and self .ws .open :
697
728
await self .ws .close (code = 1000 )
698
729
699
- await self .http .close ()
700
730
self ._ready .clear ()
701
731
702
732
def clear (self ) -> None :
@@ -994,7 +1024,7 @@ def get_user(self, id: int, /) -> User | None:
994
1024
"""
995
1025
return self ._connection .get_user (id )
996
1026
997
- def get_emoji (self , id : int , / ) -> Emoji | None :
1027
+ def get_emoji (self , id : int , / ) -> GuildEmoji | AppEmoji | None :
998
1028
"""Returns an emoji with the given ID.
999
1029
1000
1030
Parameters
@@ -1004,7 +1034,7 @@ def get_emoji(self, id: int, /) -> Emoji | None:
1004
1034
1005
1035
Returns
1006
1036
-------
1007
- Optional[:class:`.Emoji `]
1037
+ Optional[:class:`.GuildEmoji` | :class:`.AppEmoji `]
1008
1038
The custom emoji or ``None`` if not found.
1009
1039
"""
1010
1040
return self ._connection .get_emoji (id )
@@ -2130,3 +2160,112 @@ def store_url(self) -> str:
2130
2160
.. versionadded:: 2.6
2131
2161
"""
2132
2162
return f"https://discord.com/application-directory/{ self .application_id } /store"
2163
+
2164
+ async def fetch_emojis (self ) -> list [AppEmoji ]:
2165
+ r"""|coro|
2166
+
2167
+ Retrieves all custom :class:`AppEmoji`\s from the application.
2168
+
2169
+ Raises
2170
+ ---------
2171
+ HTTPException
2172
+ An error occurred fetching the emojis.
2173
+
2174
+ Returns
2175
+ --------
2176
+ List[:class:`AppEmoji`]
2177
+ The retrieved emojis.
2178
+ """
2179
+ data = await self ._connection .http .get_all_application_emojis (
2180
+ self .application_id
2181
+ )
2182
+ return [
2183
+ self ._connection .maybe_store_app_emoji (self .application_id , d )
2184
+ for d in data ["items" ]
2185
+ ]
2186
+
2187
+ async def fetch_emoji (self , emoji_id : int , / ) -> AppEmoji :
2188
+ """|coro|
2189
+
2190
+ Retrieves a custom :class:`AppEmoji` from the application.
2191
+
2192
+ Parameters
2193
+ ----------
2194
+ emoji_id: :class:`int`
2195
+ The emoji's ID.
2196
+
2197
+ Returns
2198
+ -------
2199
+ :class:`AppEmoji`
2200
+ The retrieved emoji.
2201
+
2202
+ Raises
2203
+ ------
2204
+ NotFound
2205
+ The emoji requested could not be found.
2206
+ HTTPException
2207
+ An error occurred fetching the emoji.
2208
+ """
2209
+ data = await self ._connection .http .get_application_emoji (
2210
+ self .application_id , emoji_id
2211
+ )
2212
+ return self ._connection .maybe_store_app_emoji (self .application_id , data )
2213
+
2214
+ async def create_emoji (
2215
+ self ,
2216
+ * ,
2217
+ name : str ,
2218
+ image : bytes ,
2219
+ ) -> AppEmoji :
2220
+ r"""|coro|
2221
+
2222
+ Creates a custom :class:`AppEmoji` for the application.
2223
+
2224
+ There is currently a limit of 2000 emojis per application.
2225
+
2226
+ Parameters
2227
+ -----------
2228
+ name: :class:`str`
2229
+ The emoji name. Must be at least 2 characters.
2230
+ image: :class:`bytes`
2231
+ The :term:`py:bytes-like object` representing the image data to use.
2232
+ Only JPG, PNG and GIF images are supported.
2233
+
2234
+ Raises
2235
+ -------
2236
+ HTTPException
2237
+ An error occurred creating an emoji.
2238
+
2239
+ Returns
2240
+ --------
2241
+ :class:`AppEmoji`
2242
+ The created emoji.
2243
+ """
2244
+
2245
+ img = utils ._bytes_to_base64_data (image )
2246
+ data = await self ._connection .http .create_application_emoji (
2247
+ self .application_id , name , img
2248
+ )
2249
+ return self ._connection .maybe_store_app_emoji (self .application_id , data )
2250
+
2251
+ async def delete_emoji (self , emoji : Snowflake ) -> None :
2252
+ """|coro|
2253
+
2254
+ Deletes the custom :class:`AppEmoji` from the application.
2255
+
2256
+ Parameters
2257
+ ----------
2258
+ emoji: :class:`abc.Snowflake`
2259
+ The emoji you are deleting.
2260
+
2261
+ Raises
2262
+ ------
2263
+ HTTPException
2264
+ An error occurred deleting the emoji.
2265
+ """
2266
+
2267
+ await self ._connection .http .delete_application_emoji (
2268
+ self .application_id , emoji .id
2269
+ )
2270
+ if self ._connection .cache_app_emojis and self ._connection .get_emoji (emoji .id ):
2271
+ self ._connection .remove_emoji (emoji )
0 commit comments