Skip to content

Commit

Permalink
refactor: Modified the code to comply with the review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mantouisyummy committed Jul 22, 2024
1 parent ee42cab commit 1ca8f16
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 62 deletions.
4 changes: 2 additions & 2 deletions disnake/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
from .channel import CategoryChannel, DMChannel, PartialMessageable
from .client import Client
from .embeds import Embed
from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji
from .enums import InviteTarget
from .guild import Guild, GuildMessageable
from .guild_scheduled_event import GuildScheduledEvent
Expand Down Expand Up @@ -338,7 +338,7 @@ async def _edit(
video_quality_mode: VideoQualityMode = MISSING,
flags: ChannelFlags = MISSING,
available_tags: Sequence[ForumTag] = MISSING,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = MISSING,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = MISSING,
default_sort_order: Optional[ThreadSortOrder] = MISSING,
default_layout: ThreadLayout = MISSING,
reason: Optional[str] = None,
Expand Down
6 changes: 3 additions & 3 deletions disnake/audit_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from .app_commands import APIApplicationCommand
from .automod import AutoModRule
from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji, Emoji
from .guild import Guild
from .guild_scheduled_event import GuildScheduledEvent
from .integrations import PartialIntegration
Expand Down Expand Up @@ -280,7 +280,7 @@ def _transform_automod_trigger_metadata(

def _transform_default_reaction(
entry: AuditLogEntry, data: Optional[DefaultReactionPayload]
) -> Optional[Union[Emoji, ApplicationEmoji, PartialEmoji]]:
) -> Optional[Union[AnyEmoji, PartialEmoji]]:
if data is None:
return None
return entry._state._get_emoji_from_fields(
Expand Down Expand Up @@ -817,7 +817,7 @@ def _convert_target_invite(self, target_id: int) -> Invite:
def _convert_target_webhook(self, target_id: int) -> Union[Webhook, Object]:
return self._webhooks.get(target_id) or Object(id=target_id)

def _convert_target_emoji(self, target_id: int) -> Union[Emoji, ApplicationEmoji, Object]:
def _convert_target_emoji(self, target_id: int) -> Union[AnyEmoji, Object]:
return self._state.get_emoji(target_id) or Object(id=target_id)

def _convert_target_message(self, target_id: int) -> Union[Member, User, Object, None]:
Expand Down
16 changes: 8 additions & 8 deletions disnake/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
from .abc import Snowflake, SnowflakeTime
from .asset import AssetBytes
from .embeds import Embed
from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji
from .guild import Guild, GuildChannel as GuildChannelType
from .member import Member, VoiceState
from .message import AllowedMentions, Message, PartialMessage
Expand Down Expand Up @@ -3314,7 +3314,7 @@ def requires_tag(self) -> bool:
return self.flags.require_tag

@property
def default_reaction(self) -> Optional[Union[Emoji, ApplicationEmoji, PartialEmoji]]:
def default_reaction(self) -> Optional[Union[AnyEmoji, PartialEmoji]]:
"""Optional[Union[:class:`Emoji`, :class:`PartialEmoji`]]:
The default emoji shown for reacting to threads.
Expand Down Expand Up @@ -3932,7 +3932,7 @@ async def edit(
flags: ChannelFlags = ...,
require_tag: bool = ...,
available_tags: Sequence[ForumTag] = ...,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = ...,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = ...,
default_sort_order: Optional[ThreadSortOrder] = ...,
default_layout: ThreadLayout = ...,
reason: Optional[str] = ...,
Expand All @@ -3955,7 +3955,7 @@ async def edit(
flags: ChannelFlags = MISSING,
require_tag: bool = MISSING,
available_tags: Sequence[ForumTag] = MISSING,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = MISSING,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = MISSING,
default_sort_order: Optional[ThreadSortOrder] = MISSING,
default_layout: ThreadLayout = MISSING,
reason: Optional[str] = None,
Expand Down Expand Up @@ -4102,7 +4102,7 @@ async def clone(
default_thread_slowmode_delay: Optional[int] = MISSING,
default_auto_archive_duration: Optional[AnyThreadArchiveDuration] = MISSING,
available_tags: Sequence[ForumTag] = MISSING,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = MISSING,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = MISSING,
default_sort_order: Optional[ThreadSortOrder] = MISSING,
default_layout: ThreadLayout = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
Expand Down Expand Up @@ -4362,7 +4362,7 @@ async def edit(
flags: ChannelFlags = ...,
require_tag: bool = ...,
available_tags: Sequence[ForumTag] = ...,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = ...,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = ...,
default_sort_order: Optional[ThreadSortOrder] = ...,
reason: Optional[str] = ...,
) -> MediaChannel:
Expand All @@ -4384,7 +4384,7 @@ async def edit(
flags: ChannelFlags = MISSING,
require_tag: bool = MISSING,
available_tags: Sequence[ForumTag] = MISSING,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = MISSING,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = MISSING,
default_sort_order: Optional[ThreadSortOrder] = MISSING,
reason: Optional[str] = None,
**kwargs: Never,
Expand Down Expand Up @@ -4503,7 +4503,7 @@ async def clone(
default_thread_slowmode_delay: Optional[int] = MISSING,
default_auto_archive_duration: Optional[AnyThreadArchiveDuration] = MISSING,
available_tags: Sequence[ForumTag] = MISSING,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = MISSING,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = MISSING,
default_sort_order: Optional[ThreadSortOrder] = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
reason: Optional[str] = None,
Expand Down
6 changes: 3 additions & 3 deletions disnake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from .application_role_connection import ApplicationRoleConnectionMetadata
from .backoff import ExponentialBackoff
from .channel import PartialMessageable, _threaded_channel_factory
from .emoji import ApplicationEmoji, GuildEmoji
from .emoji import AnyEmoji, ApplicationEmoji
from .entitlement import Entitlement
from .enums import ApplicationCommandType, ChannelType, Event, Status
from .errors import (
Expand Down Expand Up @@ -566,7 +566,7 @@ def guilds(self) -> List[Guild]:
return self._connection.guilds

@property
def emojis(self) -> List[Union[GuildEmoji, ApplicationEmoji]]:
def emojis(self) -> List[AnyEmoji]:
"""The emojis that the connected client has.
.. note::
Expand Down Expand Up @@ -1486,7 +1486,7 @@ def get_user(self, id: int, /) -> Optional[User]:
"""
return self._connection.get_user(id)

def get_emoji(self, id: int, /) -> Optional[Union[GuildEmoji, ApplicationEmoji]]:
def get_emoji(self, id: int, /) -> Optional[AnyEmoji]:
"""Returns an emoji with the given ID.
Parameters
Expand Down
4 changes: 2 additions & 2 deletions disnake/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
if TYPE_CHECKING:
from typing_extensions import Self, TypeAlias

from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji
from .types.components import (
ActionRow as ActionRowPayload,
AnySelectMenu as AnySelectMenuPayload,
Expand Down Expand Up @@ -546,7 +546,7 @@ def __init__(
label: str,
value: str = MISSING,
description: Optional[str] = None,
emoji: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = None,
emoji: Optional[Union[str, AnyEmoji, PartialEmoji]] = None,
default: bool = False,
) -> None:
self.label = label
Expand Down
13 changes: 8 additions & 5 deletions disnake/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .user import User
from .utils import MISSING, SnowflakeList, snowflake_time

__all__ = ("BaseEmoji", "Emoji", "GuildEmoji", "ApplicationEmoji")
__all__ = ("BaseEmoji", "Emoji", "GuildEmoji", "ApplicationEmoji", "AnyEmoji")

if TYPE_CHECKING:
from datetime import datetime
Expand Down Expand Up @@ -67,8 +67,6 @@ class BaseEmoji(_EmojiTag, AssetMixin):
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`]
Expand All @@ -84,8 +82,6 @@ class BaseEmoji(_EmojiTag, AssetMixin):
"id",
"name",
"_roles",
"guild_id",
"application_id",
"user",
"available",
)
Expand Down Expand Up @@ -197,6 +193,8 @@ class GuildEmoji(BaseEmoji):
having the :attr:`~Permissions.manage_guild_expressions` permission.
"""

__slots__: Tuple[str, ...] = BaseEmoji.__slots__ + ("guild_id",)

def __init__(
self, *, guild: Union[Guild, GuildPreview], state: ConnectionState, data: EmojiPayload
) -> None:
Expand Down Expand Up @@ -365,6 +363,8 @@ class ApplicationEmoji(BaseEmoji):
The user that created the emoji.
"""

__slots__: Tuple[str, ...] = BaseEmoji.__slots__ + ("application_id",)

def __init__(self, *, application_id: int, state: ConnectionState, data: EmojiPayload) -> None:
self.application_id: int = application_id
super().__init__(state=state, data=data)
Expand Down Expand Up @@ -426,3 +426,6 @@ async def edit(
self.application_id, self.id, payload=payload
)
return self._state.store_application_emoji(self.application_id, data)


AnyEmoji = Union[GuildEmoji, ApplicationEmoji]
6 changes: 3 additions & 3 deletions disnake/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
_threaded_guild_channel_factory,
)
from .colour import Colour
from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji, Emoji
from .enums import (
AuditLogAction,
AutoModEventType,
Expand Down Expand Up @@ -1643,7 +1643,7 @@ async def create_forum_channel(
nsfw: bool = MISSING,
overwrites: Dict[Union[Role, Member], PermissionOverwrite] = MISSING,
available_tags: Optional[Sequence[ForumTag]] = None,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = None,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = None,
default_sort_order: Optional[ThreadSortOrder] = None,
default_layout: Optional[ThreadLayout] = None,
reason: Optional[str] = None,
Expand Down Expand Up @@ -1793,7 +1793,7 @@ async def create_media_channel(
nsfw: bool = MISSING,
overwrites: Dict[Union[Role, Member], PermissionOverwrite] = MISSING,
available_tags: Optional[Sequence[ForumTag]] = None,
default_reaction: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]] = None,
default_reaction: Optional[Union[str, AnyEmoji, PartialEmoji]] = None,
default_sort_order: Optional[ThreadSortOrder] = None,
reason: Optional[str] = None,
) -> MediaChannel:
Expand Down
6 changes: 3 additions & 3 deletions disnake/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from . import utils
from .components import ActionRow, MessageComponent, _component_factory
from .embeds import Embed
from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji
from .enums import ChannelType, InteractionType, MessageType, try_enum, try_enum_to_int
from .errors import HTTPException
from .file import File
Expand Down Expand Up @@ -76,7 +76,7 @@
from .ui.action_row import Components, MessageUIComponent
from .ui.view import View

EmojiInputType = Union[Emoji, ApplicationEmoji, PartialEmoji, str]
EmojiInputType = Union[AnyEmoji, PartialEmoji, str]

__all__ = (
"Attachment",
Expand All @@ -93,7 +93,7 @@ def convert_emoji_reaction(emoji: Union[EmojiInputType, Reaction]) -> str:
if isinstance(emoji, Reaction):
emoji = emoji.emoji

if isinstance(emoji, Emoji) or isinstance(emoji, ApplicationEmoji):
if isinstance(emoji, AnyEmoji):
return f"{emoji.name}:{emoji.id}"
if isinstance(emoji, PartialEmoji):
return emoji._as_reaction()
Expand Down
4 changes: 2 additions & 2 deletions disnake/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .mixins import Hashable

if TYPE_CHECKING:
from .emoji import ApplicationEmoji, Emoji, PartialEmoji
from .emoji import AnyEmoji, PartialEmoji
from .guild import Guild, GuildChannel
from .role import Role
from .types.onboarding import (
Expand Down Expand Up @@ -170,7 +170,7 @@ def __init__(self, *, guild: Guild, data: OnboardingPromptOptionPayload) -> None
else frozenset()
)

self.emoji: Optional[Union[Emoji, ApplicationEmoji, PartialEmoji, str]]
self.emoji: Optional[Union[AnyEmoji, PartialEmoji, str]]
if emoji_data := data.get("emoji"):
self.emoji = guild._state.get_reaction_emoji(emoji_data)
else:
Expand Down
4 changes: 2 additions & 2 deletions disnake/partial_emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from typing_extensions import Self

from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji
from .state import ConnectionState
from .types.activity import ActivityEmoji as ActivityEmojiPayload
from .types.emoji import Emoji as EmojiPayload, PartialEmoji as PartialEmojiPayload
Expand Down Expand Up @@ -254,7 +254,7 @@ async def read(self) -> bytes:
# (e.g. default reaction, tag emoji)
@staticmethod
def _emoji_to_name_id(
emoji: Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]]
emoji: Optional[Union[str, AnyEmoji, PartialEmoji]]
) -> Tuple[Optional[str], Optional[int]]:
if emoji is None:
return None, None
Expand Down
4 changes: 2 additions & 2 deletions disnake/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if TYPE_CHECKING:
from .abc import Snowflake
from .emoji import ApplicationEmoji, Emoji
from .emoji import AnyEmoji, Emoji
from .message import Message
from .partial_emoji import PartialEmoji
from .types.message import Reaction as ReactionPayload
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(
*,
message: Message,
data: ReactionPayload,
emoji: Optional[Union[PartialEmoji, Emoji, ApplicationEmoji, str]] = None,
emoji: Optional[Union[PartialEmoji, AnyEmoji, str]] = None,
) -> None:
self.message: Message = message
# _get_emoji_from_data won't return None
Expand Down
18 changes: 8 additions & 10 deletions disnake/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
VoiceChannel,
_guild_channel_factory,
)
from .emoji import ApplicationEmoji, Emoji, GuildEmoji
from .emoji import AnyEmoji, ApplicationEmoji, GuildEmoji
from .entitlement import Entitlement
from .enums import ApplicationCommandType, ChannelType, ComponentType, MessageType, Status, try_enum
from .flags import ApplicationFlags, Intents, MemberCacheFlags
Expand Down Expand Up @@ -296,7 +296,7 @@ def clear(
# - the weakref slot + object in user objects likely results in a small increase in memory usage
# - accesses on `_users` are slower, e.g. `__getitem__` takes ~1us with weakrefs and ~0.2us without
self._users: weakref.WeakValueDictionary[int, User] = weakref.WeakValueDictionary()
self._emojis: Dict[int, Union[GuildEmoji, ApplicationEmoji]] = {}
self._emojis: Dict[int, AnyEmoji] = {}
self._stickers: Dict[int, GuildSticker] = {}
self._guilds: Dict[int, Guild] = {}

Expand Down Expand Up @@ -518,18 +518,18 @@ def _get_guild_command_named(
return cmd

@property
def emojis(self) -> List[Union[GuildEmoji, ApplicationEmoji]]:
def emojis(self) -> List[AnyEmoji]:
return list(self._emojis.values())

@property
def stickers(self) -> List[GuildSticker]:
return list(self._stickers.values())

def get_emoji(self, emoji_id: Optional[int]) -> Optional[Union[GuildEmoji, ApplicationEmoji]]:
def get_emoji(self, emoji_id: Optional[int]) -> Optional[AnyEmoji]:
# the keys of self._emojis are ints
return self._emojis.get(emoji_id) # type: ignore

def _remove_emoji(self, emoji: Union[GuildEmoji, ApplicationEmoji]) -> None:
def _remove_emoji(self, emoji: AnyEmoji) -> None:
self._emojis.pop(emoji.id, None)

def get_sticker(self, sticker_id: Optional[int]) -> Optional[GuildSticker]:
Expand Down Expand Up @@ -1942,7 +1942,7 @@ def _get_reaction_user(

def _get_emoji_from_data(
self, data: PartialEmojiPayload
) -> Optional[Union[str, Emoji, ApplicationEmoji, PartialEmoji]]:
) -> Optional[Union[str, AnyEmoji, PartialEmoji]]:
"""Convert partial emoji data to proper emoji.
Returns unicode emojis as strings.
Expand Down Expand Up @@ -1973,7 +1973,7 @@ def _get_emoji_from_fields(
name: Optional[str],
id: Optional[int],
animated: Optional[bool] = False,
) -> Optional[Union[Emoji, ApplicationEmoji, PartialEmoji]]:
) -> Optional[Union[AnyEmoji, PartialEmoji]]:
"""Convert partial emoji fields to proper emoji, if possible.
If both `id` and `name` are nullish, returns `None`.
Expand All @@ -1999,9 +1999,7 @@ def _get_emoji_from_fields(
animated=animated or False,
)

def _upgrade_partial_emoji(
self, emoji: PartialEmoji
) -> Union[Emoji, ApplicationEmoji, PartialEmoji, str]:
def _upgrade_partial_emoji(self, emoji: PartialEmoji) -> Union[AnyEmoji, PartialEmoji, str]:
emoji_id = emoji.id
if not emoji_id:
return emoji.name
Expand Down
Loading

0 comments on commit 1ca8f16

Please sign in to comment.