From 0af07b50e93ede95debab2aee344da0eb21950a8 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Wed, 8 Jun 2022 22:01:48 +0200 Subject: [PATCH 01/11] refactor!: guild model --- interactions/api/models/guild.py | 184 ++++++++++++++++-------------- interactions/api/models/guild.pyi | 36 +++--- 2 files changed, 119 insertions(+), 101 deletions(-) diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index 1b780411e..a164ac4b4 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -323,7 +323,7 @@ def __repr__(self) -> str: async def ban( self, - member_id: int, + member_id: Union[int, Member, Snowflake], reason: Optional[str] = None, delete_message_days: Optional[int] = 0, ) -> None: @@ -331,7 +331,7 @@ async def ban( Bans a member from the guild. :param member_id: The id of the member to ban - :type member_id: int + :type member_id: Union[int, Member, Snowflake] :param reason?: The reason of the ban :type reason: Optional[str] :param delete_message_days?: Number of days to delete messages, from 0 to 7. Defaults to 0 @@ -339,23 +339,26 @@ async def ban( """ if not self._client: raise AttributeError("HTTPClient not found!") + + _member_id = int(member_id.id) if isinstance(member_id, Member) else int(member_id) + await self._client.create_guild_ban( guild_id=int(self.id), - user_id=member_id, + user_id=_member_id, reason=reason, delete_message_days=delete_message_days, ) async def remove_ban( self, - user_id: int, + user_id: Union[int, Snowflake], # only support ID since there's no member on the guild reason: Optional[str] = None, ) -> None: """ Removes the ban of a user. :param user_id: The id of the user to remove the ban from - :type user_id: int + :type user_id: Union[int, Snowflake] :param reason?: The reason for the removal of the ban :type reason: Optional[str] """ @@ -369,90 +372,85 @@ async def remove_ban( async def kick( self, - member_id: int, + member_id: Union[int, Member, Snowflake], reason: Optional[str] = None, ) -> None: """ Kicks a member from the guild. :param member_id: The id of the member to kick - :type member_id: int + :type member_id: Union[int, Member, Snowflake] :param reason?: The reason for the kick :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") + + _member_id = int(member_id.id) if isinstance(member_id, Member) else int(member_id) + await self._client.create_guild_kick( guild_id=int(self.id), - user_id=member_id, + user_id=_member_id, reason=reason, ) async def add_member_role( self, - role: Union[Role, int], - member_id: int, + role: Union[Role, int, Snowflake], + member_id: Union[Member, int, Snowflake], reason: Optional[str] = None, ) -> None: """ This method adds a role to a member. :param role: The role to add. Either ``Role`` object or role_id - :type role Union[Role, int] + :type role Union[Role, int, Snowflake] :param member_id: The id of the member to add the roles to - :type member_id: int + :type member_id: Union[Member, int, Snowflake] :param reason?: The reason why the roles are added :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") - if isinstance(role, Role): - await self._client.add_member_role( - guild_id=int(self.id), - user_id=member_id, - role_id=int(role.id), - reason=reason, - ) - else: - await self._client.add_member_role( - guild_id=int(self.id), - user_id=member_id, - role_id=role, - reason=reason, - ) + + _role_id = int(role.id) if isinstance(role, Role) else int(role) + _member_id = int(member_id.id) if isinstance(member_id, Member) else int(member_id) + + await self._client.add_member_role( + guild_id=int(self.id), + user_id=_member_id, + role_id=_role_id, + reason=reason, + ) async def remove_member_role( self, - role: Union[Role, int], - member_id: int, + role: Union[Role, int, Snowflake], + member_id: Union[Member, int, Snowflake], reason: Optional[str] = None, ) -> None: """ This method removes a or multiple role(s) from a member. :param role: The role to remove. Either ``Role`` object or role_id - :type role: Union[Role, int] + :type role: Union[Role, int, Snowflake] :param member_id: The id of the member to remove the roles from - :type member_id: int + :type member_id: Union[Member, int, Snowflake] :param reason?: The reason why the roles are removed :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") - if isinstance(role, Role): - await self._client.remove_member_role( - guild_id=int(self.id), - user_id=member_id, - role_id=int(role.id), - reason=reason, - ) - else: - await self._client.remove_member_role( - guild_id=int(self.id), - user_id=member_id, - role_id=role, - reason=reason, - ) + + _role_id = int(role.id) if isinstance(role, Role) else int(role) + _member_id = int(member_id.id) if isinstance(member_id, Member) else int(member_id) + + await self._client.remove_member_role( + guild_id=int(self.id), + user_id=_member_id, + role_id=_role_id, + reason=reason, + ) async def create_role( self, @@ -510,13 +508,13 @@ async def create_role( async def get_member( self, - member_id: int, + member_id: Union[int, Snowflake], ) -> Member: """ Searches for the member with specified id in the guild and returns the member as member object. :param member_id: The id of the member to search for - :type member_id: int + :type member_id: Union[int, Snowflake] :return: The member searched for :rtype: Member """ @@ -524,48 +522,54 @@ async def get_member( raise AttributeError("HTTPClient not found!") res = await self._client.get_member( guild_id=int(self.id), - member_id=member_id, + member_id=int(member_id), ) return Member(**res, _client=self._client) async def delete_channel( self, - channel_id: int, + channel_id: Union[int, Snowflake], ) -> None: """ Deletes a channel from the guild. :param channel_id: The id of the channel to delete - :type channel_id: int + :type channel_id: Union[int, Snowflake, Channel] """ if not self._client: raise AttributeError("HTTPClient not found!") - await self._client.delete_channel(channel_id=channel_id) + + _channel_id = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) + + await self._client.delete_channel(_channel_id) async def delete_role( self, - role_id: int, + role_id: Union[int, Snowflake, Role], reason: Optional[str] = None, ) -> None: """ Deletes a role from the guild. :param role_id: The id of the role to delete - :type role_id: int + :type role_id: Union[int, Snowflake, Role] :param reason?: The reason of the deletion :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") + + _role_id = int(role_id.id) if isinstance(role_id, Role) else int(role_id) + await self._client.delete_guild_role( guild_id=int(self.id), - role_id=role_id, + role_id=_role_id, reason=reason, ) async def modify_role( self, - role_id: int, + role_id: Union[int, Snowflake, Role], name: Optional[str] = MISSING, permissions: Optional[int] = MISSING, color: Optional[int] = MISSING, @@ -579,7 +583,7 @@ async def modify_role( Edits a role in the guild. :param role_id: The id of the role to edit - :type role_id: int + :type role_id: Union[int, Snowflake, Role] :param name?: The name of the role, defaults to the current value of the role :type name: Optional[str] :param color?: RGB color value as integer, defaults to the current value of the role @@ -601,11 +605,11 @@ async def modify_role( """ if not self._client: raise AttributeError("HTTPClient not found!") - roles = await self._client.get_all_roles(guild_id=int(self.id)) - for i in roles: - if int(i["id"]) == role_id: - role = Role(**i) - break + + if isinstance(role_id, Role): + role = role_id + else: + role = await self.get_role(int(role_id)) _name = role.name if name is MISSING else name _color = role.color if color is MISSING else color @@ -636,7 +640,7 @@ async def modify_role( async def create_thread( self, name: str, - channel_id: int, + channel_id: Union[int, Snowflake, Channel], type: Optional[ChannelType] = ChannelType.GUILD_PUBLIC_THREAD, auto_archive_duration: Optional[int] = MISSING, invitable: Optional[bool] = MISSING, @@ -649,7 +653,7 @@ async def create_thread( :param name: The name of the thread :type name: str :param channel_id: The id of the channel to create the thread in - :type channel_id: int + :type channel_id: Union[int, Snowflake, Channel] :param auto_archive_duration?: duration in minutes to automatically archive the thread after recent activity, can be set to: 60, 1440, 4320, 10080 :type auto_archive_duration: Optional[int] @@ -676,8 +680,10 @@ async def create_thread( _auto_archive_duration = None if auto_archive_duration is MISSING else auto_archive_duration _invitable = None if invitable is MISSING else invitable _message_id = None if message_id is MISSING else message_id + # not refactoring this since I don't want to deal w import hell + _channel_id = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) res = await self._client.create_thread( - channel_id=channel_id, + channel_id=_channel_id, thread_type=type if isinstance(type, int) else type.value, name=name, auto_archive_duration=_auto_archive_duration, @@ -698,7 +704,7 @@ async def create_channel( rate_limit_per_user: Optional[int] = MISSING, position: Optional[int] = MISSING, permission_overwrites: Optional[List[Overwrite]] = MISSING, - parent_id: Optional[int] = MISSING, + parent_id: Optional[Union[int, Channel, Snowflake]] = MISSING, nsfw: Optional[bool] = MISSING, reason: Optional[str] = None, ) -> Channel: @@ -720,7 +726,7 @@ async def create_channel( :param position?: Sorting position of the channel :type position: Optional[int] :param parent_id?: The id of the parent category for a channel - :type parent_id: Optional[int] + :type parent_id: Optional[Union[int, Channel, Snowflake]] :param permission_overwrites?: The permission overwrites, if any :type permission_overwrites: Optional[Overwrite] :param nsfw?: Whether the channel is nsfw or not, default ``False`` @@ -739,8 +745,8 @@ async def create_channel( ChannelType.GROUP_DM.value, ]: raise ValueError( - "ChannelType must not be a direct-message when creating Guild Channels!" # TODO: move to custom error formatter - ) + "ChannelType must not be a direct-message when creating Guild Channels!" + ) # TODO: move to custom error formatter if type in [ ChannelType.GUILD_NEWS_THREAD, @@ -764,7 +770,7 @@ async def create_channel( if position is not MISSING: payload["position"] = position if parent_id is not MISSING: - payload["parent_id"] = parent_id + payload["parent_id"] = int(parent_id.id) if isinstance(parent_id, Channel) else (parent_id.id) if nsfw is not MISSING: payload["nsfw"] = nsfw if permission_overwrites is not MISSING: @@ -780,18 +786,19 @@ async def create_channel( return Channel(**res, _client=self._client) - async def clone_channel(self, channel_id: int) -> Channel: + async def clone_channel(self, channel_id: Union[int, Snowflake, Channel]) -> Channel: """ Clones a channel of the guild. :param channel_id: The id of the channel to clone - :type channel_id: int + :type channel_id: Union[int, Snowflake, Channel] :return: The cloned channel :rtype: Channel """ if not self._client: raise AttributeError("HTTPClient not found!") - res = await self._client.get_channel(channel_id=channel_id) + + res = channel_id._json if isinstance(channel_id, Channel) else await self._client.get_channel(channel_id=int(channel_id)) res["permission_overwrites"] = [Overwrite(**_) for _ in res["permission_overwrites"]] for attr in {"flags", "guild_id", "id", "last_message_id", "last_pin_timestamp"}: @@ -801,7 +808,7 @@ async def clone_channel(self, channel_id: int) -> Channel: async def modify_channel( self, - channel_id: int, + channel_id: Union[int, Snowflake, Channel], name: Optional[str] = MISSING, topic: Optional[str] = MISSING, bitrate: Optional[int] = MISSING, @@ -823,7 +830,7 @@ async def modify_channel( The fields `archived`, `auto_archive_duration` and `locked` require the provided channel to be a thread. :param channel_id: The id of the channel to modify - :type channel_id: int + :type channel_id: Union[int, Snowflake, Channel] :param name?: The name of the channel, defaults to the current value of the channel :type name: str :param topic?: The topic of that channel, defaults to the current value of the channel @@ -855,7 +862,11 @@ async def modify_channel( """ if not self._client: raise AttributeError("HTTPClient not found!") - ch = Channel(**await self._client.get_channel(channel_id=channel_id)) + + if isinstance(channel_id, Channel): + ch = channel_id + else: + ch = Channel(**await self._client.get_channel(channel_id=int(channel_id))) _name = ch.name if name is MISSING else name _topic = ch.topic if topic is MISSING else topic @@ -914,7 +925,7 @@ async def modify_channel( async def modify_member( self, - member_id: int, + member_id: Union[int, Snowflake, Member], nick: Optional[str] = MISSING, roles: Optional[List[int]] = MISSING, mute: Optional[bool] = MISSING, @@ -927,7 +938,7 @@ async def modify_member( Modifies a member of the guild. :param member_id: The id of the member to modify - :type member_id: int + :type member_id: Union[int, Snowflake, Member] :param nick?: The nickname of the member :type nick: Optional[str] :param roles?: A list of all role ids the member has @@ -966,8 +977,10 @@ async def modify_member( if communication_disabled_until is not MISSING: payload["communication_disabled_until"] = communication_disabled_until + _member_id = int(member_id.id) if isinstance(member_id, Member) else int(member_id) + res = await self._client.modify_member( - user_id=member_id, + user_id=_member_id, guild_id=int(self.id), payload=payload, reason=reason, @@ -1483,7 +1496,7 @@ async def create_scheduled_event( async def modify_scheduled_event( self, - event_id: int, + event_id: Union[int, "ScheduledEvents", Snowflake], name: Optional[str] = MISSING, entity_type: Optional[EntityType] = MISSING, scheduled_start_time: Optional[datetime.isoformat] = MISSING, @@ -1499,7 +1512,7 @@ async def modify_scheduled_event( Edits a scheduled event of the guild. :param event_id: The id of the event to edit - :type event_id: int + :type event_id: Union[int, "ScheduledEvents", Snowflake] :param name: The name of the event :type name: Optional[str] :param entity_type: The entity type of the scheduled event @@ -1553,25 +1566,30 @@ async def modify_scheduled_event( if image is not MISSING: payload["image"] = image.data if isinstance(image, Image) else image + _event_id = event_id.id if isinstance(event_id, ScheduledEvents) else event_id + res = await self._client.modify_scheduled_event( guild_id=self.id, - guild_scheduled_event_id=Snowflake(event_id), + guild_scheduled_event_id=_event_id, payload=payload, ) return ScheduledEvents(**res) - async def delete_scheduled_event(self, event_id: int) -> None: + async def delete_scheduled_event(self, event_id: Union[int, "ScheduledEvents", Snowflake]) -> None: """ Deletes a scheduled event of the guild. :param event_id: The id of the event to delete - :type event_id: int + :type event_id: Union[int, "ScheduledEvents", Snowflake] """ if not self._client: raise AttributeError("HTTPClient not found!") + + _event_id = event_id.id if isinstance(event_id, ScheduledEvents) else event_id + await self._client.delete_scheduled_event( guild_id=self.id, - guild_scheduled_event_id=Snowflake(event_id), + guild_scheduled_event_id=Snowflake(_event_id), ) async def get_all_channels(self) -> List[Channel]: diff --git a/interactions/api/models/guild.pyi b/interactions/api/models/guild.pyi index 2b3613f3e..a6445cb14 100644 --- a/interactions/api/models/guild.pyi +++ b/interactions/api/models/guild.pyi @@ -159,30 +159,30 @@ class Guild(ClientSerializerMixin): def __repr__(self) -> str: ... async def ban( self, - member_id: int, + member_id: Union[int, Member, Snowflake], reason: Optional[str] = None, delete_message_days: Optional[int] = 0, ) -> None: ... async def remove_ban( self, - user_id: int, + user_id: Union[int, Snowflake], reason: Optional[str] = None, ) -> None: ... async def kick( self, - member_id: int, + member_id: Union[int, Snowflake], reason: Optional[str] = None, ) -> None: ... async def add_member_role( self, - role: Union[Role, int], - member_id: int, + role: Union[Role, int, Snowflake], + member_id: Union[Member, int, Snowflake], reason: Optional[str] = None, ) -> None: ... async def remove_member_role( self, - role: Union[Role, int], - member_id: int, + role: Union[Role, int, Snowflake], + member_id: Union[Member, int, Snowflake], reason: Optional[str] = None, ) -> None: ... async def create_role( @@ -198,20 +198,20 @@ class Guild(ClientSerializerMixin): ) -> Role: ... async def get_member( self, - member_id: int, + member_id: Union[int, Snowflake], ) -> Member: ... async def delete_channel( self, - channel_id: int, + channel_id: Union[int, Snowflake, Channel], ) -> None: ... async def delete_role( self, - role_id: int, + role_id: Union[int, Snowflake, Role], reason: Optional[str] = None, ) -> None: ... async def modify_role( self, - role_id: int, + role_id: Union[int, Snowflake, Role], name: Optional[str] = MISSING, permissions: Optional[int] = MISSING, color: Optional[int] = MISSING, @@ -224,7 +224,7 @@ class Guild(ClientSerializerMixin): async def create_thread( self, name: str, - channel_id: int, + channel_id: Union[int, Snowflake, Channel], type: Optional[ChannelType] = ChannelType.GUILD_PUBLIC_THREAD, auto_archive_duration: Optional[int] = MISSING, invitable: Optional[bool] = MISSING, @@ -241,14 +241,14 @@ class Guild(ClientSerializerMixin): rate_limit_per_user: Optional[int] = MISSING, position: Optional[int] = MISSING, permission_overwrites: Optional[List[Overwrite]] = MISSING, - parent_id: Optional[int] = MISSING, + parent_id: Optional[Union[int, Channel, Snowflake]] = MISSING, nsfw: Optional[bool] = MISSING, reason: Optional[str] = None, ) -> Channel: ... - async def clone_channel(self, channel_id: int) -> Channel: ... + async def clone_channel(self, channel_id: Union[int, Snowflake, Channel]) -> Channel: ... async def modify_channel( self, - channel_id: int, + channel_id: Union[int, Snowflake, Channel], name: Optional[str] = MISSING, topic: Optional[str] = MISSING, bitrate: Optional[int] = MISSING, @@ -265,7 +265,7 @@ class Guild(ClientSerializerMixin): ) -> Channel: ... async def modify_member( self, - member_id: int, + member_id: Union[int, Snowflake, Member], nick: Optional[str] = MISSING, roles: Optional[List[int]] = MISSING, mute: Optional[bool] = MISSING, @@ -353,7 +353,7 @@ class Guild(ClientSerializerMixin): ) -> ScheduledEvents: ... async def modify_scheduled_event( self, - event_id: int, + event_id: Union[int, "ScheduledEvents", Snowflake], name: Optional[str] = ..., entity_type: Optional[EntityType] = ..., scheduled_start_time: Optional[datetime.isoformat] = ..., @@ -364,7 +364,7 @@ class Guild(ClientSerializerMixin): status: Optional[EventStatus] = ..., image: Optional[Image] = ..., ) -> ScheduledEvents: ... - async def delete_scheduled_event(self, event_id: int) -> None: ... + async def delete_scheduled_event(self, event_id: Union[int, "ScheduledEvents", Snowflake]) -> None: ... async def get_all_channels(self) -> List[Channel]: ... async def get_all_roles(self) -> List[Role]: ... async def get_role(self, role_id: int) -> Role: ... From 80efbed36fcdf33506feb72d10ee9e40ccf5a4f8 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Wed, 8 Jun 2022 22:03:36 +0200 Subject: [PATCH 02/11] ci: check --- interactions/api/models/guild.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index a164ac4b4..d4c953392 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -770,7 +770,9 @@ async def create_channel( if position is not MISSING: payload["position"] = position if parent_id is not MISSING: - payload["parent_id"] = int(parent_id.id) if isinstance(parent_id, Channel) else (parent_id.id) + payload["parent_id"] = ( + int(parent_id.id) if isinstance(parent_id, Channel) else (parent_id.id) + ) if nsfw is not MISSING: payload["nsfw"] = nsfw if permission_overwrites is not MISSING: @@ -798,7 +800,11 @@ async def clone_channel(self, channel_id: Union[int, Snowflake, Channel]) -> Cha if not self._client: raise AttributeError("HTTPClient not found!") - res = channel_id._json if isinstance(channel_id, Channel) else await self._client.get_channel(channel_id=int(channel_id)) + res = ( + channel_id._json + if isinstance(channel_id, Channel) + else await self._client.get_channel(channel_id=int(channel_id)) + ) res["permission_overwrites"] = [Overwrite(**_) for _ in res["permission_overwrites"]] for attr in {"flags", "guild_id", "id", "last_message_id", "last_pin_timestamp"}: @@ -1575,7 +1581,9 @@ async def modify_scheduled_event( ) return ScheduledEvents(**res) - async def delete_scheduled_event(self, event_id: Union[int, "ScheduledEvents", Snowflake]) -> None: + async def delete_scheduled_event( + self, event_id: Union[int, "ScheduledEvents", Snowflake] + ) -> None: """ Deletes a scheduled event of the guild. From 73c059911dd4e6fa3ef51de90683f4192a379731 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 11:08:11 +0200 Subject: [PATCH 03/11] allow message object without importing it --- interactions/api/models/guild.py | 9 +++++---- interactions/api/models/guild.pyi | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index d4c953392..9ceafb8b4 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -644,7 +644,7 @@ async def create_thread( type: Optional[ChannelType] = ChannelType.GUILD_PUBLIC_THREAD, auto_archive_duration: Optional[int] = MISSING, invitable: Optional[bool] = MISSING, - message_id: Optional[int] = MISSING, + message_id: Optional[Union[int, Snowflake, "Message"]] = MISSING, # noqa reason: Optional[str] = None, ) -> Channel: """ @@ -662,7 +662,7 @@ async def create_thread( :param invitable?: Boolean to display if the Thread is open to join or private. :type invitable: Optional[bool] :param message_id?: An optional message to create a thread from. - :type message_id: Optional[int] + :type message_id: Optional[Union[int, Snowflake, "Message"]] :param reason?: An optional reason for the audit log :type reason: Optional[str] :return: The created thread @@ -679,8 +679,9 @@ async def create_thread( _auto_archive_duration = None if auto_archive_duration is MISSING else auto_archive_duration _invitable = None if invitable is MISSING else invitable - _message_id = None if message_id is MISSING else message_id - # not refactoring this since I don't want to deal w import hell + _message_id = None if message_id is MISSING else ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) # work around Message import _channel_id = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) res = await self._client.create_thread( channel_id=_channel_id, diff --git a/interactions/api/models/guild.pyi b/interactions/api/models/guild.pyi index a6445cb14..b2062c2be 100644 --- a/interactions/api/models/guild.pyi +++ b/interactions/api/models/guild.pyi @@ -2,7 +2,7 @@ from datetime import datetime from enum import Enum, IntEnum from typing import Any, Dict, List, Optional, Union -from .attrs_utils import ClientSerializerMixin, DictSerializerMixin, define +from .attrs_utils import ClientSerializerMixin, DictSerializerMixin, define, MISSING from .channel import Channel, ChannelType, Thread from .member import Member from .message import Emoji, Sticker @@ -228,7 +228,7 @@ class Guild(ClientSerializerMixin): type: Optional[ChannelType] = ChannelType.GUILD_PUBLIC_THREAD, auto_archive_duration: Optional[int] = MISSING, invitable: Optional[bool] = MISSING, - message_id: Optional[int] = MISSING, + message_id: Optional[Union[int, Snowflake, "Message"]] = MISSING, # noqa reason: Optional[str] = None, ) -> Channel: ... async def create_channel( From 48634ad16249af513af211c0e960e54469e97669 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:08:42 +0000 Subject: [PATCH 04/11] ci: correct from checks. --- interactions/api/models/guild.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index 9ceafb8b4..1f5bedd02 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -679,8 +679,12 @@ async def create_thread( _auto_archive_duration = None if auto_archive_duration is MISSING else auto_archive_duration _invitable = None if invitable is MISSING else invitable - _message_id = None if message_id is MISSING else ( - int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + _message_id = ( + None + if message_id is MISSING + else ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) ) # work around Message import _channel_id = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) res = await self._client.create_thread( From cc7aa079edbfb4bce5af263dbbf4d421a8ae6676 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 11:29:28 +0200 Subject: [PATCH 05/11] refactor!: channel model --- interactions/api/models/channel.py | 47 ++++++++++++++++++----------- interactions/api/models/channel.pyi | 14 ++++----- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/interactions/api/models/channel.py b/interactions/api/models/channel.py index 9a74a89df..43221f525 100644 --- a/interactions/api/models/channel.py +++ b/interactions/api/models/channel.py @@ -595,7 +595,7 @@ async def lock( async def add_member( self, - member_id: int, + member_id: Union[int, Snowflake, "Member"], # noqa ) -> None: """ This adds a member to the channel, if the channel is a thread. @@ -609,46 +609,53 @@ async def add_member( raise TypeError( "The Channel you specified is not a thread!" ) # TODO: Move to new error formatter. - await self._client.add_member_to_thread(thread_id=int(self.id), user_id=member_id) + + _member_id = int(member_id) if isinstance(member_id, (int, Snowflake)) else int(member_id.id) + + await self._client.add_member_to_thread(thread_id=int(self.id), user_id=_member_id) async def pin_message( self, - message_id: int, + message_id: Union[int, Snowflake, "Message"], # noqa ) -> None: """ Pins a message to the channel. :param message_id: The id of the message to pin - :type message_id: int + :type message_id: Union[int, Snowflake, "Message"] """ if not self._client: raise AttributeError("HTTPClient not found!") - await self._client.pin_message(channel_id=int(self.id), message_id=message_id) + _message_id = int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + + await self._client.pin_message(channel_id=int(self.id), message_id=_message_id) async def unpin_message( self, - message_id: int, + message_id: Union[int, Snowflake, "Message"], # noqa ) -> None: """ Unpins a message from the channel. :param message_id: The id of the message to unpin - :type message_id: int + :type message_id: Union[int, Snowflake, "Message"] """ if not self._client: raise AttributeError("HTTPClient not found!") - await self._client.unpin_message(channel_id=int(self.id), message_id=message_id) + _message_id = int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + + await self._client.unpin_message(channel_id=int(self.id), message_id=_message_id) async def publish_message( self, - message_id: int, + message_id: Union[int, Snowflake, "Message"], # noqa ) -> "Message": # noqa """Publishes (API calls it crossposts) a message in the channel to any that is followed by. :param message_id: The id of the message to publish - :type message_id: int + :type message_id: Union[int, Snowflake, "Message"] :return: The message published :rtype: Message """ @@ -656,7 +663,9 @@ async def publish_message( raise AttributeError("HTTPClient not found!") from .message import Message - res = await self._client.publish_message(channel_id=int(self.id), message_id=message_id) + _message_id = int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + + res = await self._client.publish_message(channel_id=int(self.id), message_id=_message_id) return Message(**res, _client=self._client) @@ -676,17 +685,19 @@ async def get_pinned_messages(self) -> List["Message"]: # noqa async def get_message( self, - message_id: int, + message_id: Union[int, Snowflake], ) -> "Message": # noqa """ Gets a message sent in that channel. + :param message_id: The ID of the message to get + :type message_id: Union[int, Snowflake] :return: The message as object :rtype: Message """ res = await self._client.get_message( channel_id=int(self.id), - message_id=message_id, + message_id=int(message_id), ) from .message import Message @@ -695,7 +706,7 @@ async def get_message( async def purge( self, amount: int, - check: Callable = MISSING, + check: Callable[[...], bool] = MISSING, before: Optional[int] = MISSING, reason: Optional[str] = None, bulk: Optional[bool] = True, @@ -900,7 +911,7 @@ async def create_thread( type: Optional[ChannelType] = ChannelType.GUILD_PUBLIC_THREAD, auto_archive_duration: Optional[int] = MISSING, invitable: Optional[bool] = MISSING, - message_id: Optional[int] = MISSING, + message_id: Optional[Union[int, Snowflake, "Message"]] = MISSING, # noqa reason: Optional[str] = None, ) -> "Channel": """ @@ -916,7 +927,7 @@ async def create_thread( :param invitable?: Boolean to display if the Thread is open to join or private. :type invitable: Optional[bool] :param message_id?: An optional message to create a thread from. - :type message_id: Optional[int] + :type message_id: Optional[Union[int, Snowflake, "Message"]] :param reason?: An optional reason for the audit log :type reason: Optional[str] :return: The created thread @@ -933,7 +944,9 @@ async def create_thread( _auto_archive_duration = None if auto_archive_duration is MISSING else auto_archive_duration _invitable = None if invitable is MISSING else invitable - _message_id = None if message_id is MISSING else message_id + _message_id = None if message_id is MISSING else ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) res = await self._client.create_thread( channel_id=int(self.id), thread_type=type.value, diff --git a/interactions/api/models/channel.pyi b/interactions/api/models/channel.pyi index a7433329e..70283ebd4 100644 --- a/interactions/api/models/channel.pyi +++ b/interactions/api/models/channel.pyi @@ -122,16 +122,16 @@ class Channel(ClientSerializerMixin): self, auto_archive_duration: int, *, reason: Optional[str] = ... ) -> Channel: ... async def lock(self, locked: bool = ..., *, reason: Optional[str] = ...) -> Channel: ... - async def add_member(self, member_id: int) -> None: ... - async def pin_message(self, message_id: int) -> None: ... - async def unpin_message(self, message_id: int) -> None: ... - async def publish_message(self, message_id: int) -> Message: ... + async def add_member(self, member_id: Union[int, Snowflake, "Member"]) -> None: ... # noqa + async def pin_message(self, message_id: Union[int, Snowflake, "Message"]) -> None: ... # noqa + async def unpin_message(self, message_id: Union[int, Snowflake, "Message"]) -> None: ... # noqa + async def publish_message(self, message_id: Union[int, Snowflake, "Message"]) -> Message: ... # noqa async def get_pinned_messages(self) -> List[Message]: ... - async def get_message(self, message_id: int) -> Message: ... + async def get_message(self, message_id: Union[int, Snowflake]) -> Message: ... async def purge( self, amount: int, - check: Callable = ..., + check: Callable[[...], bool] = ..., before: Optional[int] = ..., reason: Optional[str] = ..., bulk: Optional[bool] = ..., @@ -142,7 +142,7 @@ class Channel(ClientSerializerMixin): type: Optional[ChannelType] = ..., auto_archive_duration: Optional[int] = ..., invitable: Optional[bool] = ..., - message_id: Optional[int] = ..., + message_id: Optional[Union[int, Snowflake, "Message"]] = ..., # noqa reason: Optional[str] = ..., ) -> Channel: ... @property From d0ac8328777dce3b6fefd4e79c0c5d74a97d64b4 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 12:10:40 +0200 Subject: [PATCH 06/11] refactor!: member model --- interactions/api/models/member.py | 108 +++++++++++++++-------------- interactions/api/models/member.pyi | 26 ++++--- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/interactions/api/models/member.py b/interactions/api/models/member.py index cedbe4b1c..4a970d8ff 100644 --- a/interactions/api/models/member.py +++ b/interactions/api/models/member.py @@ -92,7 +92,7 @@ def name(self) -> str: async def ban( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa reason: Optional[str] = None, delete_message_days: Optional[int] = 0, ) -> None: @@ -100,14 +100,17 @@ async def ban( Bans the member from a guild. :param guild_id: The id of the guild to ban the member from - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param reason?: The reason of the ban :type reason: Optional[str] :param delete_message_days?: Number of days to delete messages, from 0 to 7. Defaults to 0 :type delete_message_days: Optional[int] """ + + _guild_id = int(guild_id) if isinstance(guild_id, (Snowflake, int)) else int(guild_id.id) + await self._client.create_guild_ban( - guild_id=guild_id, + guild_id=_guild_id, user_id=int(self.user.id), reason=reason, delete_message_days=delete_message_days, @@ -115,19 +118,22 @@ async def ban( async def kick( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa reason: Optional[str] = None, ) -> None: """ Kicks the member from a guild. :param guild_id: The id of the guild to kick the member from - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param reason?: The reason for the kick :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") + + _guild_id = int(guild_id) if isinstance(guild_id, (Snowflake, int)) else int(guild_id.id) + await self._client.create_guild_kick( guild_id=guild_id, user_id=int(self.user.id), @@ -136,41 +142,37 @@ async def kick( async def add_role( self, - role: Union[Role, int], - guild_id: int, + role: Union[Role, int, Snowflake], + guild_id: Union[int, Snowflake, "Guild"], # noqa reason: Optional[str] = None, ) -> None: """ This method adds a role to a member. :param role: The role to add. Either ``Role`` object or role_id - :type role: Union[Role, int] + :type role: Union[Role, int, Snowflake] :param guild_id: The id of the guild to add the roles to the member - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param reason?: The reason why the roles are added :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") - if isinstance(role, Role): - await self._client.add_member_role( - guild_id=guild_id, - user_id=int(self.user.id), - role_id=int(role.id), - reason=reason, - ) - else: - await self._client.add_member_role( - guild_id=guild_id, - user_id=int(self.user.id), - role_id=role, - reason=reason, - ) + + _role = int(role.id) if isinstance(role, Role) else int(role) + _guild_id = int(guild_id) if isinstance(guild_id, (Snowflake, int)) else int(guild_id.id) + + await self._client.add_member_role( + guild_id=_guild_id, + user_id=int(self.user.id), + role_id=_role, + reason=reason, + ) async def remove_role( self, role: Union[Role, int], - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa reason: Optional[str] = None, ) -> None: """ @@ -179,26 +181,22 @@ async def remove_role( :param role: The role to remove. Either ``Role`` object or role_id :type role: Union[Role, int] :param guild_id: The id of the guild to remove the roles of the member - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param reason?: The reason why the roles are removed :type reason: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") - if isinstance(role, Role): - await self._client.remove_member_role( - guild_id=guild_id, - user_id=int(self.user.id), - role_id=int(role.id), - reason=reason, - ) - else: - await self._client.remove_member_role( - guild_id=guild_id, - user_id=int(self.user.id), - role_id=role, - reason=reason, - ) + + _guild_id = int(guild_id) if isinstance(guild_id, (Snowflake, int)) else int(guild_id.id) + _role = int(role.id) if isinstance(role, Role) else int(role) + + await self._client.remove_member_role( + guild_id=_guild_id, + user_id=int(self.user.id), + role_id=_role, + reason=reason, + ) async def send( self, @@ -284,12 +282,12 @@ async def send( async def modify( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa nick: Optional[str] = MISSING, roles: Optional[List[int]] = MISSING, mute: Optional[bool] = MISSING, deaf: Optional[bool] = MISSING, - channel_id: Optional[int] = MISSING, + channel_id: Optional[Union[Channel, int, Snowflake]] = MISSING, communication_disabled_until: Optional[datetime.isoformat] = MISSING, reason: Optional[str] = None, ) -> "Member": @@ -297,7 +295,7 @@ async def modify( Modifies the member of a guild. :param guild_id: The id of the guild to modify the member on - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param nick?: The nickname of the member :type nick: Optional[str] :param roles?: A list of all role ids the member has @@ -307,7 +305,7 @@ async def modify( :param deaf?: whether the user is deafened in voice channels :type deaf: Optional[bool] :param channel_id?: id of channel to move user to (if they are connected to voice) - :type channel_id: Optional[int] + :type channel_id: Optional[Union[Channel, int, Snowflake]] :param communication_disabled_until?: when the user's timeout will expire and the user will be able to communicate in the guild again (up to 28 days in the future) :type communication_disabled_until: Optional[datetime.isoformat] :param reason?: The reason of the modifying @@ -317,6 +315,9 @@ async def modify( """ if not self._client: raise AttributeError("HTTPClient not found!") + + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + payload = {} if nick is not MISSING: payload["nick"] = nick @@ -325,7 +326,7 @@ async def modify( payload["roles"] = roles if channel_id is not MISSING: - payload["channel_id"] = channel_id + payload["channel_id"] = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) if mute is not MISSING: payload["mute"] = mute @@ -338,7 +339,7 @@ async def modify( res = await self._client.modify_member( user_id=int(self.user.id), - guild_id=guild_id, + guild_id=_guild_id, payload=payload, reason=reason, ) @@ -348,32 +349,37 @@ async def modify( async def add_to_thread( self, - thread_id: int, + thread_id: Union[int, Snowflake, Channel], ) -> None: """ Adds the member to a thread. :param thread_id: The id of the thread to add the member to - :type thread_id: int + :type thread_id: Union[int, Snowflake, Channel] """ if not self._client: raise AttributeError("HTTPClient not found!") + + _thread_id = int(thread_id.id) if isinstance(thread_id, Channel) else int(thread_id) + await self._client.add_member_to_thread( user_id=int(self.user.id), - thread_id=thread_id, + thread_id=_thread_id, ) - def get_avatar_url(self, guild_id: int) -> Optional[str]: + def get_avatar_url(self, guild_id: Union[int, Snowflake, "Guild"]) -> Optional[str]: # noqa """ Returns the URL of the member's avatar for the specified guild. :param guild_id: The id of the guild to get the member's avatar from - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :return: URL of the members's avatar (None will be returned if no avatar is set) :rtype: str """ if not self.avatar: return None - url = f"https://cdn.discordapp.com/guilds/{guild_id}/users/{int(self.user.id)}/avatars/{self.avatar}" + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + + url = f"https://cdn.discordapp.com/guilds/{_guild_id}/users/{int(self.user.id)}/avatars/{self.avatar}" url += ".gif" if self.avatar.startswith("a_") else ".png" return url diff --git a/interactions/api/models/member.pyi b/interactions/api/models/member.pyi index 2e27b6c1a..9f8d8bad2 100644 --- a/interactions/api/models/member.pyi +++ b/interactions/api/models/member.pyi @@ -8,6 +8,7 @@ from .message import Embed, Message, MessageInteraction from .misc import File, Snowflake from .role import Role as Role from .user import User as User +from .channel import Channel @define() class Member(ClientSerializerMixin): @@ -33,14 +34,23 @@ class Member(ClientSerializerMixin): @property def name(self) -> str: ... async def ban( - self, guild_id: int, reason: Optional[str] = ..., delete_message_days: Optional[int] = ... + self, + guild_id: Union[int, Snowflake, "Guild"], # noqa + reason: Optional[str] = ..., + delete_message_days: Optional[int] = ... ) -> None: ... - async def kick(self, guild_id: int, reason: Optional[str] = ...) -> None: ... + async def kick(self, guild_id: Union[int, Snowflake, "Guild"], reason: Optional[str] = ...) -> None: ... # noqa async def add_role( - self, role: Union[Role, int], guild_id: int, reason: Optional[str] = ... + self, + role: Union[Role, int, Snowflake], + guild_id: Union[int, Snowflake, "Guild"], # noqa + reason: Optional[str] = ... ) -> None: ... async def remove_role( - self, role: Union[Role, int], guild_id: int, reason: Optional[str] = ... + self, + role: Union[Role, int, Snowflake], + guild_id: Union[int, Snowflake, "Guild"], # noqa + reason: Optional[str] = ... ) -> None: ... async def send( self, @@ -56,14 +66,14 @@ class Member(ClientSerializerMixin): ) -> Message: ... async def modify( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa nick: Optional[str] = ..., roles: Optional[List[int]] = ..., mute: Optional[bool] = ..., deaf: Optional[bool] = ..., - channel_id: Optional[int] = ..., + channel_id: Optional[Union[Channel, int, Snowflake]] = ..., communication_disabled_until: Optional[datetime.isoformat] = ..., reason: Optional[str] = ..., ) -> Member: ... - async def add_to_thread(self, thread_id: int) -> None: ... - def get_member_avatar_url(self, guild_id: int) -> Optional[str]: ... + async def add_to_thread(self, thread_id: Union[int, Snowflake, Channel]) -> None: ... + def get_member_avatar_url(self, guild_id: Union[int, Snowflake, "Guild"]) -> Optional[str]: ... # noqa From ae2cfc07efcd19c89a82a9f0b767e196286acd5d Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 12:19:19 +0200 Subject: [PATCH 07/11] refactor!: Emoji model --- interactions/api/models/message.py | 31 +++++++++++++++++++---------- interactions/api/models/message.pyi | 7 ++++++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/interactions/api/models/message.py b/interactions/api/models/message.py index 641997ee7..d817f59d9 100644 --- a/interactions/api/models/message.py +++ b/interactions/api/models/message.py @@ -188,61 +188,70 @@ class Emoji(ClientSerializerMixin): @classmethod async def get( cls, - guild_id: int, - emoji_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa + emoji_id: Union[int, Snowflake], client: "HTTPClient", # noqa ) -> "Emoji": """ Gets an emoji. :param guild_id: The id of the guild of the emoji - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param emoji_id: The id of the emoji - :type emoji_id: int + :type emoji_id: Union[int, Snowflake] :param client: The HTTPClient of your bot. Equals to ``bot._http`` :type client: HTTPClient :return: The Emoji as object :rtype: Emoji """ - res = await client.get_guild_emoji(guild_id=guild_id, emoji_id=emoji_id) + + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + + res = await client.get_guild_emoji(guild_id=_guild_id, emoji_id=int(emoji_id)) return cls(**res, _client=client) @classmethod async def get_all_of_guild( cls, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa client: "HTTPClient", # noqa ) -> List["Emoji"]: """ Gets all emoji of a guild. :param guild_id: The id of the guild to get the emojis of - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param client: The HTTPClient of your bot. Equals to ``bot._http`` :type client: HTTPClient :return: The Emoji as list :rtype: List[Emoji] """ - res = await client.get_all_emoji(guild_id=guild_id) + + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + + res = await client.get_all_emoji(guild_id=_guild_id) return [cls(**emoji, _client=client) for emoji in res] async def delete( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa reason: Optional[str] = None, ) -> None: """ Deletes the emoji. :param guild_id: The guild id to delete the emoji from - :type guild_id: int + :type guild_id: Union[int, Snowflake, "Guild"] :param reason?: The reason of the deletion :type reason?: Optional[str] """ if not self._client: raise AttributeError("HTTPClient not found!") + + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + return await self._client.delete_guild_emoji( - guild_id=guild_id, emoji_id=int(self.id), reason=reason + guild_id=_guild_id, emoji_id=int(self.id), reason=reason ) @property diff --git a/interactions/api/models/message.pyi b/interactions/api/models/message.pyi index 2c6227d7f..b8e18f284 100644 --- a/interactions/api/models/message.pyi +++ b/interactions/api/models/message.pyi @@ -87,7 +87,12 @@ class Emoji(ClientSerializerMixin): animated: Optional[bool] = None available: Optional[bool] = None @classmethod - async def get(cls, guild_id: int, emoji_id: int, client: HTTPClient) -> Emoji: ... + async def get( + cls, + guild_id: Union[int, Snowflake, "Guild"], + emoji_id: Union[int, Snowflake], + client: HTTPClient + ) -> Emoji: ... @classmethod async def get_all_of_guild(cls, guild_id: int, client: HTTPClient) -> List[Emoji]: ... async def delete(self, guild_id: int, reason: Optional[str] = ...) -> None: ... From 739685e420a7b4205fb001ffd2f61aeb657a0e78 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 12:19:54 +0200 Subject: [PATCH 08/11] refactor!: Emoji model --- interactions/api/models/message.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interactions/api/models/message.pyi b/interactions/api/models/message.pyi index b8e18f284..559296d12 100644 --- a/interactions/api/models/message.pyi +++ b/interactions/api/models/message.pyi @@ -89,13 +89,13 @@ class Emoji(ClientSerializerMixin): @classmethod async def get( cls, - guild_id: Union[int, Snowflake, "Guild"], + guild_id: Union[int, Snowflake, Guild], emoji_id: Union[int, Snowflake], client: HTTPClient ) -> Emoji: ... @classmethod - async def get_all_of_guild(cls, guild_id: int, client: HTTPClient) -> List[Emoji]: ... - async def delete(self, guild_id: int, reason: Optional[str] = ...) -> None: ... + async def get_all_of_guild(cls, guild_id: Union[int, Snowflake, Guild], client: HTTPClient) -> List[Emoji]: ... + async def delete(self, guild_id: Union[int, Snowflake, Guild], reason: Optional[str] = ...) -> None: ... @property def url(self) -> str: ... From 6f17f7753390451bdd4a83ee1c84d37faf12197a Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 12:22:37 +0200 Subject: [PATCH 09/11] refactor!: role model --- interactions/api/models/role.py | 21 ++++++++++++++------- interactions/api/models/role.pyi | 8 ++++---- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/interactions/api/models/role.py b/interactions/api/models/role.py index a4917e8b1..8d7a4397f 100644 --- a/interactions/api/models/role.py +++ b/interactions/api/models/role.py @@ -1,4 +1,4 @@ -from typing import Any, List, Optional +from typing import Any, List, Optional, Union from .attrs_utils import MISSING, ClientSerializerMixin, DictSerializerMixin, define, field from .misc import Image, Snowflake @@ -68,7 +68,7 @@ def mention(self) -> str: async def delete( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa reason: Optional[str] = None, ) -> None: """ @@ -81,13 +81,16 @@ async def delete( """ if not self._client: raise AttributeError("HTTPClient not found!") + + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + await self._client.delete_guild_role( - guild_id=guild_id, role_id=int(self.id), reason=reason + guild_id=_guild_id, role_id=int(self.id), reason=reason ), async def modify( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa name: Optional[str] = MISSING, permissions: Optional[int] = MISSING, color: Optional[int] = MISSING, @@ -130,6 +133,7 @@ async def modify( _permissions = self.permissions if permissions is MISSING else permissions _icon = self.icon if icon is MISSING else icon _unicode_emoji = self.unicode_emoji if unicode_emoji is MISSING else unicode_emoji + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) payload = dict( name=_name, @@ -142,7 +146,7 @@ async def modify( ) res = await self._client.modify_guild_role( - guild_id=guild_id, + guild_id=_guild_id, role_id=int(self.id), payload=payload, reason=reason, @@ -154,7 +158,7 @@ async def modify( async def modify_position( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa position: int, reason: Optional[str] = None, ) -> List["Role"]: @@ -172,8 +176,11 @@ async def modify_position( """ if not self._client: raise AttributeError("HTTPClient not found!") + + _guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id) + res = await self._client.modify_guild_role_positions( - guild_id=guild_id, + guild_id=_guild_id, payload=[{"position": position, "id": int(self.id)}], reason=reason, ) diff --git a/interactions/api/models/role.pyi b/interactions/api/models/role.pyi index f8eb7f503..96edef78b 100644 --- a/interactions/api/models/role.pyi +++ b/interactions/api/models/role.pyi @@ -1,4 +1,4 @@ -from typing import Any, List, Optional +from typing import Any, List, Optional, Union from .attrs_utils import ClientSerializerMixin, DictSerializerMixin, define from .misc import Snowflake @@ -24,10 +24,10 @@ class Role(ClientSerializerMixin): tags: Optional[RoleTags] @property def mention(self) -> str: ... - async def delete(self, guild_id: int, reason: Optional[str] = ...) -> None: ... + async def delete(self, guild_id: Union[int, Snowflake, "Guild"], reason: Optional[str] = ...) -> None: ... # noqa async def modify( self, - guild_id: int, + guild_id: Union[int, Snowflake, "Guild"], # noqa name: Optional[str] = ..., color: Optional[int] = ..., hoist: Optional[bool] = ..., @@ -35,5 +35,5 @@ class Role(ClientSerializerMixin): reason: Optional[str] = ..., ) -> Role: ... async def modify_position( - self, guild_id: int, position: int, reason: Optional[str] = ... + self, guild_id: Union[int, Snowflake, "Guild"], position: int, reason: Optional[str] = ... # noqa ) -> List[Role]: ... From 41e78d9fd560c560c78163822e0a351d96191a4e Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 12:24:27 +0200 Subject: [PATCH 10/11] ci: check --- interactions/api/models/channel.py | 24 ++++++++++++++++++------ interactions/api/models/guild.py | 8 ++++++-- interactions/api/models/member.py | 4 +++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/interactions/api/models/channel.py b/interactions/api/models/channel.py index 43221f525..f2169b40c 100644 --- a/interactions/api/models/channel.py +++ b/interactions/api/models/channel.py @@ -610,7 +610,9 @@ async def add_member( "The Channel you specified is not a thread!" ) # TODO: Move to new error formatter. - _member_id = int(member_id) if isinstance(member_id, (int, Snowflake)) else int(member_id.id) + _member_id = ( + int(member_id) if isinstance(member_id, (int, Snowflake)) else int(member_id.id) + ) await self._client.add_member_to_thread(thread_id=int(self.id), user_id=_member_id) @@ -627,7 +629,9 @@ async def pin_message( if not self._client: raise AttributeError("HTTPClient not found!") - _message_id = int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + _message_id = ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) await self._client.pin_message(channel_id=int(self.id), message_id=_message_id) @@ -644,7 +648,9 @@ async def unpin_message( if not self._client: raise AttributeError("HTTPClient not found!") - _message_id = int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + _message_id = ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) await self._client.unpin_message(channel_id=int(self.id), message_id=_message_id) @@ -663,7 +669,9 @@ async def publish_message( raise AttributeError("HTTPClient not found!") from .message import Message - _message_id = int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + _message_id = ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) res = await self._client.publish_message(channel_id=int(self.id), message_id=_message_id) @@ -944,8 +952,12 @@ async def create_thread( _auto_archive_duration = None if auto_archive_duration is MISSING else auto_archive_duration _invitable = None if invitable is MISSING else invitable - _message_id = None if message_id is MISSING else ( - int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + _message_id = ( + None + if message_id is MISSING + else ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) ) res = await self._client.create_thread( channel_id=int(self.id), diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index 9ceafb8b4..1f5bedd02 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -679,8 +679,12 @@ async def create_thread( _auto_archive_duration = None if auto_archive_duration is MISSING else auto_archive_duration _invitable = None if invitable is MISSING else invitable - _message_id = None if message_id is MISSING else ( - int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + _message_id = ( + None + if message_id is MISSING + else ( + int(message_id) if isinstance(message_id, (int, Snowflake)) else int(message_id.id) + ) ) # work around Message import _channel_id = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) res = await self._client.create_thread( diff --git a/interactions/api/models/member.py b/interactions/api/models/member.py index 4a970d8ff..e5157f2a2 100644 --- a/interactions/api/models/member.py +++ b/interactions/api/models/member.py @@ -326,7 +326,9 @@ async def modify( payload["roles"] = roles if channel_id is not MISSING: - payload["channel_id"] = int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) + payload["channel_id"] = ( + int(channel_id.id) if isinstance(channel_id, Channel) else int(channel_id) + ) if mute is not MISSING: payload["mute"] = mute From e3c3f1e2c79cdef32eca2a01bacadedd1be50a4d Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 9 Jun 2022 12:26:00 +0200 Subject: [PATCH 11/11] fix: Add missing `_` --- interactions/api/models/member.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions/api/models/member.py b/interactions/api/models/member.py index e5157f2a2..2e1ecc36b 100644 --- a/interactions/api/models/member.py +++ b/interactions/api/models/member.py @@ -135,7 +135,7 @@ async def kick( _guild_id = int(guild_id) if isinstance(guild_id, (Snowflake, int)) else int(guild_id.id) await self._client.create_guild_kick( - guild_id=guild_id, + guild_id=_guild_id, user_id=int(self.user.id), reason=reason, )