Browse Source

Fallback to an unavailable guild instead of an Object in interactions

pull/8041/head
Rapptz 3 years ago
parent
commit
573bb32a1e
  1. 2
      discord/app_commands/namespace.py
  2. 4
      discord/guild.py
  3. 7
      discord/interactions.py
  4. 3
      discord/state.py

2
discord/app_commands/namespace.py

@ -168,7 +168,7 @@ class Namespace:
state = interaction._state state = interaction._state
members = resolved.get('members', {}) members = resolved.get('members', {})
guild_id = interaction.guild_id 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 type = AppCommandOptionType.user.value
for (user_id, user_data) in resolved.get('users', {}).items(): for (user_id, user_data) in resolved.get('users', {}).items():
try: try:

4
discord/guild.py

@ -452,6 +452,10 @@ class Guild(Hashable):
return role 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: def _from_data(self, guild: GuildPayload) -> None:
try: try:
self._member_count = guild['member_count'] self._member_count = guild['member_count']

7
discord/interactions.py

@ -38,7 +38,6 @@ from .channel import PartialMessageable, ChannelType
from .user import User from .user import User
from .member import Member from .member import Member
from .message import Message, Attachment from .message import Message, Attachment
from .object import Object
from .permissions import Permissions from .permissions import Permissions
from .http import handle_message_parameters from .http import handle_message_parameters
from .webhook.async_ import async_context, Webhook, interaction_response_params, interaction_message_response_params 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.user: Union[User, Member] = MISSING
self._permissions: int = 0 self._permissions: int = 0
# TODO: there's a potential data loss here
if self.guild_id: 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: try:
member = data['member'] # type: ignore # The key is optional and handled member = data['member'] # type: ignore # The key is optional and handled
except KeyError: except KeyError:
pass pass
else: 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)
self.user = Member(state=self._state, guild=guild, data=member) # type: ignore
self._permissions = self.user._permissions or 0 self._permissions = self.user._permissions or 0
else: else:
try: try:

3
discord/state.py

@ -381,6 +381,9 @@ class ConnectionState:
# the keys of self._guilds are ints # the keys of self._guilds are ints
return self._guilds.get(guild_id) # type: ignore 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: def _add_guild(self, guild: Guild) -> None:
self._guilds[guild.id] = guild self._guilds[guild.id] = guild

Loading…
Cancel
Save