From 83bd54e8060795b8d2c27e2f70eed139039eb8a8 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Wed, 8 Dec 2021 20:40:35 -0500 Subject: [PATCH 01/34] :sparkles: created properties --- pincer/utils/api_object.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pincer/utils/api_object.py b/pincer/utils/api_object.py index e165b11d..dfcb59c2 100644 --- a/pincer/utils/api_object.py +++ b/pincer/utils/api_object.py @@ -19,7 +19,6 @@ if TYPE_CHECKING: from ..client import Client - from ..core.http import HTTPClient T = TypeVar("T") @@ -32,8 +31,15 @@ def _asdict_ignore_none(obj: Generic[T]) -> Union[Tuple, Dict, T]: all values that are None Modification of _asdict_inner from dataclasses - :param obj: - Dataclass obj + Parameters + ---------- + + obj: Generic[T] + The object to convert + + Returns + ------- + A dict without None values """ if _is_dataclass_instance(obj): @@ -66,6 +72,18 @@ def _asdict_ignore_none(obj: Generic[T]) -> Union[Tuple, Dict, T]: return copy.deepcopy(obj) +def get_guild(self): + "Return a guild" + return self._client.guilds[self.guild_id] + + +def get_channel(self): + "Return a channel" + return next( + filter(lambda c: c.id == self.channel_id, self.guild.channels) + ) + + class HTTPMeta(type): __meta_items: List[str] = ["_client", "_http"] __ori_annotations: Dict[str, type] = {} @@ -79,7 +97,7 @@ def __new__(mcs, name, base, mapping): if mapping.get("__annotations__") and \ (value := mapping["__annotations__"].get(key)): # We want to keep the type annotations of the objects - # tho, so lets statically store them so we can read + # tho, so lets statically store them, so we can read # them later. HTTPMeta.__ori_annotations.update({key: value}) del mapping["__annotations__"][key] @@ -116,7 +134,7 @@ def __get_types(self, attr: str, arg_type: type) -> Tuple[type]: Returns ------- Tuple[:class:`type`] - A collection of type annotation(s). Will most of the times + A collection of type annotation(s). Will most of the time consist of 1 item. Raises From fb6a3efc9e1400fd83e57d920a8369ed6b81935e Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Wed, 8 Dec 2021 20:41:30 -0500 Subject: [PATCH 02/34] :sparkles: added properties to UserMessage --- pincer/objects/message/user_message.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pincer/objects/message/user_message.py b/pincer/objects/message/user_message.py index 9bb503c1..c1ab17f3 100644 --- a/pincer/objects/message/user_message.py +++ b/pincer/objects/message/user_message.py @@ -19,7 +19,7 @@ from ..guild.role import Role from ..user.user import User from ..._config import GatewayConfig -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, get_guild, get_channel from ...utils.conversion import construct_client_dict from ...utils.snowflake import Snowflake from ...utils.types import MISSING @@ -353,6 +353,14 @@ class UserMessage(APIObject): def __str__(self): return self.content + @property + def guild(self): + return get_guild(self) + + @property + def channel(self): + return get_channel(self) + async def get_most_recent(self): """|coro| From c85071c3116897f5f7def85a61e5305d7c695e5c Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Wed, 8 Dec 2021 21:37:34 -0500 Subject: [PATCH 03/34] :construction: started updates --- pincer/client.py | 14 +++++++------- pincer/middleware/channel_create.py | 13 ++++++++----- pincer/middleware/channel_delete.py | 9 ++++----- pincer/middleware/channel_pins_update.py | 5 ++++- pincer/middleware/channel_update.py | 8 +------- pincer/middleware/guild_create.py | 3 +++ pincer/middleware/guild_delete.py | 8 ++++---- pincer/middleware/guild_emojis_update.py | 10 +++++++--- pincer/middleware/guild_member_add.py | 2 +- pincer/middleware/guild_member_remove.py | 2 +- pincer/middleware/guild_member_update.py | 2 +- pincer/middleware/guild_role_create.py | 8 +++++--- pincer/middleware/guild_role_delete.py | 10 +++++++--- pincer/middleware/guild_role_update.py | 12 ++++++++---- pincer/middleware/guild_update.py | 15 ++++----------- pincer/objects/events/channel.py | 1 - 16 files changed, 65 insertions(+), 57 deletions(-) diff --git a/pincer/client.py b/pincer/client.py index e2aa672e..7bd572f4 100644 --- a/pincer/client.py +++ b/pincer/client.py @@ -189,10 +189,11 @@ def __init__( self.http = HTTPClient(token) self.throttler = throttler self.event_mgr = EventMgr() - # TODO: Document guild prop - # The guild value is only registered if the GUILD_MEMBERS - # intent is enabled. + + # The guild and channel value is only registered if the GUILD intent. self.guilds: Dict[Snowflake, Optional[Guild]] = {} + self.channels: Dict[Snowflake, Optional[Channel]] = {} + ChatCommandHandler.managers[self.__module__] = self @property @@ -497,7 +498,7 @@ async def handle_middleware( raise RuntimeError(f"Middleware `{key}` has not been registered.") if next_call.startswith("on_"): - return next_call, ret_object + return (next_call, ret_object) return await self.handle_middleware( payload, next_call, *arguments, **params @@ -565,7 +566,7 @@ async def event_handler(self, _, payload: GatewayDispatch): ---------- _ : Socket param, but this isn't required for this handler. So - it's just a filler parameter, doesn't matter what is passed. + its just a filler parameter, doesn't matter what is passed. payload : :class:`~pincer.core.dispatch.GatewayDispatch` The payload sent from the Discord gateway, this contains the required data for the client to know what event it is and @@ -582,7 +583,7 @@ async def payload_event_handler(self, _, payload: GatewayDispatch): ---------- _ : Socket param, but this isn't required for this handler. So - it's just a filler parameter, doesn't matter what is passed. + its just a filler parameter, doesn't matter what is passed. payload : :class:`~pincer.core.dispatch.GatewayDispatch` The payload sent from the Discord gateway, this contains the required data for the client to know what event it is and @@ -857,5 +858,4 @@ async def get_webhook( """ return await Webhook.from_id(self, id, token) - Bot = Client diff --git a/pincer/middleware/channel_create.py b/pincer/middleware/channel_create.py index 78cf70c8..af50c3a2 100644 --- a/pincer/middleware/channel_create.py +++ b/pincer/middleware/channel_create.py @@ -14,7 +14,7 @@ from ..core.dispatch import GatewayDispatch -def channel_create_middleware( +async def channel_create_middleware( self, payload: GatewayDispatch ) -> Tuple[str, Channel]: @@ -32,10 +32,13 @@ def channel_create_middleware( Tuple[:class:`str`, List[:class:`~pincer.objects.guild.channel.Channel`]] ``on_channel_creation`` and a channel. """ - return ( - "on_channel_creation", - Channel.from_dict(construct_client_dict(self, payload.data)) - ) + + channel: Channel = Channel.from_dict(construct_client_dict(self, payload.data)) + self.guilds[channel.guild_id].channels.append(channel) + + self.channels[channel.id] = channel + + return "on_channel_creation", channel def export(): diff --git a/pincer/middleware/channel_delete.py b/pincer/middleware/channel_delete.py index 6bdc3528..f3ce5f34 100644 --- a/pincer/middleware/channel_delete.py +++ b/pincer/middleware/channel_delete.py @@ -26,11 +26,10 @@ async def channel_delete_middleware(self, payload: GatewayDispatch): channel = Channel.from_dict(construct_client_dict(self, payload.data)) - if channel.guild_id in self.guilds: - guild = self.guilds[channel.guild_id] - old = filter(lambda c: c.id == channel.id, guild.channels) - if old: - guild.channels.remove(old) + guild = self.guilds[channel.guild_id] + guild.channels = list(filter(lambda c: c.id != channel.id, guild.channels)) + + self.channels.pop(channel.id) return "on_channel_delete", channel diff --git a/pincer/middleware/channel_pins_update.py b/pincer/middleware/channel_pins_update.py index b14e7d28..7c8cb935 100644 --- a/pincer/middleware/channel_pins_update.py +++ b/pincer/middleware/channel_pins_update.py @@ -23,9 +23,12 @@ async def channel_pins_update_middleware(self, payload: GatewayDispatch): ``on_channel_pins_update`` and a ``Channel`` """ + event = ChannelPinsUpdateEvent.from_dict(payload.data) + self.channels[event.channel_id].last_pin_timestamp = event.last_pin_timestamp + return ( "on_channel_pins_update", - ChannelPinsUpdateEvent.from_dict(payload.data) + event ) diff --git a/pincer/middleware/channel_update.py b/pincer/middleware/channel_update.py index 7f8ea220..b822bcbe 100644 --- a/pincer/middleware/channel_update.py +++ b/pincer/middleware/channel_update.py @@ -25,13 +25,7 @@ async def channel_update_middleware(self, payload: GatewayDispatch): """ channel = Channel.from_dict(construct_client_dict(self, payload.data)) - - if channel.guild_id in self.guilds.keys(): - guild = self.guilds[channel.guild_id] - old = filter(lambda c: c.id == channel.id, guild.channels) - if old: - guild.channels.remove(old) - guild.channels.append(channel) + self.channels[channel.id] = channel return "on_channel_update", channel diff --git a/pincer/middleware/guild_create.py b/pincer/middleware/guild_create.py index 7e944e40..e037346d 100644 --- a/pincer/middleware/guild_create.py +++ b/pincer/middleware/guild_create.py @@ -33,6 +33,9 @@ async def guild_create_middleware(self, payload: GatewayDispatch): """ guild = Guild.from_dict(construct_client_dict(self, payload.data)) self.guilds[guild.id] = guild + for channel in guild.channels: + self.channels[channel.id] = channel + return "on_guild_create", guild diff --git a/pincer/middleware/guild_delete.py b/pincer/middleware/guild_delete.py index 831173cc..87c4de7c 100644 --- a/pincer/middleware/guild_delete.py +++ b/pincer/middleware/guild_delete.py @@ -23,15 +23,15 @@ async def guild_delete_middleware(self, payload: GatewayDispatch): Tuple[:class:`str`, :class:`~pincer.objects.guild.guild.UnavailableGuild`] ``on_guild_delete`` and an ``UnavailableGuild`` """ - # TODO: Fix docs on line 23 (three lines above) - # http://docs.pincer.dev/pincer.middleware#pincer.middleware.guild_delete.guild_delete_middleware guild = UnavailableGuild.from_dict( construct_client_dict(self, payload.data) ) - if guild.id in self.guilds.key(): - self.guilds.pop(guild.id) + self.guilds.pop(guild.id, None) + + for channel in self.guild.channels: + self.channels.pop(channel.id) return "on_guild_delete", guild diff --git a/pincer/middleware/guild_emojis_update.py b/pincer/middleware/guild_emojis_update.py index 8205476b..39d54094 100644 --- a/pincer/middleware/guild_emojis_update.py +++ b/pincer/middleware/guild_emojis_update.py @@ -25,11 +25,15 @@ async def guild_emojis_update_middleware(self, payload: GatewayDispatch): ``on_guild_emoji_update`` and a ``GuildEmojisUpdateEvent`` """ # noqa: E501 + event = GuildEmojisUpdateEvent.from_dict( + construct_client_dict(self, payload.data) + ) + + self.guild[event.guild_id] = event.emojis + return ( "on_guild_emojis_update", - GuildEmojisUpdateEvent.from_dict( - construct_client_dict(self, payload.data) - ) + event ) diff --git a/pincer/middleware/guild_member_add.py b/pincer/middleware/guild_member_add.py index 91196649..25743f8a 100644 --- a/pincer/middleware/guild_member_add.py +++ b/pincer/middleware/guild_member_add.py @@ -26,7 +26,7 @@ async def guild_member_add_middleware(self, payload: GatewayDispatch): ------- Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberAddEvent`] ``on_guild_member_add`` and a ``GuildMemberAddEvent`` - """ # noqa: E501 + """ return ( "on_guild_member_add", diff --git a/pincer/middleware/guild_member_remove.py b/pincer/middleware/guild_member_remove.py index e392bd25..2991d6b0 100644 --- a/pincer/middleware/guild_member_remove.py +++ b/pincer/middleware/guild_member_remove.py @@ -25,7 +25,7 @@ async def guild_member_remove_middleware(self, payload: GatewayDispatch): ------- Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberRemoveEvent`] ``on_guild_member_remove`` and a ``GuildMemberRemoveEvent`` - """ # noqa: E501 + """ return ( "on_guild_member_remove", diff --git a/pincer/middleware/guild_member_update.py b/pincer/middleware/guild_member_update.py index c9cba145..44f5b4cc 100644 --- a/pincer/middleware/guild_member_update.py +++ b/pincer/middleware/guild_member_update.py @@ -26,7 +26,7 @@ async def guild_member_update_middleware(self, payload: GatewayDispatch): ------- Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberUpdateEvent`] ``on_guild_member_update`` and a ``GuildMemberUpdateEvent`` - """ # noqa: E501 + """ return ( "on_guild_member_update", diff --git a/pincer/middleware/guild_role_create.py b/pincer/middleware/guild_role_create.py index e046a5d1..083f8cb9 100644 --- a/pincer/middleware/guild_role_create.py +++ b/pincer/middleware/guild_role_create.py @@ -25,11 +25,13 @@ async def guild_role_create_middleware(self, payload: GatewayDispatch): ``on_guild_role_create`` and a ``GuildRoleCreateEvent`` """ # noqa: E501 + event = GuildRoleCreateEvent.from_dict(construct_client_dict(self, payload.data)) + + self.guilds[event.guild_id].roles.append(event.role) + return ( "on_guild_role_create", - GuildRoleCreateEvent.from_dict( - construct_client_dict(self, payload.data) - ) + event ) diff --git a/pincer/middleware/guild_role_delete.py b/pincer/middleware/guild_role_delete.py index 304d6af7..450bb483 100644 --- a/pincer/middleware/guild_role_delete.py +++ b/pincer/middleware/guild_role_delete.py @@ -25,11 +25,15 @@ async def guild_role_delete_middleware(self, payload: GatewayDispatch): ``on_guild_role_delete`` and a ``GuildRoleDeleteEvent`` """ # noqa: E501 + event = GuildRoleDeleteEvent.from_dict(construct_client_dict(self, payload.data)) + + self.guilds[event.guild_id].roles = list(filter( + lambda role: role.id != event.role_id, self.guilds[event.guild_id].roles + )) + return ( "on_guild_role_delete", - GuildRoleDeleteEvent.from_dict( - construct_client_dict(self, payload.data) - ), + event ) diff --git a/pincer/middleware/guild_role_update.py b/pincer/middleware/guild_role_update.py index 088422d1..1b20873a 100644 --- a/pincer/middleware/guild_role_update.py +++ b/pincer/middleware/guild_role_update.py @@ -23,13 +23,17 @@ async def guild_role_update_middleware(self, payload: GatewayDispatch): ------- Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildRoleUpdateEvent`] ``on_guild_role_update`` and a ``GuildRoleUpdateEvent`` - """ # noqa: E501 + """ + + event = GuildRoleUpdateEvent.from_dict(construct_client_dict(self, payload.data)) + self.guilds[event.guild_id].roles = [ + role if role.id != event.role.id else event.role + for role in self.guilds[event.guild_id].roles + ] return ( "on_guild_role_update", - GuildRoleUpdateEvent.from_dict( - construct_client_dict(self, payload.data) - ), + event ) diff --git a/pincer/middleware/guild_update.py b/pincer/middleware/guild_update.py index ece4e1bb..4f26029c 100644 --- a/pincer/middleware/guild_update.py +++ b/pincer/middleware/guild_update.py @@ -25,19 +25,12 @@ async def guild_update_middleware(self, payload: GatewayDispatch): ``on_guild_Update`` and an ``Guild`` """ - channel_list = payload.data.pop("channels", []) - - channels: List[Channel] = [ - Channel.from_dict(construct_client_dict(self, channel)) - for channel in channel_list - ] - - guild = Guild.from_dict(construct_client_dict( - self, - {"channels": channels, **payload.data} - )) + guild = Guild.from_dict(construct_client_dict(self, payload.data)) self.guild[guild.id] = guild + for channel in guild.channels: + self.channels[channel.id] = channel + return "on_guild_update", guild diff --git a/pincer/objects/events/channel.py b/pincer/objects/events/channel.py index 70203f24..a3569135 100644 --- a/pincer/objects/events/channel.py +++ b/pincer/objects/events/channel.py @@ -32,4 +32,3 @@ class ChannelPinsUpdateEvent(APIObject): guild_id: APINullable[Snowflake] = MISSING last_pin_timestamp: APINullable[Timestamp] = MISSING - From 424c365bfd449d064da5711086239c07dc7cda2f Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Wed, 8 Dec 2021 21:46:59 -0500 Subject: [PATCH 04/34] :sparkles: sticker upadate middleware --- pincer/middleware/guild_stickers_update.py | 10 +++++++--- pincer/utils/__init__.py | 3 ++- pincer/utils/replace.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 pincer/utils/replace.py diff --git a/pincer/middleware/guild_stickers_update.py b/pincer/middleware/guild_stickers_update.py index f7941e4b..15a63016 100644 --- a/pincer/middleware/guild_stickers_update.py +++ b/pincer/middleware/guild_stickers_update.py @@ -25,11 +25,15 @@ async def guild_stickers_update_middleware(self, payload: GatewayDispatch): ``on_guild_sticker_update`` and a ``GuildStickersUpdateEvent`` """ # noqa: E501 + event = GuildStickersUpdateEvent.from_dict( + construct_client_dict(self, payload.data) + ) + + self.guilds[event.guild_id].stickers = event.stickers + return ( "on_guild_stickers_update", - GuildStickersUpdateEvent.from_dict( - construct_client_dict(self, payload.data) - ) + event ) diff --git a/pincer/utils/__init__.py b/pincer/utils/__init__.py index 1735a1da..5d263c0d 100644 --- a/pincer/utils/__init__.py +++ b/pincer/utils/__init__.py @@ -5,6 +5,7 @@ from .color import Color from .conversion import convert from .event_mgr import EventMgr +from .replace import replace from .directory import chdir from .extraction import get_index from .insertion import should_pass_cls, should_pass_ctx @@ -23,5 +24,5 @@ "EventMgr", "HTTPMeta", "MISSING", "MissingType", "Snowflake", "Task", "TaskScheduler", "Timestamp", "chdir", "choice_value_types", "convert", "get_index", "get_params", "get_signature_and_params", "should_pass_cls", - "should_pass_ctx" + "should_pass_ctx", "replace" ) diff --git a/pincer/utils/replace.py b/pincer/utils/replace.py new file mode 100644 index 00000000..dea1f4ce --- /dev/null +++ b/pincer/utils/replace.py @@ -0,0 +1,10 @@ +from typing import Any, Callable, Iterable, List, T + + +def replace( + func: Callable[[Any], bool], iter: Iterable[T], new_item: T +) -> List[T]: + return [ + item if func(item) else new_item + for item in iter + ] From 3d3356d86e9fed48705269c1c0034c3abdb6f9b9 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Thu, 9 Dec 2021 22:58:24 -0500 Subject: [PATCH 05/34] :bug: added construct client dict to utils __init__.py --- pincer/middleware/channel_create.py | 4 ++-- pincer/utils/__init__.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pincer/middleware/channel_create.py b/pincer/middleware/channel_create.py index af50c3a2..cec670f9 100644 --- a/pincer/middleware/channel_create.py +++ b/pincer/middleware/channel_create.py @@ -7,10 +7,10 @@ from typing import TYPE_CHECKING from ..objects.guild.channel import Channel -from ..utils.conversion import construct_client_dict +from ..utils import construct_client_dict, replace if TYPE_CHECKING: - from typing import List, Tuple + from typing import Tuple from ..core.dispatch import GatewayDispatch diff --git a/pincer/utils/__init__.py b/pincer/utils/__init__.py index 5d263c0d..8b47e444 100644 --- a/pincer/utils/__init__.py +++ b/pincer/utils/__init__.py @@ -3,7 +3,7 @@ from .api_object import APIObject, HTTPMeta from .color import Color -from .conversion import convert +from .conversion import convert, construct_client_dict from .event_mgr import EventMgr from .replace import replace from .directory import chdir @@ -24,5 +24,5 @@ "EventMgr", "HTTPMeta", "MISSING", "MissingType", "Snowflake", "Task", "TaskScheduler", "Timestamp", "chdir", "choice_value_types", "convert", "get_index", "get_params", "get_signature_and_params", "should_pass_cls", - "should_pass_ctx", "replace" + "should_pass_ctx", "replace", "construct_client_dict" ) From bc4417c58cb46bbdc810839b801a3da069ff9f60 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Thu, 9 Dec 2021 23:10:44 -0500 Subject: [PATCH 06/34] :bug: prevented middleware from raising exception if guild isn't registered --- pincer/middleware/channel_create.py | 2 +- pincer/middleware/channel_delete.py | 7 ++++--- pincer/middleware/channel_update.py | 13 +++++++++++-- pincer/middleware/guild_delete.py | 2 +- pincer/middleware/guild_emojis_update.py | 4 +++- pincer/middleware/guild_role_create.py | 4 +++- pincer/middleware/guild_role_delete.py | 9 ++++++--- pincer/middleware/guild_role_update.py | 12 ++++++++---- pincer/middleware/guild_stickers_update.py | 5 ++++- 9 files changed, 41 insertions(+), 17 deletions(-) diff --git a/pincer/middleware/channel_create.py b/pincer/middleware/channel_create.py index cec670f9..c6a5a2ba 100644 --- a/pincer/middleware/channel_create.py +++ b/pincer/middleware/channel_create.py @@ -7,7 +7,7 @@ from typing import TYPE_CHECKING from ..objects.guild.channel import Channel -from ..utils import construct_client_dict, replace +from ..utils.conversion import construct_client_dict if TYPE_CHECKING: from typing import Tuple diff --git a/pincer/middleware/channel_delete.py b/pincer/middleware/channel_delete.py index f3ce5f34..a46f5f02 100644 --- a/pincer/middleware/channel_delete.py +++ b/pincer/middleware/channel_delete.py @@ -26,10 +26,11 @@ async def channel_delete_middleware(self, payload: GatewayDispatch): channel = Channel.from_dict(construct_client_dict(self, payload.data)) - guild = self.guilds[channel.guild_id] - guild.channels = list(filter(lambda c: c.id != channel.id, guild.channels)) + guild = self.guilds.get(channel.guild_id) + if guild: + guild.channels = list(filter(lambda c: c.id != channel.id, guild.channels)) - self.channels.pop(channel.id) + self.channels.pop(channel.id, None) return "on_channel_delete", channel diff --git a/pincer/middleware/channel_update.py b/pincer/middleware/channel_update.py index b822bcbe..9538f571 100644 --- a/pincer/middleware/channel_update.py +++ b/pincer/middleware/channel_update.py @@ -5,7 +5,7 @@ from ..core.dispatch import GatewayDispatch from ..objects import Channel -from ..utils.conversion import construct_client_dict +from ..utils import construct_client_dict, replace async def channel_update_middleware(self, payload: GatewayDispatch): @@ -25,7 +25,16 @@ async def channel_update_middleware(self, payload: GatewayDispatch): """ channel = Channel.from_dict(construct_client_dict(self, payload.data)) - self.channels[channel.id] = channel + + guild = self.guilds.get(channel.guild_id) + + if guild: + guild.channels = replace( + lambda _channel: _channel.id == channel.id, + self.guilds[channel.guild_id].channels, + channel + ) + self.channels[channel.id] = channel return "on_channel_update", channel diff --git a/pincer/middleware/guild_delete.py b/pincer/middleware/guild_delete.py index 87c4de7c..e101501d 100644 --- a/pincer/middleware/guild_delete.py +++ b/pincer/middleware/guild_delete.py @@ -31,7 +31,7 @@ async def guild_delete_middleware(self, payload: GatewayDispatch): self.guilds.pop(guild.id, None) for channel in self.guild.channels: - self.channels.pop(channel.id) + self.channels.pop(channel.id, None) return "on_guild_delete", guild diff --git a/pincer/middleware/guild_emojis_update.py b/pincer/middleware/guild_emojis_update.py index 39d54094..c5a8e35e 100644 --- a/pincer/middleware/guild_emojis_update.py +++ b/pincer/middleware/guild_emojis_update.py @@ -29,7 +29,9 @@ async def guild_emojis_update_middleware(self, payload: GatewayDispatch): construct_client_dict(self, payload.data) ) - self.guild[event.guild_id] = event.emojis + guild = self.guild.get(event.guild_id) + if guild: + guild.emojis = event.emojis return ( "on_guild_emojis_update", diff --git a/pincer/middleware/guild_role_create.py b/pincer/middleware/guild_role_create.py index 083f8cb9..b243e748 100644 --- a/pincer/middleware/guild_role_create.py +++ b/pincer/middleware/guild_role_create.py @@ -27,7 +27,9 @@ async def guild_role_create_middleware(self, payload: GatewayDispatch): event = GuildRoleCreateEvent.from_dict(construct_client_dict(self, payload.data)) - self.guilds[event.guild_id].roles.append(event.role) + guild = self.guilds.get(event.guild_id) + if guild: + guild.roles.append(event.role) return ( "on_guild_role_create", diff --git a/pincer/middleware/guild_role_delete.py b/pincer/middleware/guild_role_delete.py index 450bb483..730cdf3c 100644 --- a/pincer/middleware/guild_role_delete.py +++ b/pincer/middleware/guild_role_delete.py @@ -27,9 +27,12 @@ async def guild_role_delete_middleware(self, payload: GatewayDispatch): event = GuildRoleDeleteEvent.from_dict(construct_client_dict(self, payload.data)) - self.guilds[event.guild_id].roles = list(filter( - lambda role: role.id != event.role_id, self.guilds[event.guild_id].roles - )) + guild = self.guilds.get(event.guild_id) + + if guild: + guild.roles = list(filter( + lambda role: role.id != event.role_id, self.guilds[event.guild_id].roles + )) return ( "on_guild_role_delete", diff --git a/pincer/middleware/guild_role_update.py b/pincer/middleware/guild_role_update.py index 1b20873a..00bb7e8c 100644 --- a/pincer/middleware/guild_role_update.py +++ b/pincer/middleware/guild_role_update.py @@ -26,10 +26,14 @@ async def guild_role_update_middleware(self, payload: GatewayDispatch): """ event = GuildRoleUpdateEvent.from_dict(construct_client_dict(self, payload.data)) - self.guilds[event.guild_id].roles = [ - role if role.id != event.role.id else event.role - for role in self.guilds[event.guild_id].roles - ] + + guild = self.guilds.get(event.guild_id) + + if guild: + guild.roles = [ + role if role.id != event.role.id else event.role + for role in guild.roles + ] return ( "on_guild_role_update", diff --git a/pincer/middleware/guild_stickers_update.py b/pincer/middleware/guild_stickers_update.py index 15a63016..57c20520 100644 --- a/pincer/middleware/guild_stickers_update.py +++ b/pincer/middleware/guild_stickers_update.py @@ -29,7 +29,10 @@ async def guild_stickers_update_middleware(self, payload: GatewayDispatch): construct_client_dict(self, payload.data) ) - self.guilds[event.guild_id].stickers = event.stickers + guild = self.guilds.get(event.guild_id) + + if guild: + guild.stickers = event.stickers return ( "on_guild_stickers_update", From f5f027dca29bdb0e44548124fc0bfdb3e9295f9c Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Thu, 9 Dec 2021 23:11:52 -0500 Subject: [PATCH 07/34] :speech_balloon: fix typo --- pincer/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pincer/client.py b/pincer/client.py index 7bd572f4..b0350e8e 100644 --- a/pincer/client.py +++ b/pincer/client.py @@ -566,7 +566,7 @@ async def event_handler(self, _, payload: GatewayDispatch): ---------- _ : Socket param, but this isn't required for this handler. So - its just a filler parameter, doesn't matter what is passed. + it's just a filler parameter, doesn't matter what is passed. payload : :class:`~pincer.core.dispatch.GatewayDispatch` The payload sent from the Discord gateway, this contains the required data for the client to know what event it is and @@ -583,7 +583,7 @@ async def payload_event_handler(self, _, payload: GatewayDispatch): ---------- _ : Socket param, but this isn't required for this handler. So - its just a filler parameter, doesn't matter what is passed. + it's just a filler parameter, doesn't matter what is passed. payload : :class:`~pincer.core.dispatch.GatewayDispatch` The payload sent from the Discord gateway, this contains the required data for the client to know what event it is and @@ -858,4 +858,5 @@ async def get_webhook( """ return await Webhook.from_id(self, id, token) + Bot = Client From 560400f1045b30f52c66817fe95407cf4a12f949 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 10 Dec 2021 20:23:20 -0500 Subject: [PATCH 08/34] :sparkles: thread and channel updates --- pincer/middleware/stage_instance_create.py | 13 ++++++----- pincer/middleware/stage_instance_delete.py | 15 ++++++++----- pincer/middleware/stage_instance_update.py | 17 +++++++++------ pincer/middleware/thread_create.py | 18 ++++++++++------ pincer/middleware/thread_delete.py | 13 +++++++---- pincer/middleware/thread_list_sync.py | 25 ++++++++-------------- pincer/middleware/thread_members_update.py | 21 +----------------- pincer/middleware/thread_update.py | 19 +++++++++++----- pincer/objects/user/voice_state.py | 4 ++-- 9 files changed, 76 insertions(+), 69 deletions(-) diff --git a/pincer/middleware/stage_instance_create.py b/pincer/middleware/stage_instance_create.py index f559f650..1d036c56 100644 --- a/pincer/middleware/stage_instance_create.py +++ b/pincer/middleware/stage_instance_create.py @@ -8,7 +8,7 @@ from ..utils.types import Coro -def stage_instance_create_middleware(self, payload: GatewayDispatch): +async def stage_instance_create_middleware(self, payload: GatewayDispatch): """|coro| Middleware for the ``on_stage_instance_create`` event. @@ -24,10 +24,13 @@ def stage_instance_create_middleware(self, payload: GatewayDispatch): ``on_stage_instance_create`` and a ``StageInstance`` """ - return ( - "on_stage_instance_create", - StageInstance.from_dict(construct_client_dict(self, payload.data)) - ) + stage = StageInstance.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(stage.guild_id) + if guild: + guild.stage_instances.append(stage) + + return "on_stage_instance_create", stage def export() -> Coro: diff --git a/pincer/middleware/stage_instance_delete.py b/pincer/middleware/stage_instance_delete.py index ceb2a7ec..b6f394da 100644 --- a/pincer/middleware/stage_instance_delete.py +++ b/pincer/middleware/stage_instance_delete.py @@ -8,7 +8,7 @@ from ..utils.types import Coro -def stage_instance_delete_middleware(self, payload: GatewayDispatch): +async def stage_instance_delete_middleware(self, payload: GatewayDispatch): """|coro| Middleware for the ``on_stage_instance_delete`` event. @@ -24,10 +24,15 @@ def stage_instance_delete_middleware(self, payload: GatewayDispatch): ``on_stage_instance_delete`` and a ``StageInstance`` """ - return ( - "on_stage_instance_delete", - StageInstance.from_dict(construct_client_dict(self, payload.data)) - ) + stage = StageInstance.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(stage.guild_id) + if guild: + guild.stage_instances = list( + filter(lambda _stage: _stage.id != stage.id, guild.stage_instances) + ) + + return "on_stage_instance_delete", stage def export() -> Coro: diff --git a/pincer/middleware/stage_instance_update.py b/pincer/middleware/stage_instance_update.py index 4470db96..2ebf299b 100644 --- a/pincer/middleware/stage_instance_update.py +++ b/pincer/middleware/stage_instance_update.py @@ -5,10 +5,10 @@ from ..core.dispatch import GatewayDispatch from ..objects import StageInstance from ..utils.conversion import construct_client_dict -from ..utils.types import Coro +from ..utils import Coro, replace -def stage_instance_update_middleware(self, payload: GatewayDispatch): +async def stage_instance_update_middleware(self, payload: GatewayDispatch): """|coro| Middleware for the ``on_stage_instance_update`` event. @@ -24,10 +24,15 @@ def stage_instance_update_middleware(self, payload: GatewayDispatch): ``on_stage_instance_update`` and a ``StageInstance`` """ - return ( - "on_stage_instance_update", - StageInstance.from_dict(construct_client_dict(self, payload.data)) - ) + stage = StageInstance.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(stage.guild_id) + if guild: + guild.stage_instances = replace( + lambda _stage: _stage.id == stage.id, guild.stage_instances, stage + ) + + return "on_stage_instance_update", stage def export() -> Coro: diff --git a/pincer/middleware/thread_create.py b/pincer/middleware/thread_create.py index c74c9fb9..29154bee 100644 --- a/pincer/middleware/thread_create.py +++ b/pincer/middleware/thread_create.py @@ -4,11 +4,11 @@ """sent when a thread is created/joined on the client.""" from ..core.dispatch import GatewayDispatch -from ..objects import Channel from ..utils.conversion import construct_client_dict +from ..objects import Channel -def thread_create_middleware(self, payload: GatewayDispatch): +async def thread_create_middleware(self, payload: GatewayDispatch): """|coro| Middleware for the ``on_thread_create`` event. @@ -24,10 +24,16 @@ def thread_create_middleware(self, payload: GatewayDispatch): ``on_thread_create`` and an ``Channel`` """ - return ( - "on_thread_create", - Channel.from_dict(construct_client_dict(self, payload.data)) - ) + channel: Channel = Channel.from_dict(construct_client_dict(self, payload.data)) + + if self.guilds[channel.guild_id].threads: + self.guilds[channel.guild_id].threads.append(channel) + else: + self.guilds[channel.guild_id].threads = [channel] + + self.channels[channel.id] = channel + + return "on_thread_create", channel def export(): diff --git a/pincer/middleware/thread_delete.py b/pincer/middleware/thread_delete.py index d661976f..8c67921d 100644 --- a/pincer/middleware/thread_delete.py +++ b/pincer/middleware/thread_delete.py @@ -24,10 +24,15 @@ async def thread_delete_middleware(self, payload: GatewayDispatch): ``on_thread_delete`` and an ``Channel`` """ - return ( - "on_thread_delete", - Channel.from_dict(construct_client_dict(self, payload.data)) - ) + channel = Channel.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(channel.guild_id) + if guild: + guild.threads = list(filter(lambda c: c.id != channel.id, guild.threads)) + + self.channels.pop(channel.id, None) + + return "on_thread_delete", channel def export(): diff --git a/pincer/middleware/thread_list_sync.py b/pincer/middleware/thread_list_sync.py index b210e029..cc9981ed 100644 --- a/pincer/middleware/thread_list_sync.py +++ b/pincer/middleware/thread_list_sync.py @@ -7,6 +7,8 @@ from typing import List +from pincer.utils.types import MISSING + from ..core.dispatch import GatewayDispatch from ..objects import Channel, ThreadMember from ..objects.events.thread import ThreadListSyncEvent @@ -29,22 +31,13 @@ async def thread_list_sync(self, payload: GatewayDispatch): ``on_thread_list_sync`` and an ``ThreadListSyncEvent`` """ # noqa: E501 - threads: List[Channel] = [ - Channel.from_dict(construct_client_dict(self, thread)) - for thread in payload.data.pop("threads") - ] - - members: List[ThreadMember] = [ - ThreadMember.from_dict(construct_client_dict(self, member)) - for member in payload.data.pop("members") - ] - - return ( - "on_thread_list_sync", - ThreadListSyncEvent.from_dict( - {"threads": threads, "members": members, **payload.data} - ) - ) + event = ThreadListSyncEvent.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(event.guild_id) + if guild: + guild.threads = event.threads + + return "on_thread_list_sync", event def export(): diff --git a/pincer/middleware/thread_members_update.py b/pincer/middleware/thread_members_update.py index 9f46c85e..fcaa5857 100644 --- a/pincer/middleware/thread_members_update.py +++ b/pincer/middleware/thread_members_update.py @@ -5,9 +5,7 @@ from typing import List from ..core.dispatch import GatewayDispatch -from ..objects import ThreadMember from ..objects.events.thread import ThreadMembersUpdateEvent -from ..utils import Timestamp from ..utils.conversion import construct_client_dict @@ -27,26 +25,9 @@ async def thread_members_update_middleware(self, payload: GatewayDispatch): Tuple[:class:`str`, :class:`~pincer.objects.events.thread.ThreadMembersUpdateEvent`] ``on_thread_members_update`` and an ``ThreadMembersUpdateEvent`` """ # noqa: E501 - - added_members: List[ThreadMember] = [ - ThreadMember.from_dict(construct_client_dict( - self, - { - "join_timestamp": Timestamp(added_member.pop("join_timestamp")), - **added_member - } - )) - for added_member in payload.data.pop("added_members") - ] - return ( "on_thread_members_update", - ThreadMembersUpdateEvent.from_dict( - { - "added_members": added_members, - **payload.data - } - ) + ThreadMembersUpdateEvent.from_dict(construct_client_dict(self, payload.data)) ) diff --git a/pincer/middleware/thread_update.py b/pincer/middleware/thread_update.py index f992dbee..e201b9e9 100644 --- a/pincer/middleware/thread_update.py +++ b/pincer/middleware/thread_update.py @@ -5,7 +5,7 @@ from ..core.dispatch import GatewayDispatch from ..objects import Channel -from ..utils.conversion import construct_client_dict +from ..utils import construct_client_dict, replace async def thread_update_middleware(self, payload: GatewayDispatch): @@ -25,10 +25,19 @@ async def thread_update_middleware(self, payload: GatewayDispatch): ``on_thread_update`` and an ``Channel`` """ - return ( - "on_thread_update", - Channel.from_dict(construct_client_dict(self, payload.data)) - ) + channel = Channel.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(channel.guild_id) + + if guild: + guild.threads = replace( + lambda _channel: _channel.id == channel.id, + self.guilds[channel.guild_id].threads, + channel + ) + self.channels[channel.id] = channel + + return "on_thread_update", channel def export(): diff --git a/pincer/objects/user/voice_state.py b/pincer/objects/user/voice_state.py index a375462c..4897ecaa 100644 --- a/pincer/objects/user/voice_state.py +++ b/pincer/objects/user/voice_state.py @@ -53,7 +53,6 @@ class VoiceState(APIObject): """ # noqa: E501 - channel_id: Optional[Snowflake] user_id: Snowflake session_id: str deaf: bool @@ -62,8 +61,9 @@ class VoiceState(APIObject): self_mute: bool self_video: bool suppress: bool - request_to_speak_timestamp: Optional[Timestamp] + channel_id: APINullable[Snowflake] = MISSING + request_to_speak_timestamp: APINullable[Timestamp] = MISSING guild_id: APINullable[Snowflake] = MISSING member: APINullable[GuildMember] = MISSING self_stream: APINullable[bool] = MISSING From 33bf97c3fb2cd2c5b802dbb9de31e2bcb5359bde Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 10 Dec 2021 20:50:35 -0500 Subject: [PATCH 09/34] :sparkles: caching voice_states --- pincer/middleware/voice_state_update.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pincer/middleware/voice_state_update.py b/pincer/middleware/voice_state_update.py index 0b62a58b..3c03b76b 100644 --- a/pincer/middleware/voice_state_update.py +++ b/pincer/middleware/voice_state_update.py @@ -10,7 +10,7 @@ from typing import TYPE_CHECKING from ..objects.user.voice_state import VoiceState -from ..utils.conversion import construct_client_dict +from ..utils import construct_client_dict, replace if TYPE_CHECKING: from typing import List, Tuple @@ -34,13 +34,21 @@ async def voice_state_update_middleware( ------- Tuple[:class:`str`, :class:`~pincer.objects.user.voice_state.VoiceState`] ``on_voice_state_update`` and a ``VoiceState`` - """ - # noqa: E501 + """ # noqa: E501 - return ( - "on_voice_state_update", - VoiceState.from_dict(construct_client_dict(self, payload.data)) - ) + voice_state = VoiceState.from_dict(construct_client_dict(self, payload.data)) + + guild = self.guilds.get(voice_state.guild_id) + + if guild: + for index, state in enumerate(guild.voice_states): + if state.user_id == voice_state.user_id: + guild.voice_states[index] = voice_state + break + else: + guild.voice_states.append(voice_state) + + return "on_voice_state_update", voice_state def export(): From 95dc34746a424e0ea0339cea76ed26295dcdc8e0 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 10 Dec 2021 22:49:20 -0500 Subject: [PATCH 10/34] :memo: add docs --- pincer/objects/guild/channel.py | 3 ++- pincer/utils/api_object.py | 32 ++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pincer/objects/guild/channel.py b/pincer/objects/guild/channel.py index cdc629cd..38170773 100644 --- a/pincer/objects/guild/channel.py +++ b/pincer/objects/guild/channel.py @@ -170,7 +170,8 @@ class Channel(APIObject): # noqa E501 parent_id: APINullable[Optional[Snowflake]] = MISSING permissions: APINullable[str] = MISSING permission_overwrites: APINullable[List[Overwrite]] = MISSING - position: APINullable[int] = MISSING + # Position is always 0 when not sent + position: APINullable[int] = 0 rate_limit_per_user: APINullable[int] = MISSING recipients: APINullable[List[User]] = MISSING rtc_region: APINullable[Optional[str]] = MISSING diff --git a/pincer/utils/api_object.py b/pincer/utils/api_object.py index dfcb59c2..3d705432 100644 --- a/pincer/utils/api_object.py +++ b/pincer/utils/api_object.py @@ -18,6 +18,8 @@ from ..exceptions import InvalidArgumentAnnotation if TYPE_CHECKING: + from ..objects.guild.channel import Channel + from ..objects.guild.guild import Guild from ..client import Client T = TypeVar("T") @@ -72,16 +74,30 @@ def _asdict_ignore_none(obj: Generic[T]) -> Union[Tuple, Dict, T]: return copy.deepcopy(obj) -def get_guild(self): - "Return a guild" - return self._client.guilds[self.guild_id] +def get_guild(obj) -> Guild: + """Return a guild from an APIObject + Parameters + ---------- + obj : :class:`~pincer.utils.api_object.APIObject` + + Returns + ------- + :class:`~pincer.objects.guild.guild.Guild` + """ + return obj._client.guilds[obj.guild_id] -def get_channel(self): - "Return a channel" - return next( - filter(lambda c: c.id == self.channel_id, self.guild.channels) - ) +def get_channel(obj) -> Channel: + """Return a channel from an APIObject + Parameters + ---------- + obj : :class:`~pincer.utils.api_object.APIObject` + + Returns + ------- + :class:`~pincer.objects.guild.channel.Channel` + """ + return obj._client.channels[obj.channel_id] class HTTPMeta(type): From 6b3b2c4b76fe9752da10391cb2e98f765a87b43c Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 13:01:28 -0500 Subject: [PATCH 11/34] :art: changed properties to classes --- pincer/objects/message/user_message.py | 12 +----- pincer/utils/api_object.py | 58 ++++++++++++++------------ 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/pincer/objects/message/user_message.py b/pincer/objects/message/user_message.py index c1ab17f3..a747390f 100644 --- a/pincer/objects/message/user_message.py +++ b/pincer/objects/message/user_message.py @@ -19,7 +19,7 @@ from ..guild.role import Role from ..user.user import User from ..._config import GatewayConfig -from ...utils.api_object import APIObject, get_guild, get_channel +from ...utils.api_object import APIObject, GuildProperty, ChannelProperty from ...utils.conversion import construct_client_dict from ...utils.snowflake import Snowflake from ...utils.types import MISSING @@ -245,7 +245,7 @@ class MessageActivity(APIObject): @dataclass(repr=False) -class UserMessage(APIObject): +class UserMessage(APIObject, GuildProperty, ChannelProperty): """Represents a message sent in a channel within Discord. Attributes @@ -353,14 +353,6 @@ class UserMessage(APIObject): def __str__(self): return self.content - @property - def guild(self): - return get_guild(self) - - @property - def channel(self): - return get_channel(self) - async def get_most_recent(self): """|coro| diff --git a/pincer/utils/api_object.py b/pincer/utils/api_object.py index 3d705432..7bf2fe82 100644 --- a/pincer/utils/api_object.py +++ b/pincer/utils/api_object.py @@ -74,32 +74,6 @@ def _asdict_ignore_none(obj: Generic[T]) -> Union[Tuple, Dict, T]: return copy.deepcopy(obj) -def get_guild(obj) -> Guild: - """Return a guild from an APIObject - Parameters - ---------- - obj : :class:`~pincer.utils.api_object.APIObject` - - Returns - ------- - :class:`~pincer.objects.guild.guild.Guild` - """ - return obj._client.guilds[obj.guild_id] - - -def get_channel(obj) -> Channel: - """Return a channel from an APIObject - Parameters - ---------- - obj : :class:`~pincer.utils.api_object.APIObject` - - Returns - ------- - :class:`~pincer.objects.guild.channel.Channel` - """ - return obj._client.channels[obj.channel_id] - - class HTTPMeta(type): __meta_items: List[str] = ["_client", "_http"] __ori_annotations: Dict[str, type] = {} @@ -308,3 +282,35 @@ def from_dict( def to_dict(self) -> Dict: """Transform the current object to a dictionary representation.""" return _asdict_ignore_none(self) + + +class GuildProperty: + + @property + def guild(obj) -> Guild: + """Return a guild from an APIObject + Parameters + ---------- + obj : :class:`~pincer.utils.api_object.APIObject` + + Returns + ------- + :class:`~pincer.objects.guild.guild.Guild` + """ + return obj._client.guilds[obj.guild_id] + + +class ChannelProperty: + + @property + def channel(obj) -> Channel: + """Return a channel from an APIObject + Parameters + ---------- + obj : :class:`~pincer.utils.api_object.APIObject` + + Returns + ------- + :class:`~pincer.objects.guild.channel.Channel` + """ + return obj._client.channels[obj.channel_id] From ad7727a109a723fc82d420f1626c1e92ac91bc8e Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 13:09:38 -0500 Subject: [PATCH 12/34] :sparkles: added channel properties --- pincer/middleware/notification_create.py | 3 ++- pincer/objects/app/interactions.py | 4 +++- pincer/objects/events/channel.py | 4 ++-- pincer/objects/events/invite.py | 6 +++--- pincer/objects/events/message.py | 16 +++++++++------- pincer/objects/events/notification.py | 4 ++-- pincer/objects/events/typing_start.py | 4 ++-- pincer/objects/events/webhook.py | 4 ++-- pincer/objects/guild/audit_log.py | 4 ++-- pincer/objects/guild/followed_channel.py | 4 ++-- pincer/objects/guild/stage.py | 4 ++-- pincer/objects/guild/welcome_screen.py | 4 ++-- pincer/objects/message/reference.py | 4 ++-- pincer/objects/user/voice_state.py | 4 ++-- 14 files changed, 37 insertions(+), 32 deletions(-) diff --git a/pincer/middleware/notification_create.py b/pincer/middleware/notification_create.py index c65cc582..1a3d1872 100644 --- a/pincer/middleware/notification_create.py +++ b/pincer/middleware/notification_create.py @@ -6,6 +6,7 @@ (mention or new message in eligible channels) """ +from pincer.utils.snowflake import Snowflake from ..core.dispatch import GatewayDispatch from ..objects.events.notification import NotificationCreateEvent from ..utils.conversion import construct_client_dict @@ -27,7 +28,7 @@ async def notification_create_middleware(self, payload: GatewayDispatch): Tuple[:class:`str`, :class:`~pincer.objects.events.notification.NotificationCreateEvent`] ``on_notification_create`` and a ``NotificationCreateEvent`` """ # noqa: E501 - channel_id: int = payload.data.get("channel_id") + channel_id = payload.data.get("channel_id") payload.data["message"]["channel_id"] = channel_id return ( "on_notification_create", diff --git a/pincer/objects/app/interactions.py b/pincer/objects/app/interactions.py index e2279bb8..d4ce03fb 100644 --- a/pincer/objects/app/interactions.py +++ b/pincer/objects/app/interactions.py @@ -7,6 +7,8 @@ from dataclasses import dataclass from typing import Any, Dict, TYPE_CHECKING, Union, Optional, List +from pincer.utils.api_object import ChannelProperty, GuildProperty + from .command_types import AppCommandOptionType from .interaction_base import InteractionType, CallbackType from .mentionable import Mentionable @@ -96,7 +98,7 @@ class InteractionData(APIObject): @dataclass(repr=False) -class Interaction(APIObject): +class Interaction(APIObject, ChannelProperty, GuildProperty): """Represents a Discord Interaction object Attributes diff --git a/pincer/objects/events/channel.py b/pincer/objects/events/channel.py index a3569135..0fdef637 100644 --- a/pincer/objects/events/channel.py +++ b/pincer/objects/events/channel.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty from ...utils.types import MISSING, APINullable if TYPE_CHECKING: @@ -15,7 +15,7 @@ @dataclass(repr=False) -class ChannelPinsUpdateEvent(APIObject): +class ChannelPinsUpdateEvent(APIObject, ChannelProperty): """Sent when a message is pinned or unpinned in a text channel. This is not sent when a pinned message is deleted. diff --git a/pincer/objects/events/invite.py b/pincer/objects/events/invite.py index e642ddb6..bb05592e 100644 --- a/pincer/objects/events/invite.py +++ b/pincer/objects/events/invite.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -17,7 +17,7 @@ @dataclass(repr=False) -class InviteCreateEvent(APIObject): +class InviteCreateEvent(APIObject, ChannelProperty): """Sent when a new invite to a channel is created. Attributes @@ -63,7 +63,7 @@ class InviteCreateEvent(APIObject): @dataclass(repr=False) -class InviteDeleteEvent(APIObject): +class InviteDeleteEvent(APIObject, ChannelProperty): """Sent when an invite is deleted. Attributes diff --git a/pincer/objects/events/message.py b/pincer/objects/events/message.py index a8e26dc7..6fae6301 100644 --- a/pincer/objects/events/message.py +++ b/pincer/objects/events/message.py @@ -6,7 +6,9 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from pincer.objects.guild.channel import Channel + +from ...utils.api_object import APIObject, ChannelProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -18,7 +20,7 @@ @dataclass(repr=False) -class MessageDeleteEvent(APIObject): +class MessageDeleteEvent(APIObject, ChannelProperty): """Sent when a message is deleted. Attributes @@ -37,7 +39,7 @@ class MessageDeleteEvent(APIObject): @dataclass(repr=False) -class MessageDeleteBulkEvent(APIObject): +class MessageDeleteBulkEvent(APIObject, ChannelProperty): """Sent when multiple messages are deleted at once. Attributes @@ -56,7 +58,7 @@ class MessageDeleteBulkEvent(APIObject): @dataclass(repr=False) -class MessageReactionAddEvent(APIObject): +class MessageReactionAddEvent(APIObject, ChannelProperty): """Sent when a user adds a reaction to a message. Attributes @@ -84,7 +86,7 @@ class MessageReactionAddEvent(APIObject): @dataclass(repr=False) -class MessageReactionRemoveEvent(APIObject): +class MessageReactionRemoveEvent(APIObject, ChannelProperty): """Sent when a user removes a reaction from a message. Attributes @@ -109,7 +111,7 @@ class MessageReactionRemoveEvent(APIObject): @dataclass(repr=False) -class MessageReactionRemoveAllEvent(APIObject): +class MessageReactionRemoveAllEvent(APIObject, ChannelProperty): """Sent when a user explicitly removes all reactions from a message. Attributes @@ -127,7 +129,7 @@ class MessageReactionRemoveAllEvent(APIObject): @dataclass(repr=False) -class MessageReactionRemoveEmojiEvent(APIObject): +class MessageReactionRemoveEmojiEvent(APIObject, ChannelProperty): """Sent when a bot removes all instances of a given emoji from the reactions of a message. diff --git a/pincer/objects/events/notification.py b/pincer/objects/events/notification.py index c5c15b0b..ce485232 100644 --- a/pincer/objects/events/notification.py +++ b/pincer/objects/events/notification.py @@ -5,12 +5,12 @@ from dataclasses import dataclass from ...objects.message import UserMessage -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty from ...utils.snowflake import Snowflake @dataclass(repr=False) -class NotificationCreateEvent(APIObject): +class NotificationCreateEvent(APIObject, ChannelProperty): """ Represents a notification diff --git a/pincer/objects/events/typing_start.py b/pincer/objects/events/typing_start.py index 7741610f..e366272d 100644 --- a/pincer/objects/events/typing_start.py +++ b/pincer/objects/events/typing_start.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -15,7 +15,7 @@ @dataclass(repr=False) -class TypingStartEvent(APIObject): +class TypingStartEvent(APIObject, ChannelProperty): """Sent when a user starts typing in a channel. Attributes diff --git a/pincer/objects/events/webhook.py b/pincer/objects/events/webhook.py index a68da56c..c403653f 100644 --- a/pincer/objects/events/webhook.py +++ b/pincer/objects/events/webhook.py @@ -6,14 +6,14 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty if TYPE_CHECKING: from ...utils.snowflake import Snowflake @dataclass(repr=False) -class WebhooksUpdateEvent(APIObject): +class WebhooksUpdateEvent(APIObject, ChannelProperty): """ Sent when a guild's channel webhook is created, updated, or deleted. diff --git a/pincer/objects/guild/audit_log.py b/pincer/objects/guild/audit_log.py index 92fb758b..4fd37072 100644 --- a/pincer/objects/guild/audit_log.py +++ b/pincer/objects/guild/audit_log.py @@ -7,7 +7,7 @@ from enum import IntEnum from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty from ...utils.types import MISSING if TYPE_CHECKING: @@ -182,7 +182,7 @@ class AuditLogChange(APIObject): @dataclass(repr=False) -class AuditEntryInfo(APIObject): +class AuditEntryInfo(APIObject, ChannelProperty): """Represents Discord Optional Audit Entry Info Attributes diff --git a/pincer/objects/guild/followed_channel.py b/pincer/objects/guild/followed_channel.py index 4e8b2fed..7f2ad9d6 100644 --- a/pincer/objects/guild/followed_channel.py +++ b/pincer/objects/guild/followed_channel.py @@ -6,14 +6,14 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty if TYPE_CHECKING: from ...utils.snowflake import Snowflake @dataclass(repr=False) -class FollowedChannel(APIObject): +class FollowedChannel(APIObject, ChannelProperty): """Represents a Discord Followed Channel object Attributes diff --git a/pincer/objects/guild/stage.py b/pincer/objects/guild/stage.py index 22e59e3f..42e900e1 100644 --- a/pincer/objects/guild/stage.py +++ b/pincer/objects/guild/stage.py @@ -7,7 +7,7 @@ from enum import IntEnum from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty if TYPE_CHECKING: from ...utils.snowflake import Snowflake @@ -28,7 +28,7 @@ class PrivacyLevel(IntEnum): @dataclass(repr=False) -class StageInstance(APIObject): +class StageInstance(APIObject, ChannelProperty): """Represents a Stage Instance object Attributes diff --git a/pincer/objects/guild/welcome_screen.py b/pincer/objects/guild/welcome_screen.py index 21381766..65898e1b 100644 --- a/pincer/objects/guild/welcome_screen.py +++ b/pincer/objects/guild/welcome_screen.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty if TYPE_CHECKING: from typing import Optional, List @@ -15,7 +15,7 @@ @dataclass(repr=False) -class WelcomeScreenChannel(APIObject): +class WelcomeScreenChannel(APIObject, ChannelProperty): """Represents a welcome screen channel. This is a channel which gets shown on the welcome screen. diff --git a/pincer/objects/message/reference.py b/pincer/objects/message/reference.py index b44c1c82..7f065c1e 100644 --- a/pincer/objects/message/reference.py +++ b/pincer/objects/message/reference.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty from ...utils.types import MISSING if TYPE_CHECKING: @@ -15,7 +15,7 @@ @dataclass(repr=False) -class MessageReference(APIObject): +class MessageReference(APIObject, ChannelProperty, GuildProperty): """Represents a Discord Message Reference object Attributes diff --git a/pincer/objects/user/voice_state.py b/pincer/objects/user/voice_state.py index 4897ecaa..fe5c01c4 100644 --- a/pincer/objects/user/voice_state.py +++ b/pincer/objects/user/voice_state.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty from ...utils.types import MISSING if TYPE_CHECKING: @@ -19,7 +19,7 @@ @dataclass(repr=False) -class VoiceState(APIObject): +class VoiceState(APIObject, ChannelProperty, GuildProperty): """Used to represent a user's voice connection status Attributes From 8b4ebf0a28d6d67283e883234933a56f85a1b65f Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 13:18:32 -0500 Subject: [PATCH 13/34] :sparkles: added guild properties --- pincer/objects/app/application.py | 4 ++-- pincer/objects/app/command.py | 4 ++-- pincer/objects/events/channel.py | 4 ++-- pincer/objects/events/gateway_commands.py | 6 +++--- pincer/objects/events/guild.py | 26 +++++++++++------------ pincer/objects/events/integration.py | 8 +++---- pincer/objects/events/invite.py | 6 +++--- pincer/objects/events/message.py | 16 ++++++-------- pincer/objects/events/presence.py | 4 ++-- pincer/objects/events/thread.py | 6 +++--- pincer/objects/events/typing_start.py | 4 ++-- pincer/objects/events/voice.py | 6 +++--- pincer/objects/guild/channel.py | 4 ++-- pincer/objects/guild/stage.py | 4 ++-- 14 files changed, 50 insertions(+), 52 deletions(-) diff --git a/pincer/objects/app/application.py b/pincer/objects/app/application.py index 28fdc29e..f41c6820 100644 --- a/pincer/objects/app/application.py +++ b/pincer/objects/app/application.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.types import MISSING if TYPE_CHECKING: @@ -18,7 +18,7 @@ @dataclass(repr=False) -class Application(APIObject): +class Application(APIObject, GuildProperty): """Represents a Discord application. (e.g. Bot, OAuth) Attributes diff --git a/pincer/objects/app/command.py b/pincer/objects/app/command.py index 4729950a..5095660b 100644 --- a/pincer/objects/app/command.py +++ b/pincer/objects/app/command.py @@ -9,7 +9,7 @@ from .command_types import AppCommandOptionType, AppCommandType from ...objects.guild.channel import ChannelType -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.snowflake import Snowflake from ...utils.types import Coro, choice_value_types from ...utils.types import MISSING @@ -93,7 +93,7 @@ class AppCommandOption(APIObject): @dataclass(repr=False) -class AppCommand(APIObject): +class AppCommand(APIObject, GuildProperty): """Represents a Discord Application Command object Attributes diff --git a/pincer/objects/events/channel.py b/pincer/objects/events/channel.py index 0fdef637..fca5a76e 100644 --- a/pincer/objects/events/channel.py +++ b/pincer/objects/events/channel.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject, ChannelProperty +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty from ...utils.types import MISSING, APINullable if TYPE_CHECKING: @@ -15,7 +15,7 @@ @dataclass(repr=False) -class ChannelPinsUpdateEvent(APIObject, ChannelProperty): +class ChannelPinsUpdateEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a message is pinned or unpinned in a text channel. This is not sent when a pinned message is deleted. diff --git a/pincer/objects/events/gateway_commands.py b/pincer/objects/events/gateway_commands.py index 10777dbd..560f9f8c 100644 --- a/pincer/objects/events/gateway_commands.py +++ b/pincer/objects/events/gateway_commands.py @@ -7,7 +7,7 @@ from enum import auto, Enum from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -70,7 +70,7 @@ class Resume(APIObject): @dataclass(repr=False) -class RequestGuildMembers(APIObject): +class RequestGuildMembers(APIObject, GuildProperty): """Used to request all members for a guild or a list of guilds. guild_id: @@ -104,7 +104,7 @@ class RequestGuildMembers(APIObject): @dataclass(repr=False) -class UpdateVoiceState(APIObject): +class UpdateVoiceState(APIObject, GuildProperty): """Sent when a client wants to join, move, or disconnect from a voice channel. diff --git a/pincer/objects/events/guild.py b/pincer/objects/events/guild.py index e9a30436..3ad5389e 100644 --- a/pincer/objects/events/guild.py +++ b/pincer/objects/events/guild.py @@ -8,7 +8,7 @@ from ..guild.guild import Guild from ..guild.member import GuildMember from ..user import User -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.conversion import construct_client_dict from ...utils.types import MISSING, APINullable @@ -24,7 +24,7 @@ @dataclass(repr=False) -class GuildBanAddEvent(APIObject): +class GuildBanAddEvent(APIObject, GuildProperty): """Sent when a user is banned from a guild. Attributes @@ -40,7 +40,7 @@ class GuildBanAddEvent(APIObject): @dataclass(repr=False) -class GuildBanRemoveEvent(APIObject): +class GuildBanRemoveEvent(APIObject, GuildProperty): """Sent when a user is unbanned from a guild. Attributes @@ -56,7 +56,7 @@ class GuildBanRemoveEvent(APIObject): @dataclass(repr=False) -class GuildEmojisUpdateEvent(APIObject): +class GuildEmojisUpdateEvent(APIObject, GuildProperty): """Sent when a guild's emojis have been updated. Attributes @@ -72,7 +72,7 @@ class GuildEmojisUpdateEvent(APIObject): @dataclass(repr=False) -class GuildStickersUpdateEvent(APIObject): +class GuildStickersUpdateEvent(APIObject, GuildProperty): """Sent when a guild's stickers have been updated. Attributes @@ -88,7 +88,7 @@ class GuildStickersUpdateEvent(APIObject): @dataclass(repr=False) -class GuildIntegrationsUpdateEvent(APIObject): +class GuildIntegrationsUpdateEvent(APIObject, GuildProperty): """Sent when a guild integration is updated. Attributes @@ -101,7 +101,7 @@ class GuildIntegrationsUpdateEvent(APIObject): @dataclass(repr=False) -class GuildMemberAddEvent(GuildMember): +class GuildMemberAddEvent(GuildMember, GuildProperty): """ Sent when a user joins a guild. @@ -118,7 +118,7 @@ class GuildMemberAddEvent(GuildMember): @dataclass(repr=False) -class GuildMemberRemoveEvent(APIObject): +class GuildMemberRemoveEvent(APIObject, GuildProperty): """Sent when a user is removed from a guild (leave/kick/ban). Attributes @@ -139,7 +139,7 @@ def __post_init__(self): @dataclass(repr=False) -class GuildMemberUpdateEvent(APIObject): +class GuildMemberUpdateEvent(APIObject, GuildProperty): """Sent when a guild member is updated. This will also fire when the user object of a guild member changes. @@ -185,7 +185,7 @@ def __post_init__(self): @dataclass(repr=False) -class GuildMembersChunkEvent(APIObject): +class GuildMembersChunkEvent(APIObject, GuildProperty): """Sent in response to Guild Request Members. You can use the ``chunk_index`` and ``chunk_count`` to calculate how many chunks are left for your request. @@ -228,7 +228,7 @@ def __post_init__(self): @dataclass(repr=False) -class GuildRoleCreateEvent(APIObject): +class GuildRoleCreateEvent(APIObject, GuildProperty): """Sent when a guild role is created. Attributes @@ -244,7 +244,7 @@ class GuildRoleCreateEvent(APIObject): @dataclass(repr=False) -class GuildRoleUpdateEvent(APIObject): +class GuildRoleUpdateEvent(APIObject, GuildProperty): """Sent when a guild role is updated. Attributes @@ -260,7 +260,7 @@ class GuildRoleUpdateEvent(APIObject): @dataclass(repr=False) -class GuildRoleDeleteEvent(APIObject): +class GuildRoleDeleteEvent(APIObject, GuildProperty): """Sent when a guild role is deleted. Attributes diff --git a/pincer/objects/events/integration.py b/pincer/objects/events/integration.py index 43ab0fca..e847d1a0 100644 --- a/pincer/objects/events/integration.py +++ b/pincer/objects/events/integration.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.types import MISSING, APINullable if TYPE_CHECKING: @@ -14,7 +14,7 @@ @dataclass(repr=False) -class IntegrationDeleteEvent(APIObject): +class IntegrationDeleteEvent(APIObject, GuildProperty): """Sent when an integration is deleted. Attributes @@ -32,7 +32,7 @@ class IntegrationDeleteEvent(APIObject): @dataclass(repr=False) -class IntegrationCreateEvent(APIObject): +class IntegrationCreateEvent(APIObject, GuildProperty): """ Sent when an integration is created. @@ -53,7 +53,7 @@ class IntegrationCreateEvent(APIObject): @dataclass(repr=False) -class IntegrationUpdateEvent(APIObject): +class IntegrationUpdateEvent(APIObject, GuildProperty): """ Sent when an integration is updated. diff --git a/pincer/objects/events/invite.py b/pincer/objects/events/invite.py index bb05592e..e1ccbff3 100644 --- a/pincer/objects/events/invite.py +++ b/pincer/objects/events/invite.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject, ChannelProperty +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -17,7 +17,7 @@ @dataclass(repr=False) -class InviteCreateEvent(APIObject, ChannelProperty): +class InviteCreateEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a new invite to a channel is created. Attributes @@ -63,7 +63,7 @@ class InviteCreateEvent(APIObject, ChannelProperty): @dataclass(repr=False) -class InviteDeleteEvent(APIObject, ChannelProperty): +class InviteDeleteEvent(APIObject, ChannelProperty, GuildProperty): """Sent when an invite is deleted. Attributes diff --git a/pincer/objects/events/message.py b/pincer/objects/events/message.py index 6fae6301..1131078f 100644 --- a/pincer/objects/events/message.py +++ b/pincer/objects/events/message.py @@ -6,9 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from pincer.objects.guild.channel import Channel - -from ...utils.api_object import APIObject, ChannelProperty +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -20,7 +18,7 @@ @dataclass(repr=False) -class MessageDeleteEvent(APIObject, ChannelProperty): +class MessageDeleteEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a message is deleted. Attributes @@ -39,7 +37,7 @@ class MessageDeleteEvent(APIObject, ChannelProperty): @dataclass(repr=False) -class MessageDeleteBulkEvent(APIObject, ChannelProperty): +class MessageDeleteBulkEvent(APIObject, ChannelProperty, GuildProperty): """Sent when multiple messages are deleted at once. Attributes @@ -58,7 +56,7 @@ class MessageDeleteBulkEvent(APIObject, ChannelProperty): @dataclass(repr=False) -class MessageReactionAddEvent(APIObject, ChannelProperty): +class MessageReactionAddEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a user adds a reaction to a message. Attributes @@ -86,7 +84,7 @@ class MessageReactionAddEvent(APIObject, ChannelProperty): @dataclass(repr=False) -class MessageReactionRemoveEvent(APIObject, ChannelProperty): +class MessageReactionRemoveEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a user removes a reaction from a message. Attributes @@ -111,7 +109,7 @@ class MessageReactionRemoveEvent(APIObject, ChannelProperty): @dataclass(repr=False) -class MessageReactionRemoveAllEvent(APIObject, ChannelProperty): +class MessageReactionRemoveAllEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a user explicitly removes all reactions from a message. Attributes @@ -129,7 +127,7 @@ class MessageReactionRemoveAllEvent(APIObject, ChannelProperty): @dataclass(repr=False) -class MessageReactionRemoveEmojiEvent(APIObject, ChannelProperty): +class MessageReactionRemoveEmojiEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a bot removes all instances of a given emoji from the reactions of a message. diff --git a/pincer/objects/events/presence.py b/pincer/objects/events/presence.py index ba9205b2..bc082f6f 100644 --- a/pincer/objects/events/presence.py +++ b/pincer/objects/events/presence.py @@ -7,7 +7,7 @@ from enum import IntEnum from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.types import MISSING, APINullable if TYPE_CHECKING: @@ -242,7 +242,7 @@ class ClientStatus(APIObject): @dataclass(repr=False) -class PresenceUpdateEvent(APIObject): +class PresenceUpdateEvent(APIObject, GuildProperty): """This event is sent when a user's presence or info, such as name or avatar, is updated. diff --git a/pincer/objects/events/thread.py b/pincer/objects/events/thread.py index 160aa27d..ce3ed4dc 100644 --- a/pincer/objects/events/thread.py +++ b/pincer/objects/events/thread.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -18,7 +18,7 @@ @dataclass(repr=False) -class ThreadListSyncEvent(APIObject): +class ThreadListSyncEvent(APIObject, GuildProperty): """Sent when the current user gains access to a channel. Attributes @@ -46,7 +46,7 @@ class ThreadListSyncEvent(APIObject): @dataclass(repr=False) -class ThreadMembersUpdateEvent(APIObject): +class ThreadMembersUpdateEvent(APIObject, GuildProperty): """Sent when anyone is added to or removed from a thread. If the current user does not have the `GUILD_MEMBERS` Gateway Intent, then this event will only be sent if diff --git a/pincer/objects/events/typing_start.py b/pincer/objects/events/typing_start.py index e366272d..ea86f86c 100644 --- a/pincer/objects/events/typing_start.py +++ b/pincer/objects/events/typing_start.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import APIObject, ChannelProperty +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty from ...utils.types import APINullable, MISSING if TYPE_CHECKING: @@ -15,7 +15,7 @@ @dataclass(repr=False) -class TypingStartEvent(APIObject, ChannelProperty): +class TypingStartEvent(APIObject, ChannelProperty, GuildProperty): """Sent when a user starts typing in a channel. Attributes diff --git a/pincer/objects/events/voice.py b/pincer/objects/events/voice.py index f098cbd4..0b3b431d 100644 --- a/pincer/objects/events/voice.py +++ b/pincer/objects/events/voice.py @@ -7,7 +7,7 @@ from enum import Enum, auto from typing import TYPE_CHECKING -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty if TYPE_CHECKING: from typing import Optional, List @@ -16,7 +16,7 @@ @dataclass(repr=False) -class VoiceServerUpdateEvent(APIObject): +class VoiceServerUpdateEvent(APIObject, GuildProperty): """Sent when a guild's voice server is updated. This is sent when initially connecting to voice, and when the current voice instance fails over to a new server. @@ -37,7 +37,7 @@ class VoiceServerUpdateEvent(APIObject): @dataclass(repr=False) -class VoiceChannelSelectEvent(APIObject): +class VoiceChannelSelectEvent(APIObject, GuildProperty): """ Sent when the client joins a voice channel diff --git a/pincer/objects/guild/channel.py b/pincer/objects/guild/channel.py index 38170773..ebb74b50 100644 --- a/pincer/objects/guild/channel.py +++ b/pincer/objects/guild/channel.py @@ -10,7 +10,7 @@ from ..message.user_message import UserMessage from ..._config import GatewayConfig -from ...utils.api_object import APIObject +from ...utils.api_object import APIObject, GuildProperty from ...utils.conversion import construct_client_dict from ...utils.convert_message import convert_message from ...utils.types import MISSING @@ -76,7 +76,7 @@ class ChannelType(IntEnum): @dataclass(repr=False) -class Channel(APIObject): # noqa E501 +class Channel(APIObject, GuildProperty): # noqa E501 """Represents a Discord Channel Mention object Attributes diff --git a/pincer/objects/guild/stage.py b/pincer/objects/guild/stage.py index 42e900e1..c6db89a5 100644 --- a/pincer/objects/guild/stage.py +++ b/pincer/objects/guild/stage.py @@ -7,7 +7,7 @@ from enum import IntEnum from typing import TYPE_CHECKING -from ...utils.api_object import APIObject, ChannelProperty +from ...utils.api_object import APIObject, ChannelProperty, GuildProperty if TYPE_CHECKING: from ...utils.snowflake import Snowflake @@ -28,7 +28,7 @@ class PrivacyLevel(IntEnum): @dataclass(repr=False) -class StageInstance(APIObject, ChannelProperty): +class StageInstance(APIObject, ChannelProperty, GuildProperty): """Represents a Stage Instance object Attributes From 568668c9d85079abb214d1471412b2a543b615fd Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 15:36:59 -0500 Subject: [PATCH 14/34] :sparkles: added guild_ids property to Client --- pincer/client.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pincer/client.py b/pincer/client.py index b0350e8e..025997f7 100644 --- a/pincer/client.py +++ b/pincer/client.py @@ -208,6 +208,17 @@ def chat_commands(self) -> List[str]: ChatCommandHandler.register.values() )) + @property + def guild_ids(self) -> List[Snowflake]: + """ + Returns a list of Guilds that the client is a part of + + Returns + ------- + :class:`pincer.utils.snowflake.Snowflake` + """ + return self.guilds.keys() + @staticmethod def event(coroutine: Coro): """A decorator to register a Discord gateway event listener. From 62c96b42fdc8e34f9d3516e9af48c181dd323518 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 16:32:42 -0500 Subject: [PATCH 15/34] :art: codacity changes --- pincer/middleware/guild_member_remove.py | 2 +- pincer/middleware/guild_update.py | 3 +-- pincer/middleware/notification_create.py | 1 - pincer/middleware/thread_list_sync.py | 3 --- pincer/middleware/thread_members_update.py | 1 - pincer/middleware/voice_state_update.py | 2 +- pincer/objects/user/voice_state.py | 4 +--- 7 files changed, 4 insertions(+), 12 deletions(-) diff --git a/pincer/middleware/guild_member_remove.py b/pincer/middleware/guild_member_remove.py index 2991d6b0..6c9ac0c6 100644 --- a/pincer/middleware/guild_member_remove.py +++ b/pincer/middleware/guild_member_remove.py @@ -25,7 +25,7 @@ async def guild_member_remove_middleware(self, payload: GatewayDispatch): ------- Tuple[:class:`str`, :class:`~pincer.objects.events.guild.GuildMemberRemoveEvent`] ``on_guild_member_remove`` and a ``GuildMemberRemoveEvent`` - """ + """ return ( "on_guild_member_remove", diff --git a/pincer/middleware/guild_update.py b/pincer/middleware/guild_update.py index 4f26029c..fce4c8b1 100644 --- a/pincer/middleware/guild_update.py +++ b/pincer/middleware/guild_update.py @@ -2,10 +2,9 @@ # Full MIT License can be found in `LICENSE` at the project root. """sent when a guild is updated""" -from typing import List from ..core.dispatch import GatewayDispatch -from ..objects import Guild, Channel +from ..objects import Guild from ..utils.conversion import construct_client_dict diff --git a/pincer/middleware/notification_create.py b/pincer/middleware/notification_create.py index 1a3d1872..16d4ec7c 100644 --- a/pincer/middleware/notification_create.py +++ b/pincer/middleware/notification_create.py @@ -6,7 +6,6 @@ (mention or new message in eligible channels) """ -from pincer.utils.snowflake import Snowflake from ..core.dispatch import GatewayDispatch from ..objects.events.notification import NotificationCreateEvent from ..utils.conversion import construct_client_dict diff --git a/pincer/middleware/thread_list_sync.py b/pincer/middleware/thread_list_sync.py index cc9981ed..e388be67 100644 --- a/pincer/middleware/thread_list_sync.py +++ b/pincer/middleware/thread_list_sync.py @@ -7,10 +7,7 @@ from typing import List -from pincer.utils.types import MISSING - from ..core.dispatch import GatewayDispatch -from ..objects import Channel, ThreadMember from ..objects.events.thread import ThreadListSyncEvent from ..utils.conversion import construct_client_dict diff --git a/pincer/middleware/thread_members_update.py b/pincer/middleware/thread_members_update.py index fcaa5857..b1e45fc3 100644 --- a/pincer/middleware/thread_members_update.py +++ b/pincer/middleware/thread_members_update.py @@ -2,7 +2,6 @@ # Full MIT License can be found in `LICENSE` at the project root. """sent when anyone is added to or removed from a thread""" -from typing import List from ..core.dispatch import GatewayDispatch from ..objects.events.thread import ThreadMembersUpdateEvent diff --git a/pincer/middleware/voice_state_update.py b/pincer/middleware/voice_state_update.py index 3c03b76b..9d8fa0cb 100644 --- a/pincer/middleware/voice_state_update.py +++ b/pincer/middleware/voice_state_update.py @@ -10,7 +10,7 @@ from typing import TYPE_CHECKING from ..objects.user.voice_state import VoiceState -from ..utils import construct_client_dict, replace +from ..utils import construct_client_dict if TYPE_CHECKING: from typing import List, Tuple diff --git a/pincer/objects/user/voice_state.py b/pincer/objects/user/voice_state.py index fe5c01c4..eb1ecbc4 100644 --- a/pincer/objects/user/voice_state.py +++ b/pincer/objects/user/voice_state.py @@ -10,8 +10,6 @@ from ...utils.types import MISSING if TYPE_CHECKING: - from typing import Optional - from ..guild.member import GuildMember from ...utils.types import APINullable from ...utils.snowflake import Snowflake @@ -24,7 +22,7 @@ class VoiceState(APIObject, ChannelProperty, GuildProperty): Attributes ---------- - channel_id: Optional[:class:`~pincer.utils.snowflake.Snowflake`] + channel_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`] The channel id this user is connected to user_id: :class:`~pincer.utils.snowflake.Snowflake` The user id this voice state is for From ce0f3ecf8027757b83efc7f43578247443dc8c92 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 16:40:08 -0500 Subject: [PATCH 16/34] :memo: updated docs --- docs/api/utils.rst | 9 +++++++++ pincer/utils/__init__.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/api/utils.rst b/docs/api/utils.rst index 9519cb7f..f376e2d3 100644 --- a/docs/api/utils.rst +++ b/docs/api/utils.rst @@ -13,6 +13,15 @@ APIObject .. autoclass:: APIObject() +APIObject Properties +~~~~~~~~~~~~~~~~~~~~ + +.. autoclass:: ChannelProperty() + +.. autoclass:: GuildProperty() + + + Directory --------- diff --git a/pincer/utils/__init__.py b/pincer/utils/__init__.py index 8b47e444..9fab52fc 100644 --- a/pincer/utils/__init__.py +++ b/pincer/utils/__init__.py @@ -1,7 +1,7 @@ # Copyright Pincer 2021-Present # Full MIT License can be found in `LICENSE` at the project root. -from .api_object import APIObject, HTTPMeta +from .api_object import APIObject, HTTPMeta, ChannelProperty, GuildProperty from .color import Color from .conversion import convert, construct_client_dict from .event_mgr import EventMgr @@ -24,5 +24,5 @@ "EventMgr", "HTTPMeta", "MISSING", "MissingType", "Snowflake", "Task", "TaskScheduler", "Timestamp", "chdir", "choice_value_types", "convert", "get_index", "get_params", "get_signature_and_params", "should_pass_cls", - "should_pass_ctx", "replace", "construct_client_dict" + "should_pass_ctx", "replace", "construct_client_dict", "ChannelProperty", "GuildProperty" ) From ef999b517e261ee195b759fc28b142b432611058 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 16:41:52 -0500 Subject: [PATCH 17/34] :speech_balloon: fix typo --- pincer/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pincer/client.py b/pincer/client.py index 025997f7..8596dbfc 100644 --- a/pincer/client.py +++ b/pincer/client.py @@ -190,7 +190,8 @@ def __init__( self.throttler = throttler self.event_mgr = EventMgr() - # The guild and channel value is only registered if the GUILD intent. + # The guild and channel value is only registered if the Client has the GUILDS + # intent. self.guilds: Dict[Snowflake, Optional[Guild]] = {} self.channels: Dict[Snowflake, Optional[Channel]] = {} From c68e9a1076becde475189d81c2be289bb302a933 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 16:45:30 -0500 Subject: [PATCH 18/34] :technologist: made Intents easier to use --- pincer/client.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pincer/client.py b/pincer/client.py index 8596dbfc..f4c07218 100644 --- a/pincer/client.py +++ b/pincer/client.py @@ -8,7 +8,7 @@ from collections import defaultdict from importlib import import_module from inspect import isasyncgenfunction -from typing import Any, Dict, List, Optional, Tuple, Union, overload +from typing import Any, Dict, Iterable, List, Optional, Tuple, Union, overload from typing import TYPE_CHECKING from . import __package__ @@ -153,8 +153,8 @@ class Client(Dispatcher): received : Optional[:class:`str`] The default message which will be sent when no response is given. |default| :data:`None` - intents : Optional[:class:`~objects.app.intents.Intents`] - The discord intents to use |default| :data:`None` + intents : Optional[Union[Iterable[:class:`~objects.app.intents.Intents`], :class:`~objects.app.intents.Intents`]] + The discord intents to use |default| :data:`Intents.all()` throttler : Optional[:class:`~objects.app.throttling.ThrottleInterface`] The cooldown handler for your client, defaults to :class:`~.objects.app.throttling.DefaultThrottleHandler` @@ -162,16 +162,20 @@ class Client(Dispatcher): Custom throttlers must derive from :class:`~pincer.objects.app.throttling.ThrottleInterface`. |default| :class:`~pincer.objects.app.throttling.DefaultThrottleHandler` - """ + """ # noqa: E501 def __init__( self, token: str, *, received: str = None, - intents: Intents = None, + intents: Union[Iterable, Intents] = None, throttler: ThrottleInterface = DefaultThrottleHandler, reconnect: bool = True, ): + + if isinstance(intents, Iterable): + intents = sum(intents) + super().__init__( token, handlers={ @@ -180,7 +184,7 @@ def __init__( # Use this event handler for opcode 0. 0: self.event_handler }, - intents=intents or Intents.NONE, + intents=intents or Intents.all(), reconnect=reconnect, ) From 354249213317a00fb7d4329d10aa1ab4bd4b3b4d Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sat, 11 Dec 2021 16:49:03 -0500 Subject: [PATCH 19/34] :art: ran black on middleware --- pincer/middleware/__init__.py | 3 +- pincer/middleware/activity_join.py | 2 +- pincer/middleware/activity_join_request.py | 2 +- pincer/middleware/channel_create.py | 7 ++-- pincer/middleware/channel_delete.py | 4 ++- pincer/middleware/channel_pins_update.py | 9 +++-- pincer/middleware/channel_update.py | 2 +- pincer/middleware/error.py | 5 ++- pincer/middleware/guild_ban_remove.py | 4 ++- pincer/middleware/guild_emojis_update.py | 5 +-- .../middleware/guild_integrations_update.py | 2 +- pincer/middleware/guild_member_add.py | 4 ++- pincer/middleware/guild_members_chunk.py | 2 +- pincer/middleware/guild_role_create.py | 9 +++-- pincer/middleware/guild_role_delete.py | 20 ++++++----- pincer/middleware/guild_role_update.py | 9 +++-- pincer/middleware/guild_status.py | 2 +- pincer/middleware/guild_stickers_update.py | 5 +-- pincer/middleware/integration_create.py | 2 +- pincer/middleware/integration_delete.py | 2 +- pincer/middleware/integration_update.py | 2 +- pincer/middleware/interaction_create.py | 36 +++++++++---------- pincer/middleware/invite_create.py | 2 +- pincer/middleware/invite_delete.py | 2 +- pincer/middleware/message_create.py | 5 ++- pincer/middleware/message_delete.py | 5 ++- pincer/middleware/message_delete_bulk.py | 2 +- pincer/middleware/message_reaction_add.py | 7 ++-- pincer/middleware/message_reaction_remove.py | 7 ++-- .../middleware/message_reaction_remove_all.py | 5 ++- .../message_reaction_remove_emoji.py | 7 ++-- pincer/middleware/message_update.py | 5 ++- pincer/middleware/notification_create.py | 2 +- pincer/middleware/payload.py | 2 +- pincer/middleware/presence_update.py | 4 ++- pincer/middleware/ready.py | 7 ++-- pincer/middleware/speaking_start.py | 2 +- pincer/middleware/speaking_stop.py | 2 +- pincer/middleware/thread_create.py | 4 ++- pincer/middleware/thread_delete.py | 4 ++- pincer/middleware/thread_list_sync.py | 4 ++- pincer/middleware/thread_member_update.py | 18 ++++++---- pincer/middleware/thread_members_update.py | 4 ++- pincer/middleware/thread_update.py | 2 +- pincer/middleware/typing_start.py | 2 +- pincer/middleware/user_update.py | 2 +- pincer/middleware/voice_channel_select.py | 2 +- pincer/middleware/voice_connection_status.py | 2 +- pincer/middleware/voice_server_update.py | 2 +- pincer/middleware/voice_settings_update.py | 2 +- pincer/middleware/voice_state_create.py | 2 +- pincer/middleware/voice_state_delete.py | 2 +- pincer/middleware/voice_state_update.py | 7 ++-- pincer/middleware/webhooks_update.py | 4 ++- 54 files changed, 137 insertions(+), 130 deletions(-) diff --git a/pincer/middleware/__init__.py b/pincer/middleware/__init__.py index 989877e3..5fde1175 100644 --- a/pincer/middleware/__init__.py +++ b/pincer/middleware/__init__.py @@ -31,8 +31,7 @@ def get_middleware() -> Dict[str, Coro]: try: middleware_list[event] = getattr( - import_module(f".{event}", package=__name__), - "export" + import_module(f".{event}", package=__name__), "export" )() except AttributeError: _log.warning( diff --git a/pincer/middleware/activity_join.py b/pincer/middleware/activity_join.py index ea07c879..a4e5326f 100644 --- a/pincer/middleware/activity_join.py +++ b/pincer/middleware/activity_join.py @@ -29,7 +29,7 @@ async def activity_join_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_activity_join", - ActivityJoinEvent.from_dict(construct_client_dict(self, payload.data)) + ActivityJoinEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/activity_join_request.py b/pincer/middleware/activity_join_request.py index 4aa66059..03514e9d 100644 --- a/pincer/middleware/activity_join_request.py +++ b/pincer/middleware/activity_join_request.py @@ -26,7 +26,7 @@ async def activity_join_request_middleware(self, payload: GatewayDispatch): """ return ( "on_activity_join_request", - User.from_dict(construct_client_dict(self, payload.data)) + User.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/channel_create.py b/pincer/middleware/channel_create.py index c6a5a2ba..948feb80 100644 --- a/pincer/middleware/channel_create.py +++ b/pincer/middleware/channel_create.py @@ -15,8 +15,7 @@ async def channel_create_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, Channel]: """|coro| @@ -33,7 +32,9 @@ async def channel_create_middleware( ``on_channel_creation`` and a channel. """ - channel: Channel = Channel.from_dict(construct_client_dict(self, payload.data)) + channel: Channel = Channel.from_dict( + construct_client_dict(self, payload.data) + ) self.guilds[channel.guild_id].channels.append(channel) self.channels[channel.id] = channel diff --git a/pincer/middleware/channel_delete.py b/pincer/middleware/channel_delete.py index a46f5f02..b3f62c78 100644 --- a/pincer/middleware/channel_delete.py +++ b/pincer/middleware/channel_delete.py @@ -28,7 +28,9 @@ async def channel_delete_middleware(self, payload: GatewayDispatch): guild = self.guilds.get(channel.guild_id) if guild: - guild.channels = list(filter(lambda c: c.id != channel.id, guild.channels)) + guild.channels = list( + filter(lambda c: c.id != channel.id, guild.channels) + ) self.channels.pop(channel.id, None) diff --git a/pincer/middleware/channel_pins_update.py b/pincer/middleware/channel_pins_update.py index 7c8cb935..62de1c85 100644 --- a/pincer/middleware/channel_pins_update.py +++ b/pincer/middleware/channel_pins_update.py @@ -24,12 +24,11 @@ async def channel_pins_update_middleware(self, payload: GatewayDispatch): """ event = ChannelPinsUpdateEvent.from_dict(payload.data) - self.channels[event.channel_id].last_pin_timestamp = event.last_pin_timestamp + self.channels[ + event.channel_id + ].last_pin_timestamp = event.last_pin_timestamp - return ( - "on_channel_pins_update", - event - ) + return ("on_channel_pins_update", event) def export(): diff --git a/pincer/middleware/channel_update.py b/pincer/middleware/channel_update.py index 9538f571..b1ae55da 100644 --- a/pincer/middleware/channel_update.py +++ b/pincer/middleware/channel_update.py @@ -32,7 +32,7 @@ async def channel_update_middleware(self, payload: GatewayDispatch): guild.channels = replace( lambda _channel: _channel.id == channel.id, self.guilds[channel.guild_id].channels, - channel + channel, ) self.channels[channel.id] = channel diff --git a/pincer/middleware/error.py b/pincer/middleware/error.py index 426ed74d..df28b7c1 100644 --- a/pincer/middleware/error.py +++ b/pincer/middleware/error.py @@ -19,8 +19,7 @@ def error_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, DiscordError]: """|coro| @@ -40,7 +39,7 @@ def error_middleware( return ( "on_error", - DiscordError.from_dict(construct_client_dict(self, payload.data)) + DiscordError.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/guild_ban_remove.py b/pincer/middleware/guild_ban_remove.py index b9dbbc71..04bf5ceb 100644 --- a/pincer/middleware/guild_ban_remove.py +++ b/pincer/middleware/guild_ban_remove.py @@ -27,7 +27,9 @@ async def guild_ban_remove_middleware(self, payload: GatewayDispatch): return ( "on_guild_ban_remove", - GuildBanRemoveEvent.from_dict(construct_client_dict(self, payload.data)) + GuildBanRemoveEvent.from_dict( + construct_client_dict(self, payload.data) + ), ) diff --git a/pincer/middleware/guild_emojis_update.py b/pincer/middleware/guild_emojis_update.py index c5a8e35e..5872f2b9 100644 --- a/pincer/middleware/guild_emojis_update.py +++ b/pincer/middleware/guild_emojis_update.py @@ -33,10 +33,7 @@ async def guild_emojis_update_middleware(self, payload: GatewayDispatch): if guild: guild.emojis = event.emojis - return ( - "on_guild_emojis_update", - event - ) + return ("on_guild_emojis_update", event) def export() -> Coro: diff --git a/pincer/middleware/guild_integrations_update.py b/pincer/middleware/guild_integrations_update.py index dadb6027..dad54667 100644 --- a/pincer/middleware/guild_integrations_update.py +++ b/pincer/middleware/guild_integrations_update.py @@ -29,7 +29,7 @@ async def guild_integrations_update_middleware(self, payload: GatewayDispatch): "on_guild_integrations_update", GuildIntegrationsUpdateEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/guild_member_add.py b/pincer/middleware/guild_member_add.py index 25743f8a..c5fc54d9 100644 --- a/pincer/middleware/guild_member_add.py +++ b/pincer/middleware/guild_member_add.py @@ -30,7 +30,9 @@ async def guild_member_add_middleware(self, payload: GatewayDispatch): return ( "on_guild_member_add", - GuildMemberAddEvent.from_dict(construct_client_dict(self, payload.data)) + GuildMemberAddEvent.from_dict( + construct_client_dict(self, payload.data) + ), ) diff --git a/pincer/middleware/guild_members_chunk.py b/pincer/middleware/guild_members_chunk.py index c3057e80..c074c1d2 100644 --- a/pincer/middleware/guild_members_chunk.py +++ b/pincer/middleware/guild_members_chunk.py @@ -32,7 +32,7 @@ async def guild_member_chunk_middleware(self, payload: GatewayDispatch): "on_guild_member_chunk", GuildMembersChunkEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/guild_role_create.py b/pincer/middleware/guild_role_create.py index b243e748..454aaca1 100644 --- a/pincer/middleware/guild_role_create.py +++ b/pincer/middleware/guild_role_create.py @@ -25,16 +25,15 @@ async def guild_role_create_middleware(self, payload: GatewayDispatch): ``on_guild_role_create`` and a ``GuildRoleCreateEvent`` """ # noqa: E501 - event = GuildRoleCreateEvent.from_dict(construct_client_dict(self, payload.data)) + event = GuildRoleCreateEvent.from_dict( + construct_client_dict(self, payload.data) + ) guild = self.guilds.get(event.guild_id) if guild: guild.roles.append(event.role) - return ( - "on_guild_role_create", - event - ) + return ("on_guild_role_create", event) def export() -> Coro: diff --git a/pincer/middleware/guild_role_delete.py b/pincer/middleware/guild_role_delete.py index 730cdf3c..e8ec17ef 100644 --- a/pincer/middleware/guild_role_delete.py +++ b/pincer/middleware/guild_role_delete.py @@ -25,19 +25,21 @@ async def guild_role_delete_middleware(self, payload: GatewayDispatch): ``on_guild_role_delete`` and a ``GuildRoleDeleteEvent`` """ # noqa: E501 - event = GuildRoleDeleteEvent.from_dict(construct_client_dict(self, payload.data)) + event = GuildRoleDeleteEvent.from_dict( + construct_client_dict(self, payload.data) + ) guild = self.guilds.get(event.guild_id) if guild: - guild.roles = list(filter( - lambda role: role.id != event.role_id, self.guilds[event.guild_id].roles - )) - - return ( - "on_guild_role_delete", - event - ) + guild.roles = list( + filter( + lambda role: role.id != event.role_id, + self.guilds[event.guild_id].roles, + ) + ) + + return ("on_guild_role_delete", event) def export() -> Coro: diff --git a/pincer/middleware/guild_role_update.py b/pincer/middleware/guild_role_update.py index 00bb7e8c..5f5eb283 100644 --- a/pincer/middleware/guild_role_update.py +++ b/pincer/middleware/guild_role_update.py @@ -25,7 +25,9 @@ async def guild_role_update_middleware(self, payload: GatewayDispatch): ``on_guild_role_update`` and a ``GuildRoleUpdateEvent`` """ - event = GuildRoleUpdateEvent.from_dict(construct_client_dict(self, payload.data)) + event = GuildRoleUpdateEvent.from_dict( + construct_client_dict(self, payload.data) + ) guild = self.guilds.get(event.guild_id) @@ -35,10 +37,7 @@ async def guild_role_update_middleware(self, payload: GatewayDispatch): for role in guild.roles ] - return ( - "on_guild_role_update", - event - ) + return ("on_guild_role_update", event) def export() -> Coro: diff --git a/pincer/middleware/guild_status.py b/pincer/middleware/guild_status.py index 447b1537..1b4fb1b7 100644 --- a/pincer/middleware/guild_status.py +++ b/pincer/middleware/guild_status.py @@ -26,7 +26,7 @@ async def guild_status_middleware(self, payload: GatewayDispatch): """ return ( "on_guild_status", - GuildStatusEvent.from_dict(construct_client_dict(self, payload.data)) + GuildStatusEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/guild_stickers_update.py b/pincer/middleware/guild_stickers_update.py index 57c20520..5fdcf2ea 100644 --- a/pincer/middleware/guild_stickers_update.py +++ b/pincer/middleware/guild_stickers_update.py @@ -34,10 +34,7 @@ async def guild_stickers_update_middleware(self, payload: GatewayDispatch): if guild: guild.stickers = event.stickers - return ( - "on_guild_stickers_update", - event - ) + return ("on_guild_stickers_update", event) def export() -> Coro: diff --git a/pincer/middleware/integration_create.py b/pincer/middleware/integration_create.py index 9b0a6b77..432d19bd 100644 --- a/pincer/middleware/integration_create.py +++ b/pincer/middleware/integration_create.py @@ -28,7 +28,7 @@ async def integration_create_middleware(self, payload: GatewayDispatch): "on_integration_create", IntegrationCreateEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/integration_delete.py b/pincer/middleware/integration_delete.py index b37b39a3..78923f51 100644 --- a/pincer/middleware/integration_delete.py +++ b/pincer/middleware/integration_delete.py @@ -28,7 +28,7 @@ async def integration_delete_middleware(self, payload: GatewayDispatch): "on_integration_delete", IntegrationDeleteEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/integration_update.py b/pincer/middleware/integration_update.py index 899e243a..614af1bd 100644 --- a/pincer/middleware/integration_update.py +++ b/pincer/middleware/integration_update.py @@ -28,7 +28,7 @@ async def integration_update_middleware(self, payload: GatewayDispatch): "on_integration_update", IntegrationUpdateEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/interaction_create.py b/pincer/middleware/interaction_create.py index 2da314cb..7c3f0b30 100644 --- a/pincer/middleware/interaction_create.py +++ b/pincer/middleware/interaction_create.py @@ -25,12 +25,12 @@ async def interaction_response_handler( - self, - command: Coro, - context: MessageContext, - interaction: Interaction, - args: List[Any], - kwargs: Dict[str, Any] + self, + command: Coro, + context: MessageContext, + interaction: Interaction, + args: List[Any], + kwargs: Dict[str, Any], ): """|coro| @@ -69,10 +69,7 @@ async def interaction_response_handler( async def interaction_handler( - self, - interaction: Interaction, - context: MessageContext, - command: Coro + self, interaction: Interaction, context: MessageContext, command: Coro ): """|coro| @@ -91,14 +88,15 @@ async def interaction_handler( sig, _ = get_signature_and_params(command) - defaults = {key: value.default for key, - value in sig.items() if value.default is not _empty} + defaults = { + key: value.default + for key, value in sig.items() + if value.default is not _empty + } params = {} if interaction.data.options is not MISSING: - params = { - opt.name: opt.value for opt in interaction.data.options - } + params = {opt.name: opt.value for opt in interaction.data.options} args = [] @@ -125,8 +123,7 @@ async def interaction_handler( async def interaction_create_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, Interaction]: """Middleware for ``on_interaction``, which handles command execution. @@ -157,8 +154,7 @@ async def interaction_create_middleware( context = interaction.convert_to_message_context(command) try: - await interaction_handler(self, interaction, context, - command.call) + await interaction_handler(self, interaction, context, command.call) except Exception as e: if coro := get_index(self.get_event_coro("on_command_error"), 0): params = get_signature_and_params(coro)[1] @@ -171,7 +167,7 @@ async def interaction_create_middleware( context, interaction, # Always take the error parameter its name. - {params[(len(params) - 1) or 0]: e} + {params[(len(params) - 1) or 0]: e}, ) else: raise e diff --git a/pincer/middleware/invite_create.py b/pincer/middleware/invite_create.py index 6df12ce1..538015d8 100644 --- a/pincer/middleware/invite_create.py +++ b/pincer/middleware/invite_create.py @@ -26,7 +26,7 @@ async def invite_create_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_invite_create", - InviteCreateEvent.from_dict(construct_client_dict(self, payload.data)) + InviteCreateEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/invite_delete.py b/pincer/middleware/invite_delete.py index efe943cc..3a4a0a7b 100644 --- a/pincer/middleware/invite_delete.py +++ b/pincer/middleware/invite_delete.py @@ -26,7 +26,7 @@ async def invite_delete_middleware(self, payload: GatewayDispatch): """ return ( "on_invite_delete", - InviteDeleteEvent.from_dict(construct_client_dict(self, payload.data)) + InviteDeleteEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/message_create.py b/pincer/middleware/message_create.py index 05bbb718..7da9e3a3 100644 --- a/pincer/middleware/message_create.py +++ b/pincer/middleware/message_create.py @@ -16,8 +16,7 @@ async def message_create_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, UserMessage]: # noqa: E501 """|coro| @@ -35,7 +34,7 @@ async def message_create_middleware( """ # noqa: E501 return ( "on_message", - UserMessage.from_dict(construct_client_dict(self, payload.data)) + UserMessage.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/message_delete.py b/pincer/middleware/message_delete.py index 2b649921..3746a222 100644 --- a/pincer/middleware/message_delete.py +++ b/pincer/middleware/message_delete.py @@ -16,8 +16,7 @@ async def on_message_delete_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, MessageDeleteEvent]: """|coro| Middleware for the ``on_message_delete`` event. @@ -35,7 +34,7 @@ async def on_message_delete_middleware( return ( "on_message_delete", - MessageDeleteEvent.from_dict(construct_client_dict(self, payload.data)) + MessageDeleteEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/message_delete_bulk.py b/pincer/middleware/message_delete_bulk.py index 11b44b44..4a00a557 100644 --- a/pincer/middleware/message_delete_bulk.py +++ b/pincer/middleware/message_delete_bulk.py @@ -28,7 +28,7 @@ async def message_delete_bulk_middleware(self, payload: GatewayDispatch): "on_message_delete_bulk", MessageDeleteBulkEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/message_reaction_add.py b/pincer/middleware/message_reaction_add.py index ab0872d0..4deb8558 100644 --- a/pincer/middleware/message_reaction_add.py +++ b/pincer/middleware/message_reaction_add.py @@ -37,9 +37,10 @@ async def message_reaction_add_middleware(self, payload: GatewayDispatch): "emoji": Emoji.from_dict( construct_client_dict(self, payload.data.pop("emoji")) ), - **payload.data - } - )) + **payload.data, + }, + ) + ), ) diff --git a/pincer/middleware/message_reaction_remove.py b/pincer/middleware/message_reaction_remove.py index 83b5bbc2..d25f7fd1 100644 --- a/pincer/middleware/message_reaction_remove.py +++ b/pincer/middleware/message_reaction_remove.py @@ -35,9 +35,10 @@ async def message_reaction_remove_middleware(self, payload: GatewayDispatch): "emoji": Emoji.from_dict( construct_client_dict(self, payload.data.pop("emoji")) ), - **payload.data - } - )) + **payload.data, + }, + ) + ), ) diff --git a/pincer/middleware/message_reaction_remove_all.py b/pincer/middleware/message_reaction_remove_all.py index 1f6cbdbf..f2247dc2 100644 --- a/pincer/middleware/message_reaction_remove_all.py +++ b/pincer/middleware/message_reaction_remove_all.py @@ -9,8 +9,7 @@ async def message_reaction_remove_all_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ): """|coro| @@ -32,7 +31,7 @@ async def message_reaction_remove_all_middleware( "on_message_reaction_remove_all", MessageReactionRemoveAllEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/message_reaction_remove_emoji.py b/pincer/middleware/message_reaction_remove_emoji.py index e12a88f9..4e6bae6b 100644 --- a/pincer/middleware/message_reaction_remove_emoji.py +++ b/pincer/middleware/message_reaction_remove_emoji.py @@ -37,9 +37,10 @@ async def message_reaction_remove_emoji_middleware( "emoji": Emoji.from_dict( construct_client_dict(self, payload.data.pop("emoji")) ), - **payload.data - } - )) + **payload.data, + }, + ) + ), ) diff --git a/pincer/middleware/message_update.py b/pincer/middleware/message_update.py index c9f481b2..1ffd3a01 100644 --- a/pincer/middleware/message_update.py +++ b/pincer/middleware/message_update.py @@ -17,8 +17,7 @@ async def message_update_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, UserMessage]: """|coro| @@ -38,7 +37,7 @@ async def message_update_middleware( """ # noqa: E501 return ( "on_message_update", - UserMessage.from_dict(construct_client_dict(self, payload.data)) + UserMessage.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/notification_create.py b/pincer/middleware/notification_create.py index 16d4ec7c..aadea9ad 100644 --- a/pincer/middleware/notification_create.py +++ b/pincer/middleware/notification_create.py @@ -33,7 +33,7 @@ async def notification_create_middleware(self, payload: GatewayDispatch): "on_notification_create", NotificationCreateEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/payload.py b/pincer/middleware/payload.py index e746e7a4..5016492d 100644 --- a/pincer/middleware/payload.py +++ b/pincer/middleware/payload.py @@ -12,7 +12,7 @@ async def payload_middleware( - payload: GatewayDispatch + payload: GatewayDispatch, ) -> Tuple[str, GatewayDispatch]: """Invoked when anything is received from gateway. diff --git a/pincer/middleware/presence_update.py b/pincer/middleware/presence_update.py index 2db95649..fee94856 100644 --- a/pincer/middleware/presence_update.py +++ b/pincer/middleware/presence_update.py @@ -26,7 +26,9 @@ async def presence_update_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_presence_update", - PresenceUpdateEvent.from_dict(construct_client_dict(self, payload.data)) + PresenceUpdateEvent.from_dict( + construct_client_dict(self, payload.data) + ), ) diff --git a/pincer/middleware/ready.py b/pincer/middleware/ready.py index ae465297..6624de30 100644 --- a/pincer/middleware/ready.py +++ b/pincer/middleware/ready.py @@ -20,10 +20,7 @@ from ..core.dispatch import GatewayDispatch -async def on_ready_middleware( - self, - payload: GatewayDispatch -) -> Tuple[str]: +async def on_ready_middleware(self, payload: GatewayDispatch) -> Tuple[str]: """|coro| Middleware for the ``on_ready`` event. @@ -51,7 +48,7 @@ async def on_ready_middleware( self.guilds = dict(map(lambda i: (i["id"], None), guilds)) await ChatCommandHandler(self).initialize() - return "on_ready", + return ("on_ready",) def export() -> Coro: diff --git a/pincer/middleware/speaking_start.py b/pincer/middleware/speaking_start.py index 14a3273f..358dc812 100644 --- a/pincer/middleware/speaking_start.py +++ b/pincer/middleware/speaking_start.py @@ -26,7 +26,7 @@ async def speaking_start_middleware(self, payload: GatewayDispatch): """ return ( "on_speaking_start", - SpeakingStartEvent.from_dict(construct_client_dict(self, payload.data)) + SpeakingStartEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/speaking_stop.py b/pincer/middleware/speaking_stop.py index 65d6dba4..bbf2d110 100644 --- a/pincer/middleware/speaking_stop.py +++ b/pincer/middleware/speaking_stop.py @@ -26,7 +26,7 @@ async def speaking_stop_middleware(self, payload: GatewayDispatch): """ return ( "on_speaking_stop", - SpeakingStopEvent.from_dict(construct_client_dict(self, payload.data)) + SpeakingStopEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/thread_create.py b/pincer/middleware/thread_create.py index 29154bee..e7b25031 100644 --- a/pincer/middleware/thread_create.py +++ b/pincer/middleware/thread_create.py @@ -24,7 +24,9 @@ async def thread_create_middleware(self, payload: GatewayDispatch): ``on_thread_create`` and an ``Channel`` """ - channel: Channel = Channel.from_dict(construct_client_dict(self, payload.data)) + channel: Channel = Channel.from_dict( + construct_client_dict(self, payload.data) + ) if self.guilds[channel.guild_id].threads: self.guilds[channel.guild_id].threads.append(channel) diff --git a/pincer/middleware/thread_delete.py b/pincer/middleware/thread_delete.py index 8c67921d..eeceea6d 100644 --- a/pincer/middleware/thread_delete.py +++ b/pincer/middleware/thread_delete.py @@ -28,7 +28,9 @@ async def thread_delete_middleware(self, payload: GatewayDispatch): guild = self.guilds.get(channel.guild_id) if guild: - guild.threads = list(filter(lambda c: c.id != channel.id, guild.threads)) + guild.threads = list( + filter(lambda c: c.id != channel.id, guild.threads) + ) self.channels.pop(channel.id, None) diff --git a/pincer/middleware/thread_list_sync.py b/pincer/middleware/thread_list_sync.py index e388be67..1e17b218 100644 --- a/pincer/middleware/thread_list_sync.py +++ b/pincer/middleware/thread_list_sync.py @@ -28,7 +28,9 @@ async def thread_list_sync(self, payload: GatewayDispatch): ``on_thread_list_sync`` and an ``ThreadListSyncEvent`` """ # noqa: E501 - event = ThreadListSyncEvent.from_dict(construct_client_dict(self, payload.data)) + event = ThreadListSyncEvent.from_dict( + construct_client_dict(self, payload.data) + ) guild = self.guilds.get(event.guild_id) if guild: diff --git a/pincer/middleware/thread_member_update.py b/pincer/middleware/thread_member_update.py index 60c1c5d0..6c723acf 100644 --- a/pincer/middleware/thread_member_update.py +++ b/pincer/middleware/thread_member_update.py @@ -27,13 +27,17 @@ async def thread_member_update_middleware(self, payload: GatewayDispatch): return ( "on_thread_member_update", - ThreadMember.from_dict(construct_client_dict( - self, - { - "join_timestamp": Timestamp(payload.data.pop("join_timestamp")), - **payload.data - } - )) + ThreadMember.from_dict( + construct_client_dict( + self, + { + "join_timestamp": Timestamp( + payload.data.pop("join_timestamp") + ), + **payload.data, + }, + ) + ), ) diff --git a/pincer/middleware/thread_members_update.py b/pincer/middleware/thread_members_update.py index b1e45fc3..34a94b1d 100644 --- a/pincer/middleware/thread_members_update.py +++ b/pincer/middleware/thread_members_update.py @@ -26,7 +26,9 @@ async def thread_members_update_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_thread_members_update", - ThreadMembersUpdateEvent.from_dict(construct_client_dict(self, payload.data)) + ThreadMembersUpdateEvent.from_dict( + construct_client_dict(self, payload.data) + ), ) diff --git a/pincer/middleware/thread_update.py b/pincer/middleware/thread_update.py index e201b9e9..c6549b6f 100644 --- a/pincer/middleware/thread_update.py +++ b/pincer/middleware/thread_update.py @@ -33,7 +33,7 @@ async def thread_update_middleware(self, payload: GatewayDispatch): guild.threads = replace( lambda _channel: _channel.id == channel.id, self.guilds[channel.guild_id].threads, - channel + channel, ) self.channels[channel.id] = channel diff --git a/pincer/middleware/typing_start.py b/pincer/middleware/typing_start.py index 709212a7..29d8652a 100644 --- a/pincer/middleware/typing_start.py +++ b/pincer/middleware/typing_start.py @@ -26,7 +26,7 @@ async def typing_start_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_typing_start", - TypingStartEvent.from_dict(construct_client_dict(self, payload.data)) + TypingStartEvent.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/user_update.py b/pincer/middleware/user_update.py index 1b0011ea..f47f6eea 100644 --- a/pincer/middleware/user_update.py +++ b/pincer/middleware/user_update.py @@ -26,7 +26,7 @@ async def user_update_middleware(self, payload: GatewayDispatch): """ return ( "on_user_update", - User.from_dict(construct_client_dict(self, payload.data)) + User.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/voice_channel_select.py b/pincer/middleware/voice_channel_select.py index 9a1d4405..042fafd7 100644 --- a/pincer/middleware/voice_channel_select.py +++ b/pincer/middleware/voice_channel_select.py @@ -28,7 +28,7 @@ async def voice_channel_select_middleware(self, payload: GatewayDispatch): "on_voice_channel_select", VoiceChannelSelectEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/voice_connection_status.py b/pincer/middleware/voice_connection_status.py index 7797f49f..1df4c38c 100644 --- a/pincer/middleware/voice_connection_status.py +++ b/pincer/middleware/voice_connection_status.py @@ -28,7 +28,7 @@ async def voice_connection_status_middleware(self, payload: GatewayDispatch): "on_voice_connection_status", VoiceConnectionStatusEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/voice_server_update.py b/pincer/middleware/voice_server_update.py index ad0eb477..8acc94b9 100644 --- a/pincer/middleware/voice_server_update.py +++ b/pincer/middleware/voice_server_update.py @@ -28,7 +28,7 @@ async def voice_server_update_middleware(self, payload: GatewayDispatch): "on_voice_server_update", VoiceServerUpdateEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/voice_settings_update.py b/pincer/middleware/voice_settings_update.py index 7eb9350c..9e6adeaa 100644 --- a/pincer/middleware/voice_settings_update.py +++ b/pincer/middleware/voice_settings_update.py @@ -28,7 +28,7 @@ async def voice_settings_update_middleware(self, payload: GatewayDispatch): "on_voice_settings_update", VoiceSettingsUpdateEvent.from_dict( construct_client_dict(self, payload.data) - ) + ), ) diff --git a/pincer/middleware/voice_state_create.py b/pincer/middleware/voice_state_create.py index f3dd3484..8f908be3 100644 --- a/pincer/middleware/voice_state_create.py +++ b/pincer/middleware/voice_state_create.py @@ -26,7 +26,7 @@ async def voice_state_create_middleware(self, payload: GatewayDispatch): """ return ( "on_voice_state_create", - VoiceState.from_dict(construct_client_dict(self, payload.data)) + VoiceState.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/voice_state_delete.py b/pincer/middleware/voice_state_delete.py index b6661f38..4b63d202 100644 --- a/pincer/middleware/voice_state_delete.py +++ b/pincer/middleware/voice_state_delete.py @@ -26,7 +26,7 @@ async def voice_state_delete_middleware(self, payload: GatewayDispatch): """ return ( "on_voice_state_delete", - VoiceState.from_dict(construct_client_dict(self, payload.data)) + VoiceState.from_dict(construct_client_dict(self, payload.data)), ) diff --git a/pincer/middleware/voice_state_update.py b/pincer/middleware/voice_state_update.py index 9d8fa0cb..50869e54 100644 --- a/pincer/middleware/voice_state_update.py +++ b/pincer/middleware/voice_state_update.py @@ -19,8 +19,7 @@ async def voice_state_update_middleware( - self, - payload: GatewayDispatch + self, payload: GatewayDispatch ) -> Tuple[str, List[VoiceState]]: """|coro| Middleware for the ``on_voice_state_update`` event. @@ -36,7 +35,9 @@ async def voice_state_update_middleware( ``on_voice_state_update`` and a ``VoiceState`` """ # noqa: E501 - voice_state = VoiceState.from_dict(construct_client_dict(self, payload.data)) + voice_state = VoiceState.from_dict( + construct_client_dict(self, payload.data) + ) guild = self.guilds.get(voice_state.guild_id) diff --git a/pincer/middleware/webhooks_update.py b/pincer/middleware/webhooks_update.py index 40cba900..2b940671 100644 --- a/pincer/middleware/webhooks_update.py +++ b/pincer/middleware/webhooks_update.py @@ -28,7 +28,9 @@ async def webhooks_update_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_webhooks_update", - WebhooksUpdateEvent.from_dict(construct_client_dict(self, payload.data)) + WebhooksUpdateEvent.from_dict( + construct_client_dict(self, payload.data) + ), ) From dbf1c63242bc2aa11a309c54db076e5e00b699ca Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 00:30:13 -0500 Subject: [PATCH 20/34] :memo: maked guild_ids return type as List[Snowflake] --- pincer/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pincer/client.py b/pincer/client.py index f4c07218..fd967afd 100644 --- a/pincer/client.py +++ b/pincer/client.py @@ -220,7 +220,7 @@ def guild_ids(self) -> List[Snowflake]: Returns ------- - :class:`pincer.utils.snowflake.Snowflake` + List[:class:`pincer.utils.snowflake.Snowflake`] """ return self.guilds.keys() From 5293c72845a845dd68f3d283720512c7facbd9eb Mon Sep 17 00:00:00 2001 From: Lunarmagpie <65521138+Lunarmagpie@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:33:32 -0500 Subject: [PATCH 21/34] Update pincer/middleware/interaction_create.py Co-authored-by: trag1c <77130613+trag1c@users.noreply.github.com> --- pincer/middleware/interaction_create.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pincer/middleware/interaction_create.py b/pincer/middleware/interaction_create.py index 7c3f0b30..cdc49759 100644 --- a/pincer/middleware/interaction_create.py +++ b/pincer/middleware/interaction_create.py @@ -167,7 +167,7 @@ async def interaction_create_middleware( context, interaction, # Always take the error parameter its name. - {params[(len(params) - 1) or 0]: e}, + {params[-1]: e}, ) else: raise e From aac1ec981781856543e35897022893c5172a769b Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 15:35:41 -0500 Subject: [PATCH 22/34] :art: fixed formatting to work with 88 char line length --- pincer/middleware/presence_update.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pincer/middleware/presence_update.py b/pincer/middleware/presence_update.py index fee94856..85cf6936 100644 --- a/pincer/middleware/presence_update.py +++ b/pincer/middleware/presence_update.py @@ -26,9 +26,7 @@ async def presence_update_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_presence_update", - PresenceUpdateEvent.from_dict( - construct_client_dict(self, payload.data) - ), + PresenceUpdateEvent.from_dict(construct_client_dict(self, payload.data)), ) From b490aa70c95d1728d97b1b370db7fdb551586a9b Mon Sep 17 00:00:00 2001 From: Lunarmagpie <65521138+Lunarmagpie@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:36:04 -0500 Subject: [PATCH 23/34] :recycle: Update pincer/middleware/stage_instance_delete.py Co-authored-by: trag1c <77130613+trag1c@users.noreply.github.com> --- pincer/middleware/stage_instance_delete.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pincer/middleware/stage_instance_delete.py b/pincer/middleware/stage_instance_delete.py index b6f394da..7dbe1bc1 100644 --- a/pincer/middleware/stage_instance_delete.py +++ b/pincer/middleware/stage_instance_delete.py @@ -28,9 +28,9 @@ async def stage_instance_delete_middleware(self, payload: GatewayDispatch): guild = self.guilds.get(stage.guild_id) if guild: - guild.stage_instances = list( - filter(lambda _stage: _stage.id != stage.id, guild.stage_instances) - ) + guild.stage_instances = [ + _stage for _stage in guild.stage_instances if _stage.id != stage.id + ] return "on_stage_instance_delete", stage From 9795c135288712ce0c70518f28754d44fee8690f Mon Sep 17 00:00:00 2001 From: Lunarmagpie <65521138+Lunarmagpie@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:36:23 -0500 Subject: [PATCH 24/34] :recycle: Update pincer/middleware/thread_delete.py Co-authored-by: trag1c <77130613+trag1c@users.noreply.github.com> --- pincer/middleware/thread_delete.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pincer/middleware/thread_delete.py b/pincer/middleware/thread_delete.py index eeceea6d..b81db505 100644 --- a/pincer/middleware/thread_delete.py +++ b/pincer/middleware/thread_delete.py @@ -28,9 +28,9 @@ async def thread_delete_middleware(self, payload: GatewayDispatch): guild = self.guilds.get(channel.guild_id) if guild: - guild.threads = list( - filter(lambda c: c.id != channel.id, guild.threads) - ) + guild.threads = [ + c for c in guild.threads if c.id != channel.id + ] self.channels.pop(channel.id, None) From 21be6f75fd206b26e1b2fcf7b4427a9d591b0a8f Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 15:38:13 -0500 Subject: [PATCH 25/34] :art: fixed formatting to work with 88 char line length --- pincer/middleware/webhooks_update.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pincer/middleware/webhooks_update.py b/pincer/middleware/webhooks_update.py index 2b940671..1856d48c 100644 --- a/pincer/middleware/webhooks_update.py +++ b/pincer/middleware/webhooks_update.py @@ -28,9 +28,7 @@ async def webhooks_update_middleware(self, payload: GatewayDispatch): """ # noqa: E501 return ( "on_webhooks_update", - WebhooksUpdateEvent.from_dict( - construct_client_dict(self, payload.data) - ), + WebhooksUpdateEvent.from_dict(construct_client_dict(self, payload.data)), ) From 1fd59d51fc271447c7d1ea8a056cb4841c175df8 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 15:55:03 -0500 Subject: [PATCH 26/34] :art: fixed formatting to work with 88 char line length --- pincer/middleware/thread_member_update.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/pincer/middleware/thread_member_update.py b/pincer/middleware/thread_member_update.py index 6c723acf..f9b92894 100644 --- a/pincer/middleware/thread_member_update.py +++ b/pincer/middleware/thread_member_update.py @@ -27,17 +27,7 @@ async def thread_member_update_middleware(self, payload: GatewayDispatch): return ( "on_thread_member_update", - ThreadMember.from_dict( - construct_client_dict( - self, - { - "join_timestamp": Timestamp( - payload.data.pop("join_timestamp") - ), - **payload.data, - }, - ) - ), + ThreadMember.from_dict(construct_client_dict(payload.data)), ) From eed689318266d6de3baaf384671f832c1126bfdf Mon Sep 17 00:00:00 2001 From: Lunarmagpie <65521138+Lunarmagpie@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:53:32 -0500 Subject: [PATCH 27/34] :recycle: Update pincer/middleware/channel_delete.py Co-authored-by: trag1c <77130613+trag1c@users.noreply.github.com> --- pincer/middleware/channel_delete.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pincer/middleware/channel_delete.py b/pincer/middleware/channel_delete.py index b3f62c78..981a764c 100644 --- a/pincer/middleware/channel_delete.py +++ b/pincer/middleware/channel_delete.py @@ -28,9 +28,9 @@ async def channel_delete_middleware(self, payload: GatewayDispatch): guild = self.guilds.get(channel.guild_id) if guild: - guild.channels = list( - filter(lambda c: c.id != channel.id, guild.channels) - ) + guild.channels = [ + c for c in guild.channels if c.id != channel.id + ] self.channels.pop(channel.id, None) From 97b09ce90dccc92528c71dcee058a19e75da7916 Mon Sep 17 00:00:00 2001 From: Lunarmagpie <65521138+Lunarmagpie@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:53:42 -0500 Subject: [PATCH 28/34] :recycle: Update pincer/middleware/guild_role_delete.py Co-authored-by: trag1c <77130613+trag1c@users.noreply.github.com> --- pincer/middleware/guild_role_delete.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pincer/middleware/guild_role_delete.py b/pincer/middleware/guild_role_delete.py index e8ec17ef..485e98ca 100644 --- a/pincer/middleware/guild_role_delete.py +++ b/pincer/middleware/guild_role_delete.py @@ -32,12 +32,10 @@ async def guild_role_delete_middleware(self, payload: GatewayDispatch): guild = self.guilds.get(event.guild_id) if guild: - guild.roles = list( - filter( - lambda role: role.id != event.role_id, - self.guilds[event.guild_id].roles, - ) - ) + guild.roles = [ + role for role in self.guilds[event.guild_id].roles + if role.id != event.role_id + ] return ("on_guild_role_delete", event) From f13850390f3e2e961c097adce68dd6fe3d95fb0f Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 17:30:14 -0500 Subject: [PATCH 29/34] :memo: added ctx to examples --- docs/interactions.rst | 3 +-- examples/guessing_game.py | 11 +++++------ pincer/objects/message/context.py | 4 +++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/interactions.rst b/docs/interactions.rst index 75e66fab..ca4b78bc 100644 --- a/docs/interactions.rst +++ b/docs/interactions.rst @@ -281,6 +281,5 @@ The :class:`~pincer.objects.message.context.MessageContext` object provides meth @command async def some_other_command(self, ctx: MessageContext): - channel = await self.get_channel(ctx.channel_id) - channel.send("Hello world!") # Sends a message in the channel + ctx.channel.send("Hello world!") # Sends a message in the channel return "Hello world 2" # This is sent because the interaction was not "used up" diff --git a/examples/guessing_game.py b/examples/guessing_game.py index 4cc4054d..94bc9a28 100644 --- a/examples/guessing_game.py +++ b/examples/guessing_game.py @@ -13,7 +13,6 @@ async def guess(self, ctx: MessageContext, biggest_number: int): f" Pick a number between 0 and {biggest_number}." ) - channel = await self.get_channel(ctx.channel_id) number = random.randint(0, biggest_number) try: @@ -24,7 +23,7 @@ async def guess(self, ctx: MessageContext, biggest_number: int): continue if not next_message.content.isdigit(): - await channel.send( + await ctx.channel.send( f"{next_message.content} is not a number. Try again!" ) continue @@ -32,16 +31,16 @@ async def guess(self, ctx: MessageContext, biggest_number: int): guessed_number = int(next_message.content) if guessed_number > number: - await channel.send("Number is too high!") + await ctx.channel.send("Number is too high!") elif guessed_number < number: - await channel.send("Number is too low!") + await ctx.channel.send("Number is too low!") else: await next_message.react("🚀") - await channel.send("Number is correct!") + await ctx.channel.send("Number is correct!") break except TimeoutError: - await channel.send("You took too long! The game timed out.") + await ctx.channel.send("You took too long! The game timed out.") if __name__ == "__main__": diff --git a/pincer/objects/message/context.py b/pincer/objects/message/context.py index f44af982..ebf51ab8 100644 --- a/pincer/objects/message/context.py +++ b/pincer/objects/message/context.py @@ -5,6 +5,8 @@ from dataclasses import dataclass from typing import TYPE_CHECKING +from ...utils.api_object import ChannelProperty, GuildProperty + if TYPE_CHECKING: from typing import Optional, Union @@ -18,7 +20,7 @@ @dataclass(repr=False) -class MessageContext: +class MessageContext(GuildProperty, ChannelProperty): """Represents the context of a message interaction. Attributes From 913ba15bfecb1f6a3056fe4c4c03c75138ea4cfa Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 19:22:44 -0500 Subject: [PATCH 30/34] :bug: APIObject _client is now correctly propogated to children. --- examples/guessing_game.py | 2 +- pincer/objects/app/interactions.py | 1 + pincer/objects/message/context.py | 20 +++++++++++++----- pincer/utils/__init__.py | 4 ++-- pincer/utils/api_object.py | 29 +++++++++++++++----------- pincer/utils/conversion.py | 33 ------------------------------ 6 files changed, 36 insertions(+), 53 deletions(-) diff --git a/examples/guessing_game.py b/examples/guessing_game.py index 94bc9a28..7cfa1379 100644 --- a/examples/guessing_game.py +++ b/examples/guessing_game.py @@ -16,7 +16,7 @@ async def guess(self, ctx: MessageContext, biggest_number: int): number = random.randint(0, biggest_number) try: - async for next_message, in self.loop_for( + async for next_message in self.loop_for( "on_message", loop_timeout=60 ): if next_message.author.bot: diff --git a/pincer/objects/app/interactions.py b/pincer/objects/app/interactions.py index d4ce03fb..6b8bcfff 100644 --- a/pincer/objects/app/interactions.py +++ b/pincer/objects/app/interactions.py @@ -209,6 +209,7 @@ def return_type( def convert_to_message_context(self, command): return MessageContext( + self._client, self.member or self.user, command, self, diff --git a/pincer/objects/message/context.py b/pincer/objects/message/context.py index ebf51ab8..afab2d6b 100644 --- a/pincer/objects/message/context.py +++ b/pincer/objects/message/context.py @@ -5,8 +5,6 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from ...utils.api_object import ChannelProperty, GuildProperty - if TYPE_CHECKING: from typing import Optional, Union @@ -15,12 +13,13 @@ from ..app.interaction_flags import InteractionFlags from ..guild.member import GuildMember from ..user.user import User + from ...client import Client from ...utils.convert_message import MessageConvertable from ...utils.snowflake import Snowflake @dataclass(repr=False) -class MessageContext(GuildProperty, ChannelProperty): +class MessageContext: """Represents the context of a message interaction. Attributes @@ -40,8 +39,9 @@ class MessageContext(GuildProperty, ChannelProperty): channel_id: Optional[:class:`~pincer.utils.snowflake.Snowflake`] The ID of the channel the interaction was invoked in. Can be None if it wasn't invoked in a channel. - """ - # noqa: E501 + """ # noqa: E501 + _client: Client + author: Union[GuildMember, User] command: ClientCommandStructure interaction: Interaction @@ -49,6 +49,16 @@ class MessageContext(GuildProperty, ChannelProperty): guild_id: Optional[Snowflake] = None channel_id: Optional[Snowflake] = None + # Properties do not use ChannelProperty and GuildProperty because MessageContext is + # not an APIObject. + @property + def channel(self): + return self._client.channels[self.channel_id] + + @property + def guild(self): + return self._client.guilds[self.channel_id] + async def ack(self, flags: InteractionFlags = None): """|coro| diff --git a/pincer/utils/__init__.py b/pincer/utils/__init__.py index 9fab52fc..0ac991d2 100644 --- a/pincer/utils/__init__.py +++ b/pincer/utils/__init__.py @@ -3,7 +3,7 @@ from .api_object import APIObject, HTTPMeta, ChannelProperty, GuildProperty from .color import Color -from .conversion import convert, construct_client_dict +from .conversion import construct_client_dict from .event_mgr import EventMgr from .replace import replace from .directory import chdir @@ -22,7 +22,7 @@ __all__ = ( "APINullable", "APIObject", "CheckFunction", "Color", "Coro", "EventMgr", "HTTPMeta", "MISSING", "MissingType", "Snowflake", "Task", - "TaskScheduler", "Timestamp", "chdir", "choice_value_types", "convert", + "TaskScheduler", "Timestamp", "chdir", "choice_value_types", "get_index", "get_params", "get_signature_and_params", "should_pass_cls", "should_pass_ctx", "replace", "construct_client_dict", "ChannelProperty", "GuildProperty" ) diff --git a/pincer/utils/api_object.py b/pincer/utils/api_object.py index 7bf2fe82..b1b4b1f7 100644 --- a/pincer/utils/api_object.py +++ b/pincer/utils/api_object.py @@ -13,7 +13,8 @@ List, get_type_hints, get_origin, get_args ) -from .conversion import convert +from pincer.utils.conversion import construct_client_dict + from .types import MissingType, MISSING, TypeCache from ..exceptions import InvalidArgumentAnnotation @@ -172,12 +173,16 @@ def __attr_convert(self, attr_value: Dict, attr_type: T) -> T: if getattr(attr_type, "__factory__", None): factory = attr_type.__factory__ - return convert( - attr_value, - factory, - attr_type, - self._client - ) + if attr_value is MISSING: + return MISSING + + if attr_type is not None and isinstance(attr_value, attr_type): + return attr_value + + if isinstance(attr_value, dict): + return factory(construct_client_dict(self._client, attr_value)) + + return factory(attr_value) def __post_init__(self): self._http = getattr(self._client, "http", None) @@ -287,23 +292,23 @@ def to_dict(self) -> Dict: class GuildProperty: @property - def guild(obj) -> Guild: + def guild(self) -> Guild: """Return a guild from an APIObject Parameters ---------- - obj : :class:`~pincer.utils.api_object.APIObject` + self : :class:`~pincer.utils.api_object.APIObject` Returns ------- :class:`~pincer.objects.guild.guild.Guild` """ - return obj._client.guilds[obj.guild_id] + return self._client.guilds[self.guild_id] class ChannelProperty: @property - def channel(obj) -> Channel: + def channel(self) -> Channel: """Return a channel from an APIObject Parameters ---------- @@ -313,4 +318,4 @@ def channel(obj) -> Channel: ------- :class:`~pincer.objects.guild.channel.Channel` """ - return obj._client.channels[obj.channel_id] + return self._client.channels[self.channel_id] diff --git a/pincer/utils/conversion.py b/pincer/utils/conversion.py index 487d30d8..1e7137d7 100644 --- a/pincer/utils/conversion.py +++ b/pincer/utils/conversion.py @@ -29,39 +29,6 @@ def construct_client_dict(client: Client, data: Dict) -> Dict: return {**data, "_client": client} -def convert( - value: Any, - factory: Callable[[Any], T], - check: Optional[T] = None, - client: Optional[Client] = None, -) -> T: - """ - Parameters - ---------- - value : Any - The value that has to have its type converted. - factory : Callable[[Any], T] - The conversion factory/object to use. - check : Optional[T] - Skip conversion if ``value`` is already this type. - client : Optional[:class:`~pincer.client.Client`] - Reference to :class:`~pincer.client.Client` - """ - def handle_factory() -> T: - if check is not None and isinstance(value, check): - return value - - try: - if client and "_client" in getfullargspec(factory).args: - return factory(construct_client_dict(client, value)) - except TypeError: # Building type/has no signature - pass - - return factory(value) - - return MISSING if value is MISSING else handle_factory() - - def remove_none(obj: Union[List, Dict, Set]) -> Union[List, Dict, Set]: """ Removes all ``None`` values from a list, dict or set. From cbac5daad1b4832b1049ef5456d2308adb4c8ab7 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 19:28:16 -0500 Subject: [PATCH 31/34] :bug: ThreadMember now has correct args --- pincer/middleware/thread_member_update.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pincer/middleware/thread_member_update.py b/pincer/middleware/thread_member_update.py index f9b92894..4d4a8036 100644 --- a/pincer/middleware/thread_member_update.py +++ b/pincer/middleware/thread_member_update.py @@ -27,7 +27,7 @@ async def thread_member_update_middleware(self, payload: GatewayDispatch): return ( "on_thread_member_update", - ThreadMember.from_dict(construct_client_dict(payload.data)), + ThreadMember.from_dict(construct_client_dict(self, payload.data)), ) From 267ea16ba8d8f3c249fe19ad314a9377a53fadf7 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 19:29:20 -0500 Subject: [PATCH 32/34] :memo: ctx.channel.send is now awaited --- docs/interactions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/interactions.rst b/docs/interactions.rst index ca4b78bc..701572a4 100644 --- a/docs/interactions.rst +++ b/docs/interactions.rst @@ -281,5 +281,5 @@ The :class:`~pincer.objects.message.context.MessageContext` object provides meth @command async def some_other_command(self, ctx: MessageContext): - ctx.channel.send("Hello world!") # Sends a message in the channel + await ctx.channel.send("Hello world!") # Sends a message in the channel return "Hello world 2" # This is sent because the interaction was not "used up" From 0c32152d2de66f0fbac5235750104010a0c72e27 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 19:38:35 -0500 Subject: [PATCH 33/34] :art: added missing await --- docs/interactions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/interactions.rst b/docs/interactions.rst index 701572a4..298e9647 100644 --- a/docs/interactions.rst +++ b/docs/interactions.rst @@ -276,7 +276,7 @@ The :class:`~pincer.objects.message.context.MessageContext` object provides meth @command async def some_command(self, ctx: MessageContext): - ctx.send("Hello world!") # Sends hello world as the response to the interaction + await ctx.send("Hello world!") # Sends hello world as the response to the interaction return # No response will be sent now that the interaction has been completed @command From 954f6bf949df84a924e60652c407bf5766d3413e Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Sun, 12 Dec 2021 20:41:11 -0500 Subject: [PATCH 34/34] :art: removed unused import --- pincer/middleware/thread_member_update.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pincer/middleware/thread_member_update.py b/pincer/middleware/thread_member_update.py index 4d4a8036..3ad1074a 100644 --- a/pincer/middleware/thread_member_update.py +++ b/pincer/middleware/thread_member_update.py @@ -5,7 +5,6 @@ from ..core.dispatch import GatewayDispatch from ..objects import ThreadMember -from ..utils import Timestamp from ..utils.conversion import construct_client_dict