Browse Source

Implement Emoji/Sticker.fetch_guild (and run black)

pull/10109/head
dolfies 3 years ago
parent
commit
5edcd7d64b
  1. 23
      discord/emoji.py
  2. 15
      discord/gateway.py
  3. 4
      discord/http.py
  4. 2
      discord/message.py
  5. 32
      discord/partial_emoji.py
  6. 4
      discord/state.py
  7. 55
      discord/sticker.py

23
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)

15
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,

4
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))

2
discord/message.py

@ -2030,4 +2030,4 @@ class Message(PartialMessage, Hashable):
applications=applications,
application=application,
)
return iterator.iterate()
return iterator.iterate()

32
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)

4
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

55
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)

Loading…
Cancel
Save