First pass at supporting user apps

Co-authored-by: red <red@kalab.sk>
Co-authored-by: Vioshim <63890837+Vioshim@users.noreply.github.com>
This commit is contained in:
Danny
2024-05-04 23:25:01 -04:00
committed by GitHub
parent 2892401992
commit 2e2f51fd5c
19 changed files with 920 additions and 52 deletions

View File

@ -45,6 +45,7 @@ from .message import Message, Attachment
from .permissions import Permissions
from .http import handle_message_parameters
from .webhook.async_ import async_context, Webhook, interaction_response_params, interaction_message_response_params
from .app_commands.installs import AppCommandContext
from .app_commands.namespace import Namespace
from .app_commands.translator import locale_str, TranslationContext, TranslationContextLocation
from .channel import _threaded_channel_factory
@ -64,6 +65,7 @@ if TYPE_CHECKING:
from .types.webhook import (
Webhook as WebhookPayload,
)
from .types.snowflake import Snowflake
from .guild import Guild
from .state import ConnectionState
from .file import File
@ -139,6 +141,10 @@ class Interaction(Generic[ClientT]):
command_failed: :class:`bool`
Whether the command associated with this interaction failed to execute.
This includes checks and execution.
context: :class:`.AppCommandContext`
The context of the interaction.
.. versionadded:: 2.4
"""
__slots__: Tuple[str, ...] = (
@ -157,6 +163,8 @@ class Interaction(Generic[ClientT]):
'command_failed',
'entitlement_sku_ids',
'entitlements',
"context",
'_integration_owners',
'_permissions',
'_app_permissions',
'_state',
@ -194,6 +202,14 @@ class Interaction(Generic[ClientT]):
self.application_id: int = int(data['application_id'])
self.entitlement_sku_ids: List[int] = [int(x) for x in data.get('entitlement_skus', []) or []]
self.entitlements: List[Entitlement] = [Entitlement(self._state, x) for x in data.get('entitlements', [])]
# This is not entirely useful currently, unsure how to expose it in a way that it is.
self._integration_owners: Dict[int, Snowflake] = {
int(k): int(v) for k, v in data.get('authorizing_integration_owners', {}).items()
}
try:
self.context = AppCommandContext._from_value([data['context']])
except KeyError:
self.context = AppCommandContext()
self.locale: Locale = try_enum(Locale, data.get('locale', 'en-US'))
self.guild_locale: Optional[Locale]
@ -204,7 +220,10 @@ class Interaction(Generic[ClientT]):
guild = None
if self.guild_id:
guild = self._state._get_or_create_unavailable_guild(self.guild_id)
# The data type is a TypedDict but it doesn't narrow to Dict[str, Any] properly
guild = self._state._get_or_create_unavailable_guild(self.guild_id, data=data.get('guild')) # type: ignore
if guild.me is None and self._client.user is not None:
guild._add_member(Member._from_client_user(user=self._client.user, guild=guild, state=self._state))
raw_channel = data.get('channel', {})
channel_id = utils._get_as_snowflake(raw_channel, 'id')
@ -371,6 +390,22 @@ class Interaction(Generic[ClientT]):
""":class:`bool`: Returns ``True`` if the interaction is expired."""
return utils.utcnow() >= self.expires_at
def is_guild_integration(self) -> bool:
""":class:`bool`: Returns ``True`` if the interaction is a guild integration.
.. versionadded:: 2.4
"""
if self.guild_id:
return self.guild_id == self._integration_owners.get(0)
return False
def is_user_integration(self) -> bool:
""":class:`bool`: Returns ``True`` if the interaction is a user integration.
.. versionadded:: 2.4
"""
return self.user.id == self._integration_owners.get(1)
async def original_response(self) -> InteractionMessage:
"""|coro|