From 5edcd7d64b3224cce182bc8497ed9c2fafebaabb Mon Sep 17 00:00:00 2001 From: dolfies Date: Wed, 23 Mar 2022 15:50:31 -0400 Subject: [PATCH] Implement Emoji/Sticker.fetch_guild (and run black) --- discord/emoji.py | 23 +++++++++++++++++ discord/gateway.py | 15 +++++++++-- discord/http.py | 4 ++- discord/message.py | 2 +- discord/partial_emoji.py | 32 +++++++++++++++++++++++ discord/state.py | 4 ++- discord/sticker.py | 55 ++++++++++++++++++++++++++++++++++++---- 7 files changed, 125 insertions(+), 10 deletions(-) diff --git a/discord/emoji.py b/discord/emoji.py index 8fa663e6f..d6db70657 100644 --- a/discord/emoji.py +++ b/discord/emoji.py @@ -257,3 +257,26 @@ class Emoji(_EmojiTag, AssetMixin): data = await self._state.http.edit_custom_emoji(self.guild_id, self.id, payload=payload, reason=reason) return Emoji(guild=self.guild, data=data, state=self._state) # type: ignore - if guild is None, the http request would have failed + + async def fetch_guild(self): + """|coro| + + Retrieves the guild this emoji belongs to. + + Raises + ------ + NotFound + The guild this emoji belongs to is not public. + HTTPException + An error occurred while fetching the guild. + + Returns + ------- + :class:`Guild` + The guild this emoji belongs to. + """ + from .guild import Guild # Circular import + + state = self._state + data = await state.http.get_emoji_guild(self.id) + return Guild(state=state, data=data) diff --git a/discord/gateway.py b/discord/gateway.py index b550cd5b3..7093bef8f 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -668,14 +668,25 @@ class DiscordWebSocket: if status == 'idle': since = int(time.time() * 1000) - payload = {'op': self.PRESENCE, 'd': {'activities': activities_data, 'afk': afk, 'since': since, 'status': str(status)}} + payload = { + 'op': self.PRESENCE, + 'd': {'activities': activities_data, 'afk': afk, 'since': since, 'status': str(status)}, + } sent = utils._to_json(payload) _log.debug('Sending "%s" to change presence.', sent) await self.send(sent) async def request_lazy_guild( - self, guild_id: Snowflake, *, typing: Optional[bool] = None, threads: Optional[bool] = None, activities: Optional[bool] = None, members: Optional[List[Snowflake]]=None, channels: Optional[Dict[Snowflake, List[List[int]]]]=None, thread_member_lists: Optional[List[Snowflake]]=None + self, + guild_id: Snowflake, + *, + typing: Optional[bool] = None, + threads: Optional[bool] = None, + activities: Optional[bool] = None, + members: Optional[List[Snowflake]] = None, + channels: Optional[Dict[Snowflake, List[List[int]]]] = None, + thread_member_lists: Optional[List[Snowflake]] = None, ): payload = { 'op': self.GUILD_SUBSCRIBE, diff --git a/discord/http.py b/discord/http.py index 708ebebf9..b813c4495 100644 --- a/discord/http.py +++ b/discord/http.py @@ -1275,7 +1275,9 @@ class HTTPClient: ) payload = {k: v for k, v in payload.items() if k in valid_keys} - return self.request(Route('PATCH', '/guilds/{guild_id}/templates/{code}', guild_id=guild_id, code=code), json=payload) + return self.request( + Route('PATCH', '/guilds/{guild_id}/templates/{code}', guild_id=guild_id, code=code), json=payload + ) def delete_template(self, guild_id: Snowflake, code: str) -> Response[None]: return self.request(Route('DELETE', '/guilds/{guild_id}/templates/{code}', guild_id=guild_id, code=code)) diff --git a/discord/message.py b/discord/message.py index be2819a12..909ffa279 100644 --- a/discord/message.py +++ b/discord/message.py @@ -2030,4 +2030,4 @@ class Message(PartialMessage, Hashable): applications=applications, application=application, ) - return iterator.iterate() \ No newline at end of file + return iterator.iterate() diff --git a/discord/partial_emoji.py b/discord/partial_emoji.py index cf65efb3b..9bf9a6e7f 100644 --- a/discord/partial_emoji.py +++ b/discord/partial_emoji.py @@ -237,3 +237,35 @@ class PartialEmoji(_EmojiTag, AssetMixin): raise ValueError('PartialEmoji is not a custom emoji') return await super().read() + + async def fetch_guild(self): + """|coro| + + Retrieves the guild this emoji belongs to. + + Raises + ------ + NotFound + The guild this emoji belongs to is not public. + HTTPException + An error occurred while fetching the guild. + ValueError + The emoji is not custom. + TypeError + The emoji does not have state available. + + Returns + ------- + :class:`Guild` + The guild this emoji belongs to. + """ + from .guild import Guild # Circular import + + if self.id is None: + raise ValueError('PartialEmoji is not a custom emoji') + if self._state is None: + raise TypeError('PartialEmoji does not have state available') + + state = self._state + data = await state.http.get_emoji_guild(self.id) + return Guild(state=state, data=data) diff --git a/discord/state.py b/discord/state.py index 0ea2add33..036fa8860 100644 --- a/discord/state.py +++ b/discord/state.py @@ -1712,7 +1712,9 @@ class ConnectionState: request.start() @overload - async def chunk_guild(self, guild: Guild, *, wait: Literal[True] = ..., channels: List[abcSnowflake] = ...) -> Optional[List[Member]]: + async def chunk_guild( + self, guild: Guild, *, wait: Literal[True] = ..., channels: List[abcSnowflake] = ... + ) -> Optional[List[Member]]: ... @overload diff --git a/discord/sticker.py b/discord/sticker.py index d4fe2c207..3face3e90 100644 --- a/discord/sticker.py +++ b/discord/sticker.py @@ -51,7 +51,6 @@ if TYPE_CHECKING: Sticker as StickerPayload, StandardSticker as StandardStickerPayload, GuildSticker as GuildStickerPayload, - ListPremiumStickerPacks as ListPremiumStickerPacksPayload, EditGuildSticker, ) @@ -227,10 +226,33 @@ class StickerItem(_StickerTag): Union[:class:`StandardSticker`, :class:`GuildSticker`] The retrieved sticker. """ - data: StickerPayload = await self._state.http.get_sticker(self.id) - cls, _ = _sticker_factory(data['type']) # type: ignore + data = await self._state.http.get_sticker(self.id) + cls, _ = _sticker_factory(data['type']) return cls(state=self._state, data=data) + async def fetch_guild(self): + """|coro| + + Retrieves the guild this sticker belongs to. + + Raises + ------ + NotFound + The guild this sticker belongs to is not public. + HTTPException + An error occurred while fetching the guild. + + Returns + ------- + :class:`Guild` + The guild this emoji belongs to. + """ + from .guild import Guild # Circular import + + state = self._state + data = await state.http.get_sticker_guild(self.id) + return Guild(state=state, data=data) + class Sticker(_StickerTag): """Represents a sticker. @@ -362,7 +384,7 @@ class StandardSticker(Sticker): :class:`StickerPack` The retrieved sticker pack. """ - data: ListPremiumStickerPacksPayload = await self._state.http.list_premium_sticker_packs() + data = await self._state.http.list_premium_sticker_packs() packs = data['sticker_packs'] pack = find(lambda d: int(d['id']) == self.pack_id, packs) @@ -487,7 +509,7 @@ class GuildSticker(Sticker): payload['tags'] = emoji - data: GuildStickerPayload = await self._state.http.modify_guild_sticker(self.guild_id, self.id, payload, reason) + data = await self._state.http.modify_guild_sticker(self.guild_id, self.id, payload, reason) return GuildSticker(state=self._state, data=data) async def delete(self, *, reason: Optional[str] = None) -> None: @@ -512,6 +534,29 @@ class GuildSticker(Sticker): """ await self._state.http.delete_guild_sticker(self.guild_id, self.id, reason) + async def fetch_guild(self): + """|coro| + + Retrieves the guild this sticker belongs to. + + Raises + ------ + NotFound + The guild this sticker belongs to is not public. + HTTPException + An error occurred while fetching the guild. + + Returns + ------- + :class:`Guild` + The guild this emoji belongs to. + """ + from .guild import Guild # Circular import + + state = self._state + data = await state.http.get_sticker_guild(self.id) + return Guild(state=state, data=data) + def _sticker_factory(sticker_type: Literal[1, 2]) -> Tuple[Type[Union[StandardSticker, GuildSticker, Sticker]], StickerType]: value = try_enum(StickerType, sticker_type)