4141from .application_role_connection import ApplicationRoleConnectionMetadata
4242from .backoff import ExponentialBackoff
4343from .channel import PartialMessageable , _threaded_channel_factory
44- from .emoji import Emoji
44+ from .emoji import AppEmoji , GuildEmoji
4545from .enums import ChannelType , Status
4646from .errors import *
4747from .flags import ApplicationFlags , Intents
@@ -199,6 +199,16 @@ class Client:
199199 To enable these events, this must be set to ``True``. Defaults to ``False``.
200200
201201 .. 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
202212
203213 Attributes
204214 -----------
@@ -330,10 +340,30 @@ def guilds(self) -> list[Guild]:
330340 return self ._connection .guilds
331341
332342 @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+ """
335350 return self ._connection .emojis
336351
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+
337367 @property
338368 def stickers (self ) -> list [GuildSticker ]:
339369 """The stickers that the connected client has.
@@ -684,6 +714,7 @@ async def close(self) -> None:
684714 if self ._closed :
685715 return
686716
717+ await self .http .close ()
687718 self ._closed = True
688719
689720 for voice in self .voice_clients :
@@ -696,7 +727,6 @@ async def close(self) -> None:
696727 if self .ws is not None and self .ws .open :
697728 await self .ws .close (code = 1000 )
698729
699- await self .http .close ()
700730 self ._ready .clear ()
701731
702732 def clear (self ) -> None :
@@ -994,7 +1024,7 @@ def get_user(self, id: int, /) -> User | None:
9941024 """
9951025 return self ._connection .get_user (id )
9961026
997- def get_emoji (self , id : int , / ) -> Emoji | None :
1027+ def get_emoji (self , id : int , / ) -> GuildEmoji | AppEmoji | None :
9981028 """Returns an emoji with the given ID.
9991029
10001030 Parameters
@@ -1004,7 +1034,7 @@ def get_emoji(self, id: int, /) -> Emoji | None:
10041034
10051035 Returns
10061036 -------
1007- Optional[:class:`.Emoji `]
1037+ Optional[:class:`.GuildEmoji` | :class:`.AppEmoji `]
10081038 The custom emoji or ``None`` if not found.
10091039 """
10101040 return self ._connection .get_emoji (id )
@@ -2130,3 +2160,112 @@ def store_url(self) -> str:
21302160 .. versionadded:: 2.6
21312161 """
21322162 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