Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions interactions/api/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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)

async def get_permissions_for(self, member: "Member") -> Permissions:
"""
Expand Down
5 changes: 4 additions & 1 deletion interactions/api/models/gw.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,10 @@ 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,
)

async def get_guild(self) -> "Guild":
"""
Expand Down
22 changes: 21 additions & 1 deletion interactions/api/models/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,33 @@ 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

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 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]]
"""

if hasattr(self, "_guild_id"):
Expand Down