Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction class context, is_guild_integration and is_user_integration #9792

Merged
merged 13 commits into from May 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions discord/app_commands/installs.py
Expand Up @@ -32,6 +32,7 @@

if TYPE_CHECKING:
from typing_extensions import Self
from ..types.interactions import InteractionContextType, InteractionInstallationType


class AppInstallationType:
Expand Down Expand Up @@ -84,14 +85,14 @@ def merge(self, other: AppInstallationType) -> AppInstallationType:
def _is_unset(self) -> bool:
return all(x is None for x in (self._guild, self._user))

def _merge_to_array(self, other: Optional[AppInstallationType]) -> Optional[list[int]]:
def _merge_to_array(self, other: Optional[AppInstallationType]) -> Optional[Sequence[InteractionInstallationType]]:
result = self.merge(other) if other is not None else self
if result._is_unset():
return None
return result.to_array()

@classmethod
def _from_value(cls, value: Sequence[int]) -> Self:
def _from_value(cls, value: Sequence[InteractionInstallationType]) -> Self:
self = cls()
for x in value:
if x == cls.GUILD:
Expand All @@ -100,7 +101,7 @@ def _from_value(cls, value: Sequence[int]) -> Self:
self._user = True
return self

def to_array(self) -> list[int]:
def to_array(self) -> Sequence[InteractionInstallationType]:
values = []
if self._guild:
values.append(self.GUILD)
Expand Down Expand Up @@ -177,14 +178,14 @@ def merge(self, other: AppCommandContext) -> AppCommandContext:
def _is_unset(self) -> bool:
return all(x is None for x in (self._guild, self._dm_channel, self._private_channel))

def _merge_to_array(self, other: Optional[AppCommandContext]) -> Optional[list[int]]:
def _merge_to_array(self, other: Optional[AppCommandContext]) -> Optional[Sequence[InteractionContextType]]:
result = self.merge(other) if other is not None else self
if result._is_unset():
return None
return result.to_array()

@classmethod
def _from_value(cls, value: Sequence[int]) -> Self:
def _from_value(cls, value: Sequence[InteractionContextType]) -> Self:
self = cls()
for x in value:
if x == cls.GUILD:
Expand All @@ -195,7 +196,7 @@ def _from_value(cls, value: Sequence[int]) -> Self:
self._private_channel = True
return self

def to_array(self) -> list[int]:
def to_array(self) -> Sequence[InteractionContextType]:
values = []
if self._guild:
values.append(self.GUILD)
Expand Down
18 changes: 18 additions & 0 deletions discord/interactions.py
Expand Up @@ -45,6 +45,7 @@
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
Expand Down Expand Up @@ -140,6 +141,8 @@ 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.
Vioshim marked this conversation as resolved.
Show resolved Hide resolved
"""

__slots__: Tuple[str, ...] = (
Expand All @@ -158,6 +161,7 @@ class Interaction(Generic[ClientT]):
'command_failed',
'entitlement_sku_ids',
'entitlements',
"context",
'_integration_owners',
'_permissions',
'_app_permissions',
Expand Down Expand Up @@ -200,6 +204,10 @@ def _from_data(self, data: InteractionPayload):
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]
Expand Down Expand Up @@ -380,6 +388,16 @@ def is_expired(self) -> bool:
""":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."""
Vioshim marked this conversation as resolved.
Show resolved Hide resolved
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."""
Vioshim marked this conversation as resolved.
Show resolved Hide resolved
return self.user.id == self._integration_owners.get(1)

async def original_response(self) -> InteractionMessage:
"""|coro|

Expand Down
1 change: 1 addition & 0 deletions discord/types/interactions.py
Expand Up @@ -43,6 +43,7 @@

InteractionType = Literal[1, 2, 3, 4, 5]
InteractionContextType = Literal[0, 1, 2]
InteractionInstallationType = Literal[0, 1]


class _BasePartialChannel(TypedDict):
Expand Down