diff --git a/discord/app_commands/namespace.py b/discord/app_commands/namespace.py index 81d8f35ad..bd6b0f833 100644 --- a/discord/app_commands/namespace.py +++ b/discord/app_commands/namespace.py @@ -168,7 +168,7 @@ class Namespace: state = interaction._state members = resolved.get('members', {}) guild_id = interaction.guild_id - guild = (state._get_guild(guild_id) or Object(id=guild_id)) if guild_id is not None else None + guild = state._get_or_create_unavailable_guild(guild_id) if guild_id is not None else None type = AppCommandOptionType.user.value for (user_id, user_data) in resolved.get('users', {}).items(): try: diff --git a/discord/guild.py b/discord/guild.py index f00b25958..65259e607 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -452,6 +452,10 @@ class Guild(Hashable): return role + @classmethod + def _create_unavailable(cls, *, state: ConnectionState, guild_id: int) -> Guild: + return cls(state=state, data={'id': guild_id, 'unavailable': True}) # type: ignore + def _from_data(self, guild: GuildPayload) -> None: try: self._member_count = guild['member_count'] diff --git a/discord/interactions.py b/discord/interactions.py index 8a8d0c9d5..a7d74f318 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -38,7 +38,6 @@ from .channel import PartialMessageable, ChannelType from .user import User from .member import Member from .message import Message, Attachment -from .object import Object from .permissions import Permissions from .http import handle_message_parameters from .webhook.async_ import async_context, Webhook, interaction_response_params, interaction_message_response_params @@ -184,16 +183,14 @@ class Interaction: self.user: Union[User, Member] = MISSING self._permissions: int = 0 - # TODO: there's a potential data loss here if self.guild_id: - guild = self.guild or Object(id=self.guild_id) + guild = self._state._get_or_create_unavailable_guild(self.guild_id) try: member = data['member'] # type: ignore # The key is optional and handled except KeyError: pass else: - # The fallback to Object for guild causes a type check error but is explicitly allowed here - self.user = Member(state=self._state, guild=guild, data=member) # type: ignore + self.user = Member(state=self._state, guild=guild, data=member) self._permissions = self.user._permissions or 0 else: try: diff --git a/discord/state.py b/discord/state.py index 36cf47ae6..196d88c5d 100644 --- a/discord/state.py +++ b/discord/state.py @@ -381,6 +381,9 @@ class ConnectionState: # the keys of self._guilds are ints return self._guilds.get(guild_id) # type: ignore + def _get_or_create_unavailable_guild(self, guild_id: int) -> Guild: + return self._guilds.get(guild_id) or Guild._create_unavailable(state=self, guild_id=guild_id) + def _add_guild(self, guild: Guild) -> None: self._guilds[guild.id] = guild