From 8f4c6c5e876bbd30421a4f925943f20f2a738d00 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 17 Nov 2022 21:04:59 +0100 Subject: [PATCH 1/5] feat: add new guild properties to member and channel --- interactions/api/models/channel.py | 56 +++++++++++++++++++++++++++--- interactions/api/models/guild.py | 6 ++-- interactions/api/models/gw.py | 6 +++- interactions/api/models/member.py | 23 +++++++++++- interactions/api/models/message.py | 4 +-- interactions/api/models/webhook.py | 2 +- interactions/client/context.py | 2 +- 7 files changed, 87 insertions(+), 12 deletions(-) diff --git a/interactions/api/models/channel.py b/interactions/api/models/channel.py index bc3e6ce10..7a4a87b4d 100644 --- a/interactions/api/models/channel.py +++ b/interactions/api/models/channel.py @@ -16,6 +16,7 @@ field, ) from ...utils.missing import MISSING +from ...utils.utils import search_iterable from ..error import LibraryException from .emoji import Emoji from .flags import Permissions @@ -26,7 +27,7 @@ if TYPE_CHECKING: from ...client.models.component import ActionRow, Button, SelectMenu from ..http.client import HTTPClient - from .guild import Invite, InviteTargetType + from .guild import Guild, Invite, InviteTargetType from .gw import VoiceState from .member import Member from .message import Attachment, Embed, Message, Sticker @@ -417,7 +418,9 @@ class Channel(ClientSerializerMixin, IDMixin): type: ChannelType = field(converter=ChannelType) id: Snowflake = field(converter=Snowflake) - guild_id: Optional[Snowflake] = field(converter=Snowflake, default=None) + _guild_id: Optional[Snowflake] = field( + converter=Snowflake, default=None, discord_name="guild_id" + ) position: Optional[int] = field(default=None) permission_overwrites: Optional[List[Overwrite]] = field( converter=convert_list(Overwrite), factory=list @@ -462,6 +465,51 @@ def __attrs_post_init__(self): # sourcery skip: last-if-guard if not self.recipients: self.recipients = channel.recipients + @property + def guild_id(self) -> Optional[Snowflake]: + """ + .. versionadded:: 4.4.0 + + Attempts to get the guild ID the channel is in. + + :return: The ID of the guild this channel belongs to. + :rtype: Optional[Snowflake] + """ + + if self._guild_id: + return self._guild_id + + elif _id := self._extras.get("guild_id"): + return Snowflake(_id) + + if not self._client: + raise LibraryException(code=13) + + from .guild import Guild + + def check(channel: Channel): + return self.id == channel.id + + for guild in self._client.cache[Guild].values.values(): + if len(search_iterable(guild.channels, check=check)) == 1: + self._extras["guild_id"] = guild.id + return guild.id + + @property + def guild(self) -> Optional["Guild"]: + """ + .. versionadded:: 4.4.0 + + Attempts to get the guild the channel is in. + + :return: The guild this channel belongs to. + :rtype: Guild + """ + _id = self.guild_id + from .guild import Guild + + return self._client.cache[Guild].get(_id, None) if _id else None + @property def typing(self) -> Union[Awaitable, ContextManager]: """ @@ -1339,7 +1387,7 @@ async def create_thread( reason=reason, ) - return Channel(**res, _client=self._client) + return Channel(**res, _client=self._client, guild_id=self.guild_id) @property def url(self) -> str: @@ -1767,7 +1815,7 @@ async def create_forum_post( data = await self._client.create_thread_in_forum(int(self.id), **_top_payload) - return Channel(**data) + return Channel(**data, _client=self._client, guild_id=self.guild_id) async def get_permissions_for(self, member: "Member") -> Permissions: """ diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index 839ff5182..abefd115d 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -903,7 +903,7 @@ async def create_thread( reason=reason, ) - return Channel(**res, _client=self._client) + return Channel(**res, _client=self._client, guild_id=self.id) async def create_channel( self, @@ -1752,7 +1752,9 @@ async def get_all_channels(self) -> List[Channel]: if not self._client: raise LibraryException(code=13) res = await self._client.get_all_channels(int(self.id)) - self.channels = [Channel(**channel, _client=self._client) for channel in res] + self.channels = [ + Channel(**channel, _client=self._client, guild_id=self.id) for channel in res + ] return self.channels async def get_all_active_threads(self) -> List[Channel]: diff --git a/interactions/api/models/gw.py b/interactions/api/models/gw.py index a4c65e5c2..25b6d7471 100644 --- a/interactions/api/models/gw.py +++ b/interactions/api/models/gw.py @@ -666,7 +666,11 @@ async def get_channel(self) -> Channel: :rtype: Channel """ - return Channel(**await self._client.get_channel(int(self.channel_id)), _client=self._client) + return Channel( + **await self._client.get_channel(int(self.channel_id)), + _client=self._client, + guild_id=self.guild_id + ) async def get_guild(self) -> "Guild": """ diff --git a/interactions/api/models/member.py b/interactions/api/models/member.py index 4ebcf9946..ed9f13f63 100644 --- a/interactions/api/models/member.py +++ b/interactions/api/models/member.py @@ -87,13 +87,34 @@ def voice_state(self) -> Optional["VoiceState"]: return self._client.cache[VoiceState].get(self.id) + @property + def guild(self) -> Optional["Guild"]: + """ + .. versionadded:: 4.4.0 + + Attempts to get the guild the member is in. + Only works then roles or nick or joined at is present and the guild is cached. + + :return: The guild this member belongs to. + :rtype: Guild + """ + _id = self.guild_id + from .guild import Guild + + if not _id or isinstance(_id, LibraryException): + return None + + else: + return self._client.cache[Guild].get(_id, None) + @property def guild_id(self) -> Optional[Union[Snowflake, LibraryException]]: """ Attempts to get the guild ID the member is in. - Only works then roles or nick or joined at is present. + Only works then roles or nick or joined at is present and the guild is cached. :return: The ID of the guild this member belongs to. + :rtype: Optional[Union[Snowflake, LibraryException]] """ if hasattr(self, "_guild_id"): diff --git a/interactions/api/models/message.py b/interactions/api/models/message.py index 4f83e0f0d..62df7dc7e 100644 --- a/interactions/api/models/message.py +++ b/interactions/api/models/message.py @@ -813,7 +813,7 @@ async def get_channel(self) -> Channel: if not self._client: raise LibraryException(code=13) res = await self._client.get_channel(channel_id=int(self.channel_id)) - return Channel(**res, _client=self._client) + return Channel(**res, _client=self._client, guild_id=self.guild_id) async def get_guild(self): """ @@ -1103,7 +1103,7 @@ async def create_thread( invitable=_invitable, auto_archive_duration=_auto_archive_duration, ) - return Channel(**res, _client=self._client) + return Channel(**res, _client=self._client, guild_id=self.guild_id) async def create_reaction( self, diff --git a/interactions/api/models/webhook.py b/interactions/api/models/webhook.py index d7addba97..6e75072a6 100644 --- a/interactions/api/models/webhook.py +++ b/interactions/api/models/webhook.py @@ -67,7 +67,7 @@ def __attrs_post_init__(self): else None ) self.source_channel = ( - Channel(**self.source_channel, _client=self._client) + Channel(**self.source_channel, _client=self._client, guild_id=self.guild_id) if self._json.get("source_channel") else None ) diff --git a/interactions/client/context.py b/interactions/client/context.py index 4d43c16f8..e349d8f44 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -91,7 +91,7 @@ async def get_channel(self) -> Channel: """ res = await self._client.get_channel(int(self.channel_id)) - self.channel = Channel(**res, _client=self._client) + self.channel = Channel(**res, _client=self._client, guild_id=self.guild_id) return self.channel async def get_guild(self) -> Guild: From 3c8d2f592c087853cd363e61a9e1e726c245b82b Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 17 Nov 2022 21:10:11 +0100 Subject: [PATCH 2/5] revert some stuff --- interactions/api/models/channel.py | 4 ++-- interactions/api/models/guild.py | 6 ++---- interactions/api/models/gw.py | 1 - interactions/api/models/message.py | 4 ++-- interactions/client/context.py | 2 +- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/interactions/api/models/channel.py b/interactions/api/models/channel.py index 7a4a87b4d..4d37eea7b 100644 --- a/interactions/api/models/channel.py +++ b/interactions/api/models/channel.py @@ -1387,7 +1387,7 @@ async def create_thread( reason=reason, ) - return Channel(**res, _client=self._client, guild_id=self.guild_id) + return Channel(**res, _client=self._client) @property def url(self) -> str: @@ -1815,7 +1815,7 @@ async def create_forum_post( data = await self._client.create_thread_in_forum(int(self.id), **_top_payload) - return Channel(**data, _client=self._client, guild_id=self.guild_id) + return Channel(**data, _client=self._client) async def get_permissions_for(self, member: "Member") -> Permissions: """ diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index abefd115d..839ff5182 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -903,7 +903,7 @@ async def create_thread( reason=reason, ) - return Channel(**res, _client=self._client, guild_id=self.id) + return Channel(**res, _client=self._client) async def create_channel( self, @@ -1752,9 +1752,7 @@ async def get_all_channels(self) -> List[Channel]: if not self._client: raise LibraryException(code=13) res = await self._client.get_all_channels(int(self.id)) - self.channels = [ - Channel(**channel, _client=self._client, guild_id=self.id) for channel in res - ] + self.channels = [Channel(**channel, _client=self._client) for channel in res] return self.channels async def get_all_active_threads(self) -> List[Channel]: diff --git a/interactions/api/models/gw.py b/interactions/api/models/gw.py index 25b6d7471..db2261a6f 100644 --- a/interactions/api/models/gw.py +++ b/interactions/api/models/gw.py @@ -669,7 +669,6 @@ async def get_channel(self) -> Channel: return Channel( **await self._client.get_channel(int(self.channel_id)), _client=self._client, - guild_id=self.guild_id ) async def get_guild(self) -> "Guild": diff --git a/interactions/api/models/message.py b/interactions/api/models/message.py index 62df7dc7e..4f83e0f0d 100644 --- a/interactions/api/models/message.py +++ b/interactions/api/models/message.py @@ -813,7 +813,7 @@ async def get_channel(self) -> Channel: if not self._client: raise LibraryException(code=13) res = await self._client.get_channel(channel_id=int(self.channel_id)) - return Channel(**res, _client=self._client, guild_id=self.guild_id) + return Channel(**res, _client=self._client) async def get_guild(self): """ @@ -1103,7 +1103,7 @@ async def create_thread( invitable=_invitable, auto_archive_duration=_auto_archive_duration, ) - return Channel(**res, _client=self._client, guild_id=self.guild_id) + return Channel(**res, _client=self._client) async def create_reaction( self, diff --git a/interactions/client/context.py b/interactions/client/context.py index e349d8f44..4d43c16f8 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -91,7 +91,7 @@ async def get_channel(self) -> Channel: """ res = await self._client.get_channel(int(self.channel_id)) - self.channel = Channel(**res, _client=self._client, guild_id=self.guild_id) + self.channel = Channel(**res, _client=self._client) return self.channel async def get_guild(self) -> Guild: From acb7272f19fa358c82ad0c3f3393d1915c5021ab Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 17 Nov 2022 21:11:25 +0100 Subject: [PATCH 3/5] revert some stuff --- interactions/api/models/webhook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions/api/models/webhook.py b/interactions/api/models/webhook.py index 6e75072a6..d7addba97 100644 --- a/interactions/api/models/webhook.py +++ b/interactions/api/models/webhook.py @@ -67,7 +67,7 @@ def __attrs_post_init__(self): else None ) self.source_channel = ( - Channel(**self.source_channel, _client=self._client, guild_id=self.guild_id) + Channel(**self.source_channel, _client=self._client) if self._json.get("source_channel") else None ) From 3914ee6a697404ffd49cb52c5904f6abcfd546f1 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Thu, 17 Nov 2022 21:13:28 +0100 Subject: [PATCH 4/5] Update interactions/api/models/member.py Co-authored-by: Max --- 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 ed9f13f63..6372e4475 100644 --- a/interactions/api/models/member.py +++ b/interactions/api/models/member.py @@ -111,7 +111,7 @@ def guild(self) -> Optional["Guild"]: def guild_id(self) -> Optional[Union[Snowflake, LibraryException]]: """ Attempts to get the guild ID the member is in. - Only works then roles or nick or joined at is present and the guild is cached. + Only works when roles or nick or joined at is present and the guild is cached. :return: The ID of the guild this member belongs to. :rtype: Optional[Union[Snowflake, LibraryException]] From dbdcb44fb462e9462412ac9f4bb8f4555182207d Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Fri, 18 Nov 2022 13:43:33 +0100 Subject: [PATCH 5/5] Update interactions/api/models/member.py Co-authored-by: Damego --- interactions/api/models/member.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/interactions/api/models/member.py b/interactions/api/models/member.py index 6372e4475..3737d49f2 100644 --- a/interactions/api/models/member.py +++ b/interactions/api/models/member.py @@ -102,10 +102,9 @@ def guild(self) -> Optional["Guild"]: from .guild import Guild if not _id or isinstance(_id, LibraryException): - return None + return - else: - return self._client.cache[Guild].get(_id, None) + return self._client.cache[Guild].get(_id, None) @property def guild_id(self) -> Optional[Union[Snowflake, LibraryException]]: