Diff
checker
Text
Text
Images
Documents
Excel
Folders
Legal
Enterprise
Desktop
Pricing
Sign in
Download Diffchecker Desktop
Compare text
Find the difference between two text files
Tools
History
Real-time editor
Hide whitespace changes
Hide unchanged lines
Disable line wrap
Layout
Split
Unified
Diff precision
Smart
Word
Char
Text styles
Change appearance
Syntax highlighting
Choose syntax
Ignore
Transform text
Go to first change
Edit input
Diffchecker Desktop
The most secure way to run Diffchecker. Get the Diffchecker Desktop app: your diffs never leave your computer!
Get Desktop
stuff
Created
2 years ago
Diff never expires
Clear
Export
Share
Explain
268 removals
Lines
Total
Removed
Characters
Total
Removed
To continue using this feature, upgrade to
Diff
checker
Pro
View Pricing
526 lines
Copy
147 additions
Lines
Total
Added
Characters
Total
Added
To continue using this feature, upgrade to
Diff
checker
Pro
View Pricing
404 lines
Copy
Copy
Copied
Copy
Copied
diff --git a/
discord
/client.py b/
discord
/client.py
diff --git a/
disnake
/client.py b/
disnake
/client.py
--- a/
discord
/client.py
--- a/
disnake
/client.py
+++ b/
discord
/client.py
+++ b/
disnake
/client.py
-from .emoji import Emoji
-from .emoji import Emoji
Copy
Copied
Copy
Copied
+from .emoji import App
Emoji, GuildEmoji
+from .emoji import App
lication
Emoji, GuildEmoji
+
cache_app_emojis:
:class:`
bool
`
+
cache_app_emojis:
bool
= False,
+ Whether to automatically fetch and cache the application's emojis on startup and when fetching. Defaults to ``False``.
+
cache_app_
emojis
=cache_app_emojis,
+
+
cache
_app_emojis: bool,
+ .. warning::
+
cache_app
_emojis
=cache_app_emojis,
+
- def emojis(self) ->
L
ist[Emoji]:
+
There are no events related to application
emojis
- if any are created/deleted on the
- """
List[:class:`.Emoji`]:
The emojis that the connected client has."""
+
Developer Dashboard while the client is running, the
cache
will not be updated until you manually
+ def emojis(self) ->
L
ist[
Union[
GuildEmoji
,
App
lication
Emoji]
]
:
+
run :func:`fetch
_emojis
`.
+
+ .. versionadded:: 2.7
- def emojis(self) ->
l
ist[Emoji]:
- """
The emojis that the connected client has."""
+ def emojis(self) ->
l
ist[
GuildEmoji
|
App
Emoji]
:
+ """The emojis that the connected client has.
+ """The emojis that the connected client has.
+
+
+ .. note::
+ .. note::
Copy
Copied
Copy
Copied
+
+ This only includes the application's emojis if :attr:`~Client.cache_app_emojis` is
+ This only includes the application's emojis if :attr:`~Client.cache_app_emojis` is
``True``.
+
``True``.
+ """
+ """
Copy
Copied
Copy
Copied
- def get_emoji(self, id: int, /) ->
Emoji
| None
:
- def get_emoji(self, id: int, /) ->
Optional[
Emoji
]
:
+ def get_emoji(self, id: int, /) ->
GuildEmoji | AppEmoji | None:
+ def get_emoji(self, id: int, /) ->
Optional[
Union[
GuildEmoji
,
App
lication
Emoji
]]:
-
Optional[
:class:`.Emoji`]
+ Optional[:class:`.
GuildEmoji
` | :class:`.
App
Emoji
`]
+
+
Copy
Copied
Copy
Copied
+ async def fetch_
emojis(self) -> list[App
Emoji]:
+ async def fetch_
application_
emojis(self) -> list[App
lication
Emoji]:
+ r"""|coro|
+ r"""|coro|
Copy
Copied
Copy
Copied
+
+ Retrieves all custom :class:`App
lication
Emoji`\s from the application.
+ Retrieves all custom :class:`App
Emoji`\s from the application.
+
+
+ Raises
+ Raises
Copy
Copied
Copy
Copied
+ ------
---
+ ------
+ HTTPException
+ HTTPException
+ An error occurred fetching the emojis.
+ An error occurred fetching the emojis.
+
+
+ Returns
+ Returns
Copy
Copied
Copy
Copied
+ -------
-
+ -------
+ List[:class:`App
Emoji`]
+ List[:class:`App
lication
Emoji`]
+ The retrieved emojis.
+ The retrieved emojis.
+ """
+ """
Copy
Copied
Copy
Copied
+ data = await self._
state
.http.get_all_application_emojis(self.application_id)
+ data = await self._
connection
.http.get_all_application_emojis(self.application_id)
+ return [
+ return [
Copy
Copied
Copy
Copied
+ self._
state.maybe_
store_
app
_emoji(self.application_id, d)
+ self._
connection.
store_
application
_emoji(self.application_id, d)
for d in data["items"]
+
for d in data["items"]
+ ]
+ ]
+
+
Copy
Copied
Copy
Copied
+ async def fetch_
emoji(self, emoji_id: int, /) -> App
Emoji:
+ async def fetch_
application_
emoji(self, emoji_id: int, /) -> App
lication
Emoji:
+ """|coro|
+ """|coro|
Copy
Copied
Copy
Copied
+
+ Retrieves a custom :class:`App
lication
Emoji` from the application.
+ Retrieves a custom :class:`App
Emoji` from the application.
+
+
+ Parameters
+ Parameters
+ ----------
+ ----------
+ emoji_id: :class:`int`
+ emoji_id: :class:`int`
+ The emoji's ID.
+ The emoji's ID.
+
+
+ Returns
+ Returns
+ -------
+ -------
Copy
Copied
Copy
Copied
+ :class:`App
Emoji`
+ :class:`App
lication
Emoji`
+ The retrieved emoji.
+ The retrieved emoji.
+
+
+ Raises
+ Raises
+ ------
+ ------
+ NotFound
+ NotFound
+ The emoji requested could not be found.
+ The emoji requested could not be found.
+ HTTPException
+ HTTPException
+ An error occurred fetching the emoji.
+ An error occurred fetching the emoji.
+ """
+ """
Copy
Copied
Copy
Copied
+ data = await self._
state
.http.get_application_emoji(
+ data = await self._
connection
.http.get_application_emoji(
self.application_id, emoji_id
)
+
self.application_id, emoji_id
+ return self._
connection.
store_
application
_emoji(self.application_id, data)
+
)
+ return self._
state.maybe_
store_
app
_emoji(self.application_id, data)
+
+
Copy
Copied
Copy
Copied
+ async def create_
emoji(
+ async def create_
application_
emoji(
+ self,
+ self,
+ *,
+ *,
+ name: str,
+ name: str,
+ image: bytes,
+ image: bytes,
Copy
Copied
Copy
Copied
+ ) -> App
Emoji:
+ ) -> App
lication
Emoji:
+ r"""|coro|
+ r"""|coro|
Copy
Copied
Copy
Copied
+
+ Creates a custom :class:`App
lication
Emoji` for the application.
+ Creates a custom :class:`App
Emoji` for the application.
+
+ There is currently a limit of 2000 emojis per application.
+ There is currently a limit of 2000 emojis per application.
+
+
+ Parameters
+ Parameters
Copy
Copied
Copy
Copied
+ ----------
-
+ ----------
+ name: :class:`str`
+ name: :class:`str`
+ The emoji name. Must be at least 2 characters.
+ The emoji name. Must be at least 2 characters.
+ image: :class:`bytes`
+ image: :class:`bytes`
+ The :term:`py:bytes-like object` representing the image data to use.
+ The :term:`py:bytes-like object` representing the image data to use.
+ Only JPG, PNG and GIF images are supported.
+ Only JPG, PNG and GIF images are supported.
+
+
+ Raises
+ Raises
Copy
Copied
Copy
Copied
+ ------
-
+ ------
+ Forbidden
+ Forbidden
+ You are not allowed to create emojis.
+ You are not allowed to create emojis.
+ HTTPException
+ HTTPException
+ An error occurred creating an emoji.
+ An error occurred creating an emoji.
+
+
+ Returns
+ Returns
Copy
Copied
Copy
Copied
+ -------
-
+ -------
+ :class:`App
Emoji`
+ :class:`App
lication
Emoji`
+ The created emoji.
+ The created emoji.
+ """
+ """
Copy
Copied
Copy
Copied
+
+ img = utils._bytes_to_base64_data(image)
+ img = utils._bytes_to_base64_data(image)
Copy
Copied
Copy
Copied
+ data = await self._
state
.http.create_application_emoji(
+ data = await self._
connection
.http.create_application_emoji(
self.application_id, name, img
)
+
self.application_id, name, img
+ return self._
connection.
store_
application_emoji(self.application_id,
data)
+
)
diff --git a/
disnake
/emoji.py b/
disnake
/emoji.py
+ return self._
state.maybe_
store_
app_emoji(self.application_id, data)
--- a/
disnake
/emoji.py
+
+++ b/
disnake
/emoji.py
+ async def delete_emoji(
-from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Tuple, Union
+ self, emoji: Snowflake, *, reason: str | None = None
+from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Tuple
+ ) -> None:
+ """|coro|
+
+ Deletes the custom :class:`AppEmoji` from the application.
+
+ Parameters
+ ----------
+ emoji: :class:`abc.Snowflake`
+ The emoji you are deleting.
+
+ Raises
+ ------
+ HTTPException
+ An error occurred deleting the emoji.
+ """
+
+ await self._state.http.delete_
application_emoji(self.application_id,
emoji.id)
+ if self._state.cache_app_emojis and self._state.get_emoji(emoji.id):
+ self._state.remove_emoji(emoji)
diff --git a/
discord
/emoji.py b/
discord
/emoji.py
--- a/
discord
/emoji.py
+++ b/
discord
/emoji.py
-__all__ = ("Emoji",)
-__all__ = ("Emoji",)
Copy
Copied
Copy
Copied
+__all__ = (
+__all__ = (
"Emoji",
"GuildEmoji",
"App
lication
Emoji"
)
+
"Emoji",
- from .guild_preview import GuildPreview
+
"GuildEmoji",
+
"App
Emoji"
,
+)
-class Emoji(_EmojiTag, AssetMixin):
-class Emoji(_EmojiTag, AssetMixin):
Copy
Copied
Copy
Copied
- """Represents a custom emoji.
-
- Depending on the way this object was created, some attributes can
- have a value of ``None``.
-
- .. container:: operations
-
- .. describe:: x == y
-
- Checks if two emoji are the same.
-
- .. describe:: x != y
-
- Checks if two emoji are not the same.
-
- .. describe:: hash(x)
-
- Return the emoji's hash.
-
- .. describe:: iter(x)
-
- Returns an iterator of ``(field, value)`` pairs. This allows this class
- to be used as an iterable in list/dict/etc constructions.
-
- .. describe:: str(x)
-
- Returns the emoji rendered for discord.
-
- Attributes
- ----------
- name: :class:`str`
- The name of the emoji.
- id: :class:`int`
- The emoji's ID.
- require_colons: :class:`bool`
- If colons are required to use this emoji in the client (:PJSalt: vs PJSalt).
- animated: :class:`bool`
- Whether an emoji is animated or not.
- managed: :class:`bool`
- If this emoji is managed by a Twitch integration.
- guild_id: :class:`int`
- The guild ID the emoji belongs to.
- available: :class:`bool`
- Whether the emoji is available for use.
- user: Optional[:class:`User`]
- The user that created the emoji. This can only be retrieved using :meth:`Guild.fetch_emoji` and
- having the :attr:`~Permissions.manage_emojis` permission.
- """
+class BaseEmoji(_EmojiTag, AssetMixin):
+class BaseEmoji(_EmojiTag, AssetMixin):
+ "application_id",
+ "application_id",
Copy
Copied
Copy
Copied
- def __init__(
self, *, guild:
Guild
, state: ConnectionState, data: EmojiPayload
)
:
- def __init__(
-
self, *, guild:
Union[
Guild
, GuildPreview]
, state: ConnectionState, data: EmojiPayload
- ) -> None
:
- self.guild_id: int = guild.id
- self.guild_id: int = guild.id
Copy
Copied
Copy
Copied
+ def __init__(self, *, state: ConnectionState, data: EmojiPayload)
:
+ def __init__(self, *, state: ConnectionState, data: EmojiPayload)
-> None
:
-
"<Emoji
"
-
return f
"<Emoji
id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>"
- f"
id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>"
+
return f
"<BaseEmoji
id={self.id} name={self.name!r} animated={self.animated}>"
+
"<BaseEmoji
" f"
id={self.id} name={self.name!r} animated={self.animated}>"
+
+
+class GuildEmoji(BaseEmoji):
+class GuildEmoji(BaseEmoji):
+ """Represents a custom emoji in a guild.
+ """Represents a custom emoji in a guild.
+
+
Copy
Copied
Copy
Copied
+ Depending on the way this object was created, some
attributes can
+ Depending on the way this object was created, some
of the
attributes can
+ have a value of ``None``.
+ have a value of ``None``.
+
+
Copy
Copied
Copy
Copied
+ ..
container
:: operations
+ ..
collapse
:: operations
+
+
+ .. describe:: x == y
+ .. describe:: x == y
+
+
+ Checks if two emoji are the same.
+ Checks if two emoji are the same.
+
+
+ .. describe:: x != y
+ .. describe:: x != y
+
+
+ Checks if two emoji are not the same.
+ Checks if two emoji are not the same.
+
+
+ .. describe:: hash(x)
+ .. describe:: hash(x)
+
+
+ Return the emoji's hash.
+ Return the emoji's hash.
+
+
+ .. describe:: iter(x)
+ .. describe:: iter(x)
+
+
+ Returns an iterator of ``(field, value)`` pairs. This allows this class
+ Returns an iterator of ``(field, value)`` pairs. This allows this class
+ to be used as an iterable in list/dict/etc constructions.
+ to be used as an iterable in list/dict/etc constructions.
+
+
+ .. describe:: str(x)
+ .. describe:: str(x)
+
+
Copy
Copied
Copy
Copied
+ Returns the emoji rendered for
d
iscord.
+ Returns the emoji rendered for
D
iscord.
+
+
+ Attributes
+ Attributes
+ ----------
+ ----------
+ name: :class:`str`
+ name: :class:`str`
Copy
Copied
Copy
Copied
+ The
name of the
emoji
.
+ The
emoji
's name
.
+ id: :class:`int`
+ id: :class:`int`
+ The emoji's ID.
+ The emoji's ID.
+ require_colons: :class:`bool`
+ require_colons: :class:`bool`
Copy
Copied
Copy
Copied
+
If
colons are required to use this emoji in the client (:PJSalt: vs PJSalt).
+
Whether
colons are required to use this emoji in the client (:PJSalt: vs PJSalt).
+ animated: :class:`bool`
+ animated: :class:`bool`
Copy
Copied
Copy
Copied
+ Whether
an
emoji is animated or not.
+ Whether
the
emoji is animated or not.
+ managed: :class:`bool`
+ managed: :class:`bool`
Copy
Copied
Copy
Copied
+
If this
emoji is managed by a Twitch integration.
+
Whether the
emoji is managed by a Twitch integration.
+ guild_id: :class:`int`
+ guild_id: :class:`int`
+ The guild ID the emoji belongs to.
+ The guild ID the emoji belongs to.
+ available: :class:`bool`
+ available: :class:`bool`
+ Whether the emoji is available for use.
+ Whether the emoji is available for use.
+ user: Optional[:class:`User`]
+ user: Optional[:class:`User`]
Copy
Copied
Copy
Copied
+ The user that created
the
emoji. This can only be retrieved using
:meth:`Guild.fetch_emoji`
and
+ The user that created
this
emoji. This can only be retrieved using
+ having the :attr:`~Permissions.manage_
emojis
` permission.
+
:meth:`Guild.fetch_emoji`
/:meth:`Guild.fetch_emojis` while
+ having the :attr:`~Permissions.manage_
guild_expressions
` permission.
+ """
+ """
+
+
+ def __init__(self, *, guild: Guild, state: ConnectionState, data: EmojiPayload):
+ def __init__(self, *, guild: Guild, state: ConnectionState, data: EmojiPayload):
+ self.guild_id: int = guild.id
+ self.guild_id: int = guild.id
Copy
Copied
Copy
Copied
+ super().__init__(state
, data
)
+ super().__init__(state
=state
, data
=data
)
+
+
+ def __repr__(self) -> str:
+ def __repr__(self) -> str:
Copy
Copied
Copy
Copied
+ return
(
+ return
f
"<GuildEmoji
id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>"
+
"<GuildEmoji
"
+ f"
id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>"
+ )
+
+
- ) -> Emoji:
- ) -> Emoji:
+ ) -> GuildEmoji:
+ ) -> GuildEmoji:
- :class:`Emoji`
- :class:`Emoji`
+ :class:`GuildEmoji`
+ :class:`GuildEmoji`
- return Emoji(guild=self.guild, data=data, state=self._state)
- return Emoji(guild=self.guild, data=data, state=self._state)
+ return GuildEmoji(guild=self.guild, data=data, state=self._state)
+ return GuildEmoji(guild=self.guild, data=data, state=self._state)
+
+
+
+
+Emoji = GuildEmoji
+Emoji = GuildEmoji
+
+
+
+
Copy
Copied
Copy
Copied
+class App
Emoji(BaseEmoji):
+class App
lication
Emoji(BaseEmoji):
+ """Represents a custom emoji from an application.
+ """Represents a custom emoji from an application.
+
+
+ Depending on the way this object was created, some attributes can
+ Depending on the way this object was created, some attributes can
+ have a value of ``None``.
+ have a value of ``None``.
+
+
Copy
Copied
Copy
Copied
+ .. versionadded:: 2.7
+
+ .. container:: operations
+ .. container:: operations
+
+
+ .. describe:: x == y
+ .. describe:: x == y
+
+
+ Checks if two emoji are the same.
+ Checks if two emoji are the same.
+
+
+ .. describe:: x != y
+ .. describe:: x != y
+
+
+ Checks if two emoji are not the same.
+ Checks if two emoji are not the same.
+
+
+ .. describe:: hash(x)
+ .. describe:: hash(x)
+
+
+ Return the emoji's hash.
+ Return the emoji's hash.
+
+
+ .. describe:: iter(x)
+ .. describe:: iter(x)
+
+
+ Returns an iterator of ``(field, value)`` pairs. This allows this class
+ Returns an iterator of ``(field, value)`` pairs. This allows this class
+ to be used as an iterable in list/dict/etc constructions.
+ to be used as an iterable in list/dict/etc constructions.
+
+
+ .. describe:: str(x)
+ .. describe:: str(x)
+
+
+ Returns the emoji rendered for discord.
+ Returns the emoji rendered for discord.
+
+
+ Attributes
+ Attributes
+ ----------
+ ----------
+ name: :class:`str`
+ name: :class:`str`
+ The name of the emoji.
+ The name of the emoji.
+ id: :class:`int`
+ id: :class:`int`
+ The emoji's ID.
+ The emoji's ID.
+ require_colons: :class:`bool`
+ require_colons: :class:`bool`
+ If colons are required to use this emoji in the client (:PJSalt: vs PJSalt).
+ If colons are required to use this emoji in the client (:PJSalt: vs PJSalt).
+ animated: :class:`bool`
+ animated: :class:`bool`
+ Whether an emoji is animated or not.
+ Whether an emoji is animated or not.
+ managed: :class:`bool`
+ managed: :class:`bool`
+ If this emoji is managed by a Twitch integration.
+ If this emoji is managed by a Twitch integration.
+ application_id: Optional[:class:`int`]
+ application_id: Optional[:class:`int`]
+ The application ID the emoji belongs to, if available.
+ The application ID the emoji belongs to, if available.
+ available: :class:`bool`
+ available: :class:`bool`
+ Whether the emoji is available for use.
+ Whether the emoji is available for use.
+ user: Optional[:class:`User`]
+ user: Optional[:class:`User`]
+ The user that created the emoji.
+ The user that created the emoji.
+ """
+ """
+
+
Copy
Copied
Copy
Copied
+ def __init__(
+ def __init__(
self, *, application_id: int, state: ConnectionState, data: EmojiPayload
):
+
self, *, application_id: int, state: ConnectionState, data: EmojiPayload
+
):
+ self.application_id: int = application_id
+ self.application_id: int = application_id
Copy
Copied
Copy
Copied
+ super().__init__(state
, data
)
+ super().__init__(state
=state
, data
=data)
+
+ def __repr__(self) -> str:
+ return "<AppEmoji" f" id={self.id} name={self.name!r} animated={self.animated}>"
+
+ @property
+ def guild(self) -> Guild:
+ """The guild this emoji belongs to. This is always `None` for :class:`AppEmoji`."""
+ return None
+
+
Copy
Copied
Copy
Copied
+ @property
+ def
__repr__
(self)
:
+ def
roles
(self)
-> list[Role]
:
+ return f"<ApplicationEmoji id={self.id} name={self.name!r} animated={self.animated}>"
+ """A :class:`list` of roles that is allowed to use this emoji. This is always empty for :class:`AppEmoji`."""
+ return []
+
+
+ def is_usable(self) -> bool:
+ def is_usable(self) -> bool:
+ """Whether the bot can use this emoji."""
+ """Whether the bot can use this emoji."""
+ return self.application_id == self._state.application_id
+ return self.application_id == self._state.application_id
+
+
+ async def delete(self) -> None:
+ async def delete(self) -> None:
+ """|coro|
+ """|coro|
Copy
Copied
Copy
Copied
+
+ Deletes the application emoji.
+ Deletes the application emoji.
Copy
Copied
Copy
Copied
+
+ You must own the emoji to do this.
+ You must own the emoji to do this.
+
+
+ Raises
+ Raises
+ ------
+ ------
+ Forbidden
+ Forbidden
+ You are not allowed to delete the emoji.
+ You are not allowed to delete the emoji.
+ HTTPException
+ HTTPException
+ An error occurred deleting the emoji.
+ An error occurred deleting the emoji.
+ """
+ """
Copy
Copied
Copy
Copied
+
+ await self._state.http.delete_application_emoji(self.application_id, self.id)
+ await self._state.http.delete_application_emoji(self.application_id, self.id)
+ if self._state.cache_app_emojis and self._state.get_emoji(self.id):
+ if self._state.cache_app_emojis and self._state.get_emoji(self.id):
+ self._state._remove_emoji(self)
+ self._state._remove_emoji(self)
+
+
+ async def edit(
+ async def edit(
+ self,
+ self,
+ *,
+ *,
+ name: str = MISSING,
+ name: str = MISSING,
Copy
Copied
Copy
Copied
+ ) -> App
Emoji:
+ ) -> App
lication
Emoji:
+ r"""|coro|
+ r"""|coro|
Copy
Copied
Copy
Copied
+
+ Edits the application emoji.
+ Edits the application emoji.
Copy
Copied
Copy
Copied
+
+ You must own the emoji to do this.
+ You must own the emoji to do this.
+
+
+ Parameters
+ Parameters
Copy
Copied
Copy
Copied
+ ----------
-
+ ----------
+ name: :class:`str`
+ name: :class:`str`
+ The new emoji name.
+ The new emoji name.
+
+
+ Raises
+ Raises
Copy
Copied
Copy
Copied
+ ------
-
+ ------
+ Forbidden
+ Forbidden
+ You are not allowed to edit the emoji.
+ You are not allowed to edit the emoji.
+ HTTPException
+ HTTPException
+ An error occurred editing the emoji.
+ An error occurred editing the emoji.
+
+
+ Returns
+ Returns
Copy
Copied
Copy
Copied
+ -------
-
+ -------
+ :class:`App
Emoji`
+ :class:`App
lication
Emoji`
+ The newly updated emoji.
+ The newly updated emoji.
+ """
+ """
Copy
Copied
Copy
Copied
+
+ payload = {}
+ payload = {}
+ if name is not MISSING:
+ if name is not MISSING:
+ payload["name"] = name
+ payload["name"] = name
+
+
+ data = await self._state.http.edit_application_emoji(
+ data = await self._state.http.edit_application_emoji(
+ self.application_id, self.id, payload=payload
+ self.application_id, self.id, payload=payload
+ )
+ )
Copy
Copied
Copy
Copied
+ return self._state.
maybe_
store_
app
_emoji(self.application_id, data)
+ return self._state.
store_
application
_emoji(self.application_id, data)
diff --git a/
discord
/http.py b/
discord
/http.py
diff --git a/
disnake/ext/commands/bot.py b/disnake/ext/commands/bot.py
--- a/
discord
/http.py
--- a/disnake/ext/commands/bot.py
+++ b/
discord
/http.py
+++ b/disnake/ext/commands/bot.py
+ def get_all_application_emojis(
+
+
self, application_id: Snowflake
+ cache_app_emojis: :class:`bool`
+
) -> Response[
l
ist[emoji.Emoji]]:
+ Whether to automatically fetch and cache the application's emojis on startup and when fetching.
+ Defaults to ``False``.
+ cache_app_emojis: bool = False,
+ cache_app_emojis: bool = False,
+
+ cache_app_emojis: :class:`bool`
+ Whether to automatically fetch and cache the application's emojis on startup and when fetching.
+ Defaults to ``False``.
+ cache_app_emojis: bool = False,
+ cache_app_emojis: bool = False,
diff --git a/disnake
/http.py b/
disnake
/http.py
--- a/
disnake
/http.py
+++ b/
disnake
/http.py
+ def get_all_application_emojis(
self, application_id: Snowflake
) -> Response[
L
ist[emoji.Emoji]]:
+ return self.request(
+ return self.request(
Copy
Copied
Copy
Copied
+ Route(
+ Route(
"GET",
"/applications/{application_id}/emojis",
application_id=application_id
)
+
"GET",
+
"/applications/{application_id}/emojis",
+
application_id=application_id
,
+
)
+ )
+ )
+
+
+ def get_application_emoji(
+ def get_application_emoji(
+ self, application_id: Snowflake, emoji_id: Snowflake
+ self, application_id: Snowflake, emoji_id: Snowflake
+ ) -> Response[emoji.Emoji]:
+ ) -> Response[emoji.Emoji]:
+ return self.request(
+ return self.request(
+ Route(
+ Route(
+ "GET",
+ "GET",
+ "/applications/{application_id}/emojis/{emoji_id}",
+ "/applications/{application_id}/emojis/{emoji_id}",
+ application_id=application_id,
+ application_id=application_id,
+ emoji_id=emoji_id,
+ emoji_id=emoji_id,
+ )
+ )
+ )
+ )
+
+
+ def create_application_emoji(
+ def create_application_emoji(
+ self,
+ self,
+ application_id: Snowflake,
+ application_id: Snowflake,
+ name: str,
+ name: str,
Copy
Copied
Copy
Copied
+ image:
bytes
,
+ image:
str
,
+ ) -> Response[emoji.Emoji]:
+ ) -> Response[emoji.Emoji]:
Copy
Copied
Copy
Copied
+ payload = {
+ payload = {
"name": name, "image": image}
+ "name": name,
+ r = Route("POST",
"/applications/{application_id}/emojis",
application_id=application_id
)
+ "image": image,
+ }
+
+ r = Route(
+ "POST",
+
"/applications/{application_id}/emojis",
+
application_id=application_id
,
+
)
+ return self.request(r, json=payload)
+ return self.request(r, json=payload)
+
+
+ def delete_application_emoji(
+ def delete_application_emoji(
+ self,
+ self,
+ application_id: Snowflake,
+ application_id: Snowflake,
+ emoji_id: Snowflake,
+ emoji_id: Snowflake,
+ ) -> Response[None]:
+ ) -> Response[None]:
+ r = Route(
+ r = Route(
+ "DELETE",
+ "DELETE",
+ "/applications/{application_id}/emojis/{emoji_id}",
+ "/applications/{application_id}/emojis/{emoji_id}",
+ application_id=application_id,
+ application_id=application_id,
+ emoji_id=emoji_id,
+ emoji_id=emoji_id,
+ )
+ )
+ return self.request(r)
+ return self.request(r)
+
+
+ def edit_application_emoji(
+ def edit_application_emoji(
+ self,
+ self,
+ application_id: Snowflake,
+ application_id: Snowflake,
+ emoji_id: Snowflake,
+ emoji_id: Snowflake,
+ *,
+ *,
+ payload: dict[str, Any],
+ payload: dict[str, Any],
+ ) -> Response[emoji.Emoji]:
+ ) -> Response[emoji.Emoji]:
+ r = Route(
+ r = Route(
+ "PATCH",
+ "PATCH",
+ "/applications/{application_id}/emojis/{emoji_id}",
+ "/applications/{application_id}/emojis/{emoji_id}",
+ application_id=application_id,
+ application_id=application_id,
+ emoji_id=emoji_id,
+ emoji_id=emoji_id,
+ )
+ )
+ return self.request(r, json=payload)
+ return self.request(r, json=payload)
+
+
Copy
Copied
Copy
Copied
diff --git a/
discord
/state.py b/
discord
/state.py
diff --git a/
disnake
/state.py b/
disnake
/state.py
--- a/
discord
/state.py
--- a/
disnake
/state.py
+++ b/
discord
/state.py
+++ b/
disnake
/state.py
-from .emoji import Emoji
-from .emoji import Emoji
Copy
Copied
Copy
Copied
+from .emoji import App
Emoji, Emoji, GuildEmoji
+from .emoji import App
lication
Emoji, Emoji, GuildEmoji
+ self.cache_app_emojis: bool =
options.get("
cache_app_emojis
", False)
+ cache_app_emojis: bool = False,
+ self.cache_app_emojis: bool =
cache_app_emojis
+
+
Copy
Copied
Copy
Copied
- self._emojis: Dict[int, Emoji] = {}
+ self._emojis: Dict[int, Emoji, ApplicationEmoji] = {}
- def store_emoji(self, guild: Guild, data: EmojiPayload) -> Emoji:
- def store_emoji(self, guild: Guild, data: EmojiPayload) -> Emoji:
+ def store_emoji(self, guild: Guild, data: EmojiPayload) -> GuildEmoji:
+ def store_emoji(self, guild: Guild, data: EmojiPayload) -> GuildEmoji:
- self._emojis[emoji_id] = emoji = Emoji(guild=guild, state=self, data=data)
- self._emojis[emoji_id] = emoji = Emoji(guild=guild, state=self, data=data)
+ self._emojis[emoji_id] = emoji = GuildEmoji(guild=guild, state=self, data=data)
+ self._emojis[emoji_id] = emoji = GuildEmoji(guild=guild, state=self, data=data)
+ return emoji
+ return emoji
+
+
Copy
Copied
Copy
Copied
+ def
maybe_
store_
app
_emoji(
+ def
store_
application
_emoji(
self, application_id: int, data: EmojiPayload
) -> App
lication
Emoji:
+
self, application_id: int, data: EmojiPayload
+ emoji = App
lication
Emoji(application_id=application_id, state=self, data=data)
+
) -> App
Emoji:
+ # the id will be present here
+ emoji = App
Emoji(application_id=application_id, state=self, data=data)
+ if self.cache_app_emojis:
+ if self.cache_app_emojis:
+ emoji_id = int(data["id"]) # type: ignore
+ emoji_id = int(data["id"]) # type: ignore
+ self._emojis[emoji_id] = emoji
+ self._emojis[emoji_id] = emoji
- self._emojis.pop(emoji.id, None)
- self._emojis.pop(emoji.id, None)
Copy
Copied
Copy
Copied
+ self._remove_emoji(emoji
)
+ self._remove_emoji(emoji
.id
)
- def emojis(self) ->
l
ist[Emoji]:
- def emojis(self) ->
L
ist[Emoji]:
+ def emojis(self) ->
l
ist[
GuildEmoji
|
App
Emoji]
:
+ def emojis(self) ->
L
ist[
Union[
GuildEmoji
,
App
lication
Emoji]
]
:
- def get_emoji(self, emoji_id:
int
| None
) ->
Emoji
| None
:
- def get_emoji(self, emoji_id:
Optional[
int
]
) ->
Optional[
Emoji
]
:
+ def get_emoji(self, emoji_id:
int
| None
) ->
GuildEmoji
|
App
Emoji
| None
:
+ def get_emoji(self, emoji_id:
Optional[
int
]
) ->
Optional[Union[
GuildEmoji
,
App
lication
Emoji
]]
:
+ def _remove_emoji(self, emoji:
GuildEmoji
|
App
Emoji
) -> None:
+ def _remove_emoji(self, emoji:
Union[
GuildEmoji
,
App
lication
Emoji
]
) -> None:
+ self._emojis.pop(emoji.id, None)
+ self._emojis.pop(emoji.id, None)
+
+
+ if self.cache_app_emojis and self.application_id:
+ if self.cache_app_emojis and self.application_id:
+ data = await self.http.get_all_application_emojis(self.application_id)
+ data = await self.http.get_all_application_emojis(self.application_id)
+ for e in data.get("items", []):
+ for e in data.get("items", []):
Copy
Copied
Copy
Copied
+ self.
maybe_
store_
app
_emoji(self.application_id, e)
+ self.
store_
application
_emoji(self.application_id, e)
+
diff --git a/docs/api/models.rst b/docs/api/models.rst
--- a/docs/api/models.rst
+++ b/docs/api/models.rst
-.. attributetable:: Emoji
+.. attributetable:: GuildEmoji
-.. autoclass:: Emoji()
+.. autoclass:: GuildEmoji()
+ :members:
+ :inherited-members:
+
+.. attributetable:: AppEmoji
+
+
Copy
Copied
Copy
Copied
+.. autoclass:: AppEmoji()
Saved diffs
Original text
Open file
diff --git a/discord/client.py b/discord/client.py --- a/discord/client.py +++ b/discord/client.py -from .emoji import Emoji +from .emoji import AppEmoji, GuildEmoji + cache_app_emojis: :class:`bool` + Whether to automatically fetch and cache the application's emojis on startup and when fetching. Defaults to ``False``. + + .. warning:: + + There are no events related to application emojis - if any are created/deleted on the + Developer Dashboard while the client is running, the cache will not be updated until you manually + run :func:`fetch_emojis`. + + .. versionadded:: 2.7 - def emojis(self) -> list[Emoji]: - """The emojis that the connected client has.""" + def emojis(self) -> list[GuildEmoji | AppEmoji]: + """The emojis that the connected client has. + + .. note:: + + This only includes the application's emojis if :attr:`~Client.cache_app_emojis` is ``True``. + """ - def get_emoji(self, id: int, /) -> Emoji | None: + def get_emoji(self, id: int, /) -> GuildEmoji | AppEmoji | None: - Optional[:class:`.Emoji`] + Optional[:class:`.GuildEmoji` | :class:`.AppEmoji`] + + async def fetch_emojis(self) -> list[AppEmoji]: + r"""|coro| + + Retrieves all custom :class:`AppEmoji`\s from the application. + + Raises + --------- + HTTPException + An error occurred fetching the emojis. + + Returns + -------- + List[:class:`AppEmoji`] + The retrieved emojis. + """ + data = await self._state.http.get_all_application_emojis(self.application_id) + return [ + self._state.maybe_store_app_emoji(self.application_id, d) + for d in data["items"] + ] + + async def fetch_emoji(self, emoji_id: int, /) -> AppEmoji: + """|coro| + + Retrieves a custom :class:`AppEmoji` from the application. + + Parameters + ---------- + emoji_id: :class:`int` + The emoji's ID. + + Returns + ------- + :class:`AppEmoji` + The retrieved emoji. + + Raises + ------ + NotFound + The emoji requested could not be found. + HTTPException + An error occurred fetching the emoji. + """ + data = await self._state.http.get_application_emoji( + self.application_id, emoji_id + ) + return self._state.maybe_store_app_emoji(self.application_id, data) + + async def create_emoji( + self, + *, + name: str, + image: bytes, + ) -> AppEmoji: + r"""|coro| + + Creates a custom :class:`AppEmoji` for the application. + + There is currently a limit of 2000 emojis per application. + + Parameters + ----------- + name: :class:`str` + The emoji name. Must be at least 2 characters. + image: :class:`bytes` + The :term:`py:bytes-like object` representing the image data to use. + Only JPG, PNG and GIF images are supported. + + Raises + ------- + Forbidden + You are not allowed to create emojis. + HTTPException + An error occurred creating an emoji. + + Returns + -------- + :class:`AppEmoji` + The created emoji. + """ + + img = utils._bytes_to_base64_data(image) + data = await self._state.http.create_application_emoji( + self.application_id, name, img + ) + return self._state.maybe_store_app_emoji(self.application_id, data) + + async def delete_emoji( + self, emoji: Snowflake, *, reason: str | None = None + ) -> None: + """|coro| + + Deletes the custom :class:`AppEmoji` from the application. + + Parameters + ---------- + emoji: :class:`abc.Snowflake` + The emoji you are deleting. + + Raises + ------ + HTTPException + An error occurred deleting the emoji. + """ + + await self._state.http.delete_application_emoji(self.application_id, emoji.id) + if self._state.cache_app_emojis and self._state.get_emoji(emoji.id): + self._state.remove_emoji(emoji) diff --git a/discord/emoji.py b/discord/emoji.py --- a/discord/emoji.py +++ b/discord/emoji.py -__all__ = ("Emoji",) +__all__ = ( + "Emoji", + "GuildEmoji", + "AppEmoji", +) -class Emoji(_EmojiTag, AssetMixin): - """Represents a custom emoji. - - Depending on the way this object was created, some attributes can - have a value of ``None``. - - .. container:: operations - - .. describe:: x == y - - Checks if two emoji are the same. - - .. describe:: x != y - - Checks if two emoji are not the same. - - .. describe:: hash(x) - - Return the emoji's hash. - - .. describe:: iter(x) - - Returns an iterator of ``(field, value)`` pairs. This allows this class - to be used as an iterable in list/dict/etc constructions. - - .. describe:: str(x) - - Returns the emoji rendered for discord. - - Attributes - ---------- - name: :class:`str` - The name of the emoji. - id: :class:`int` - The emoji's ID. - require_colons: :class:`bool` - If colons are required to use this emoji in the client (:PJSalt: vs PJSalt). - animated: :class:`bool` - Whether an emoji is animated or not. - managed: :class:`bool` - If this emoji is managed by a Twitch integration. - guild_id: :class:`int` - The guild ID the emoji belongs to. - available: :class:`bool` - Whether the emoji is available for use. - user: Optional[:class:`User`] - The user that created the emoji. This can only be retrieved using :meth:`Guild.fetch_emoji` and - having the :attr:`~Permissions.manage_emojis` permission. - """ +class BaseEmoji(_EmojiTag, AssetMixin): + "application_id", - def __init__(self, *, guild: Guild, state: ConnectionState, data: EmojiPayload): - self.guild_id: int = guild.id + def __init__(self, *, state: ConnectionState, data: EmojiPayload): - "<Emoji" - f" id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>" + "<BaseEmoji" f" id={self.id} name={self.name!r} animated={self.animated}>" + +class GuildEmoji(BaseEmoji): + """Represents a custom emoji in a guild. + + Depending on the way this object was created, some attributes can + have a value of ``None``. + + .. container:: operations + + .. describe:: x == y + + Checks if two emoji are the same. + + .. describe:: x != y + + Checks if two emoji are not the same. + + .. describe:: hash(x) + + Return the emoji's hash. + + .. describe:: iter(x) + + Returns an iterator of ``(field, value)`` pairs. This allows this class + to be used as an iterable in list/dict/etc constructions. + + .. describe:: str(x) + + Returns the emoji rendered for discord. + + Attributes + ---------- + name: :class:`str` + The name of the emoji. + id: :class:`int` + The emoji's ID. + require_colons: :class:`bool` + If colons are required to use this emoji in the client (:PJSalt: vs PJSalt). + animated: :class:`bool` + Whether an emoji is animated or not. + managed: :class:`bool` + If this emoji is managed by a Twitch integration. + guild_id: :class:`int` + The guild ID the emoji belongs to. + available: :class:`bool` + Whether the emoji is available for use. + user: Optional[:class:`User`] + The user that created the emoji. This can only be retrieved using :meth:`Guild.fetch_emoji` and + having the :attr:`~Permissions.manage_emojis` permission. + """ + + def __init__(self, *, guild: Guild, state: ConnectionState, data: EmojiPayload): + self.guild_id: int = guild.id + super().__init__(state, data) + + def __repr__(self) -> str: + return ( + "<GuildEmoji" + f" id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>" + ) + - ) -> Emoji: + ) -> GuildEmoji: - :class:`Emoji` + :class:`GuildEmoji` - return Emoji(guild=self.guild, data=data, state=self._state) + return GuildEmoji(guild=self.guild, data=data, state=self._state) + + +Emoji = GuildEmoji + + +class AppEmoji(BaseEmoji): + """Represents a custom emoji from an application. + + Depending on the way this object was created, some attributes can + have a value of ``None``. + + .. versionadded:: 2.7 + + .. container:: operations + + .. describe:: x == y + + Checks if two emoji are the same. + + .. describe:: x != y + + Checks if two emoji are not the same. + + .. describe:: hash(x) + + Return the emoji's hash. + + .. describe:: iter(x) + + Returns an iterator of ``(field, value)`` pairs. This allows this class + to be used as an iterable in list/dict/etc constructions. + + .. describe:: str(x) + + Returns the emoji rendered for discord. + + Attributes + ---------- + name: :class:`str` + The name of the emoji. + id: :class:`int` + The emoji's ID. + require_colons: :class:`bool` + If colons are required to use this emoji in the client (:PJSalt: vs PJSalt). + animated: :class:`bool` + Whether an emoji is animated or not. + managed: :class:`bool` + If this emoji is managed by a Twitch integration. + application_id: Optional[:class:`int`] + The application ID the emoji belongs to, if available. + available: :class:`bool` + Whether the emoji is available for use. + user: Optional[:class:`User`] + The user that created the emoji. + """ + + def __init__( + self, *, application_id: int, state: ConnectionState, data: EmojiPayload + ): + self.application_id: int = application_id + super().__init__(state, data) + + def __repr__(self) -> str: + return "<AppEmoji" f" id={self.id} name={self.name!r} animated={self.animated}>" + + @property + def guild(self) -> Guild: + """The guild this emoji belongs to. This is always `None` for :class:`AppEmoji`.""" + return None + + @property + def roles(self) -> list[Role]: + """A :class:`list` of roles that is allowed to use this emoji. This is always empty for :class:`AppEmoji`.""" + return [] + + def is_usable(self) -> bool: + """Whether the bot can use this emoji.""" + return self.application_id == self._state.application_id + + async def delete(self) -> None: + """|coro| + + Deletes the application emoji. + + You must own the emoji to do this. + + Raises + ------ + Forbidden + You are not allowed to delete the emoji. + HTTPException + An error occurred deleting the emoji. + """ + + await self._state.http.delete_application_emoji(self.application_id, self.id) + if self._state.cache_app_emojis and self._state.get_emoji(self.id): + self._state._remove_emoji(self) + + async def edit( + self, + *, + name: str = MISSING, + ) -> AppEmoji: + r"""|coro| + + Edits the application emoji. + + You must own the emoji to do this. + + Parameters + ----------- + name: :class:`str` + The new emoji name. + + Raises + ------- + Forbidden + You are not allowed to edit the emoji. + HTTPException + An error occurred editing the emoji. + + Returns + -------- + :class:`AppEmoji` + The newly updated emoji. + """ + + payload = {} + if name is not MISSING: + payload["name"] = name + + data = await self._state.http.edit_application_emoji( + self.application_id, self.id, payload=payload + ) + return self._state.maybe_store_app_emoji(self.application_id, data) diff --git a/discord/http.py b/discord/http.py --- a/discord/http.py +++ b/discord/http.py + def get_all_application_emojis( + self, application_id: Snowflake + ) -> Response[list[emoji.Emoji]]: + return self.request( + Route( + "GET", + "/applications/{application_id}/emojis", + application_id=application_id, + ) + ) + + def get_application_emoji( + self, application_id: Snowflake, emoji_id: Snowflake + ) -> Response[emoji.Emoji]: + return self.request( + Route( + "GET", + "/applications/{application_id}/emojis/{emoji_id}", + application_id=application_id, + emoji_id=emoji_id, + ) + ) + + def create_application_emoji( + self, + application_id: Snowflake, + name: str, + image: bytes, + ) -> Response[emoji.Emoji]: + payload = { + "name": name, + "image": image, + } + + r = Route( + "POST", + "/applications/{application_id}/emojis", + application_id=application_id, + ) + return self.request(r, json=payload) + + def delete_application_emoji( + self, + application_id: Snowflake, + emoji_id: Snowflake, + ) -> Response[None]: + r = Route( + "DELETE", + "/applications/{application_id}/emojis/{emoji_id}", + application_id=application_id, + emoji_id=emoji_id, + ) + return self.request(r) + + def edit_application_emoji( + self, + application_id: Snowflake, + emoji_id: Snowflake, + *, + payload: dict[str, Any], + ) -> Response[emoji.Emoji]: + r = Route( + "PATCH", + "/applications/{application_id}/emojis/{emoji_id}", + application_id=application_id, + emoji_id=emoji_id, + ) + return self.request(r, json=payload) + diff --git a/discord/state.py b/discord/state.py --- a/discord/state.py +++ b/discord/state.py -from .emoji import Emoji +from .emoji import AppEmoji, Emoji, GuildEmoji + self.cache_app_emojis: bool = options.get("cache_app_emojis", False) + - def store_emoji(self, guild: Guild, data: EmojiPayload) -> Emoji: + def store_emoji(self, guild: Guild, data: EmojiPayload) -> GuildEmoji: - self._emojis[emoji_id] = emoji = Emoji(guild=guild, state=self, data=data) + self._emojis[emoji_id] = emoji = GuildEmoji(guild=guild, state=self, data=data) + return emoji + + def maybe_store_app_emoji( + self, application_id: int, data: EmojiPayload + ) -> AppEmoji: + # the id will be present here + emoji = AppEmoji(application_id=application_id, state=self, data=data) + if self.cache_app_emojis: + emoji_id = int(data["id"]) # type: ignore + self._emojis[emoji_id] = emoji - self._emojis.pop(emoji.id, None) + self._remove_emoji(emoji) - def emojis(self) -> list[Emoji]: + def emojis(self) -> list[GuildEmoji | AppEmoji]: - def get_emoji(self, emoji_id: int | None) -> Emoji | None: + def get_emoji(self, emoji_id: int | None) -> GuildEmoji | AppEmoji | None: + def _remove_emoji(self, emoji: GuildEmoji | AppEmoji) -> None: + self._emojis.pop(emoji.id, None) + + if self.cache_app_emojis and self.application_id: + data = await self.http.get_all_application_emojis(self.application_id) + for e in data.get("items", []): + self.maybe_store_app_emoji(self.application_id, e) + diff --git a/docs/api/models.rst b/docs/api/models.rst --- a/docs/api/models.rst +++ b/docs/api/models.rst -.. attributetable:: Emoji +.. attributetable:: GuildEmoji -.. autoclass:: Emoji() +.. autoclass:: GuildEmoji() + :members: + :inherited-members: + +.. attributetable:: AppEmoji + +.. autoclass:: AppEmoji()
Changed text
Open file
diff --git a/disnake/client.py b/disnake/client.py --- a/disnake/client.py +++ b/disnake/client.py -from .emoji import Emoji +from .emoji import ApplicationEmoji, GuildEmoji + cache_app_emojis: bool = False, + cache_app_emojis=cache_app_emojis, + cache_app_emojis: bool, + cache_app_emojis=cache_app_emojis, - def emojis(self) -> List[Emoji]: - """List[:class:`.Emoji`]: The emojis that the connected client has.""" + def emojis(self) -> List[Union[GuildEmoji, ApplicationEmoji]]: + """The emojis that the connected client has. + + .. note:: + This only includes the application's emojis if :attr:`~Client.cache_app_emojis` is + ``True``. + """ - def get_emoji(self, id: int, /) -> Optional[Emoji]: + def get_emoji(self, id: int, /) -> Optional[Union[GuildEmoji, ApplicationEmoji]]: + + async def fetch_application_emojis(self) -> list[ApplicationEmoji]: + r"""|coro| + Retrieves all custom :class:`ApplicationEmoji`\s from the application. + + Raises + ------ + HTTPException + An error occurred fetching the emojis. + + Returns + ------- + List[:class:`ApplicationEmoji`] + The retrieved emojis. + """ + data = await self._connection.http.get_all_application_emojis(self.application_id) + return [ + self._connection.store_application_emoji(self.application_id, d) for d in data["items"] + ] + + async def fetch_application_emoji(self, emoji_id: int, /) -> ApplicationEmoji: + """|coro| + Retrieves a custom :class:`ApplicationEmoji` from the application. + + Parameters + ---------- + emoji_id: :class:`int` + The emoji's ID. + + Returns + ------- + :class:`ApplicationEmoji` + The retrieved emoji. + + Raises + ------ + NotFound + The emoji requested could not be found. + HTTPException + An error occurred fetching the emoji. + """ + data = await self._connection.http.get_application_emoji(self.application_id, emoji_id) + return self._connection.store_application_emoji(self.application_id, data) + + async def create_application_emoji( + self, + *, + name: str, + image: bytes, + ) -> ApplicationEmoji: + r"""|coro| + Creates a custom :class:`ApplicationEmoji` for the application. + There is currently a limit of 2000 emojis per application. + + Parameters + ---------- + name: :class:`str` + The emoji name. Must be at least 2 characters. + image: :class:`bytes` + The :term:`py:bytes-like object` representing the image data to use. + Only JPG, PNG and GIF images are supported. + + Raises + ------ + Forbidden + You are not allowed to create emojis. + HTTPException + An error occurred creating an emoji. + + Returns + ------- + :class:`ApplicationEmoji` + The created emoji. + """ + img = utils._bytes_to_base64_data(image) + data = await self._connection.http.create_application_emoji(self.application_id, name, img) + return self._connection.store_application_emoji(self.application_id, data) diff --git a/disnake/emoji.py b/disnake/emoji.py --- a/disnake/emoji.py +++ b/disnake/emoji.py -from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Tuple -__all__ = ("Emoji",) +__all__ = ("Emoji", "GuildEmoji", "ApplicationEmoji") - from .guild_preview import GuildPreview -class Emoji(_EmojiTag, AssetMixin): +class BaseEmoji(_EmojiTag, AssetMixin): + "application_id", - def __init__( - self, *, guild: Union[Guild, GuildPreview], state: ConnectionState, data: EmojiPayload - ) -> None: - self.guild_id: int = guild.id + def __init__(self, *, state: ConnectionState, data: EmojiPayload) -> None: - return f"<Emoji id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>" + return f"<BaseEmoji id={self.id} name={self.name!r} animated={self.animated}>" + +class GuildEmoji(BaseEmoji): + """Represents a custom emoji in a guild. + + Depending on the way this object was created, some of the attributes can + have a value of ``None``. + + .. collapse:: operations + + .. describe:: x == y + + Checks if two emoji are the same. + + .. describe:: x != y + + Checks if two emoji are not the same. + + .. describe:: hash(x) + + Return the emoji's hash. + + .. describe:: iter(x) + + Returns an iterator of ``(field, value)`` pairs. This allows this class + to be used as an iterable in list/dict/etc constructions. + + .. describe:: str(x) + + Returns the emoji rendered for Discord. + + Attributes + ---------- + name: :class:`str` + The emoji's name. + id: :class:`int` + The emoji's ID. + require_colons: :class:`bool` + Whether colons are required to use this emoji in the client (:PJSalt: vs PJSalt). + animated: :class:`bool` + Whether the emoji is animated or not. + managed: :class:`bool` + Whether the emoji is managed by a Twitch integration. + guild_id: :class:`int` + The guild ID the emoji belongs to. + available: :class:`bool` + Whether the emoji is available for use. + user: Optional[:class:`User`] + The user that created this emoji. This can only be retrieved using + :meth:`Guild.fetch_emoji`/:meth:`Guild.fetch_emojis` while + having the :attr:`~Permissions.manage_guild_expressions` permission. + """ + + def __init__(self, *, guild: Guild, state: ConnectionState, data: EmojiPayload): + self.guild_id: int = guild.id + super().__init__(state=state, data=data) + + def __repr__(self) -> str: + return f"<GuildEmoji id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>" + - ) -> Emoji: + ) -> GuildEmoji: - :class:`Emoji` + :class:`GuildEmoji` - return Emoji(guild=self.guild, data=data, state=self._state) + return GuildEmoji(guild=self.guild, data=data, state=self._state) + + +Emoji = GuildEmoji + + +class ApplicationEmoji(BaseEmoji): + """Represents a custom emoji from an application. + + Depending on the way this object was created, some attributes can + have a value of ``None``. + + .. container:: operations + + .. describe:: x == y + + Checks if two emoji are the same. + + .. describe:: x != y + + Checks if two emoji are not the same. + + .. describe:: hash(x) + + Return the emoji's hash. + + .. describe:: iter(x) + + Returns an iterator of ``(field, value)`` pairs. This allows this class + to be used as an iterable in list/dict/etc constructions. + + .. describe:: str(x) + + Returns the emoji rendered for discord. + + Attributes + ---------- + name: :class:`str` + The name of the emoji. + id: :class:`int` + The emoji's ID. + require_colons: :class:`bool` + If colons are required to use this emoji in the client (:PJSalt: vs PJSalt). + animated: :class:`bool` + Whether an emoji is animated or not. + managed: :class:`bool` + If this emoji is managed by a Twitch integration. + application_id: Optional[:class:`int`] + The application ID the emoji belongs to, if available. + available: :class:`bool` + Whether the emoji is available for use. + user: Optional[:class:`User`] + The user that created the emoji. + """ + + def __init__(self, *, application_id: int, state: ConnectionState, data: EmojiPayload): + self.application_id: int = application_id + super().__init__(state=state, data=data) + + def __repr__(self): + return f"<ApplicationEmoji id={self.id} name={self.name!r} animated={self.animated}>" + + def is_usable(self) -> bool: + """Whether the bot can use this emoji.""" + return self.application_id == self._state.application_id + + async def delete(self) -> None: + """|coro| + Deletes the application emoji. + You must own the emoji to do this. + + Raises + ------ + Forbidden + You are not allowed to delete the emoji. + HTTPException + An error occurred deleting the emoji. + """ + await self._state.http.delete_application_emoji(self.application_id, self.id) + if self._state.cache_app_emojis and self._state.get_emoji(self.id): + self._state._remove_emoji(self) + + async def edit( + self, + *, + name: str = MISSING, + ) -> ApplicationEmoji: + r"""|coro| + Edits the application emoji. + You must own the emoji to do this. + + Parameters + ---------- + name: :class:`str` + The new emoji name. + + Raises + ------ + Forbidden + You are not allowed to edit the emoji. + HTTPException + An error occurred editing the emoji. + + Returns + ------- + :class:`ApplicationEmoji` + The newly updated emoji. + """ + payload = {} + if name is not MISSING: + payload["name"] = name + + data = await self._state.http.edit_application_emoji( + self.application_id, self.id, payload=payload + ) + return self._state.store_application_emoji(self.application_id, data) diff --git a/disnake/ext/commands/bot.py b/disnake/ext/commands/bot.py --- a/disnake/ext/commands/bot.py +++ b/disnake/ext/commands/bot.py + + cache_app_emojis: :class:`bool` + Whether to automatically fetch and cache the application's emojis on startup and when fetching. + Defaults to ``False``. + cache_app_emojis: bool = False, + cache_app_emojis: bool = False, + + cache_app_emojis: :class:`bool` + Whether to automatically fetch and cache the application's emojis on startup and when fetching. + Defaults to ``False``. + cache_app_emojis: bool = False, + cache_app_emojis: bool = False, diff --git a/disnake/http.py b/disnake/http.py --- a/disnake/http.py +++ b/disnake/http.py + def get_all_application_emojis(self, application_id: Snowflake) -> Response[List[emoji.Emoji]]: + return self.request( + Route("GET", "/applications/{application_id}/emojis", application_id=application_id) + ) + + def get_application_emoji( + self, application_id: Snowflake, emoji_id: Snowflake + ) -> Response[emoji.Emoji]: + return self.request( + Route( + "GET", + "/applications/{application_id}/emojis/{emoji_id}", + application_id=application_id, + emoji_id=emoji_id, + ) + ) + + def create_application_emoji( + self, + application_id: Snowflake, + name: str, + image: str, + ) -> Response[emoji.Emoji]: + payload = {"name": name, "image": image} + r = Route("POST", "/applications/{application_id}/emojis", application_id=application_id) + return self.request(r, json=payload) + + def delete_application_emoji( + self, + application_id: Snowflake, + emoji_id: Snowflake, + ) -> Response[None]: + r = Route( + "DELETE", + "/applications/{application_id}/emojis/{emoji_id}", + application_id=application_id, + emoji_id=emoji_id, + ) + return self.request(r) + + def edit_application_emoji( + self, + application_id: Snowflake, + emoji_id: Snowflake, + *, + payload: dict[str, Any], + ) -> Response[emoji.Emoji]: + r = Route( + "PATCH", + "/applications/{application_id}/emojis/{emoji_id}", + application_id=application_id, + emoji_id=emoji_id, + ) + return self.request(r, json=payload) + diff --git a/disnake/state.py b/disnake/state.py --- a/disnake/state.py +++ b/disnake/state.py -from .emoji import Emoji +from .emoji import ApplicationEmoji, Emoji, GuildEmoji + cache_app_emojis: bool = False, + self.cache_app_emojis: bool = cache_app_emojis + - self._emojis: Dict[int, Emoji] = {} + self._emojis: Dict[int, Emoji, ApplicationEmoji] = {} - def store_emoji(self, guild: Guild, data: EmojiPayload) -> Emoji: + def store_emoji(self, guild: Guild, data: EmojiPayload) -> GuildEmoji: - self._emojis[emoji_id] = emoji = Emoji(guild=guild, state=self, data=data) + self._emojis[emoji_id] = emoji = GuildEmoji(guild=guild, state=self, data=data) + return emoji + + def store_application_emoji(self, application_id: int, data: EmojiPayload) -> ApplicationEmoji: + emoji = ApplicationEmoji(application_id=application_id, state=self, data=data) + if self.cache_app_emojis: + emoji_id = int(data["id"]) # type: ignore + self._emojis[emoji_id] = emoji - self._emojis.pop(emoji.id, None) + self._remove_emoji(emoji.id) - def emojis(self) -> List[Emoji]: + def emojis(self) -> List[Union[GuildEmoji, ApplicationEmoji]]: - def get_emoji(self, emoji_id: Optional[int]) -> Optional[Emoji]: + def get_emoji(self, emoji_id: Optional[int]) -> Optional[Union[GuildEmoji, ApplicationEmoji]]: + def _remove_emoji(self, emoji: Union[GuildEmoji, ApplicationEmoji]) -> None: + self._emojis.pop(emoji.id, None) + + if self.cache_app_emojis and self.application_id: + data = await self.http.get_all_application_emojis(self.application_id) + for e in data.get("items", []): + self.store_application_emoji(self.application_id, e) +
Find difference