mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-09-05 01:16:21 +00:00
Add Interaction.channel from Discord payload
Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
This commit is contained in:
@ -2890,7 +2890,12 @@ class DMChannel(discord.abc.Messageable, discord.abc.PrivateChannel, Hashable):
|
|||||||
|
|
||||||
def __init__(self, *, me: ClientUser, state: ConnectionState, data: DMChannelPayload):
|
def __init__(self, *, me: ClientUser, state: ConnectionState, data: DMChannelPayload):
|
||||||
self._state: ConnectionState = state
|
self._state: ConnectionState = state
|
||||||
self.recipient: Optional[User] = state.store_user(data['recipients'][0])
|
self.recipient: Optional[User] = None
|
||||||
|
|
||||||
|
recipients = data.get('recipients')
|
||||||
|
if recipients is not None:
|
||||||
|
self.recipient = state.store_user(recipients[0])
|
||||||
|
|
||||||
self.me: ClientUser = me
|
self.me: ClientUser = me
|
||||||
self.id: int = int(data['id'])
|
self.id: int = int(data['id'])
|
||||||
|
|
||||||
|
@ -25,6 +25,8 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
from typing import Any, Dict, Optional, Generic, TYPE_CHECKING, Sequence, Tuple, Union
|
from typing import Any, Dict, Optional, Generic, TYPE_CHECKING, Sequence, Tuple, Union
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
@ -33,7 +35,7 @@ from . import utils
|
|||||||
from .enums import try_enum, Locale, InteractionType, InteractionResponseType
|
from .enums import try_enum, Locale, InteractionType, InteractionResponseType
|
||||||
from .errors import InteractionResponded, HTTPException, ClientException, DiscordException
|
from .errors import InteractionResponded, HTTPException, ClientException, DiscordException
|
||||||
from .flags import MessageFlags
|
from .flags import MessageFlags
|
||||||
from .channel import PartialMessageable, ChannelType
|
from .channel import ChannelType
|
||||||
from ._types import ClientT
|
from ._types import ClientT
|
||||||
|
|
||||||
from .user import User
|
from .user import User
|
||||||
@ -44,6 +46,7 @@ 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
|
||||||
from .app_commands.namespace import Namespace
|
from .app_commands.namespace import Namespace
|
||||||
from .app_commands.translator import locale_str, TranslationContext, TranslationContextLocation
|
from .app_commands.translator import locale_str, TranslationContext, TranslationContextLocation
|
||||||
|
from .channel import _threaded_channel_factory
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Interaction',
|
'Interaction',
|
||||||
@ -69,12 +72,19 @@ if TYPE_CHECKING:
|
|||||||
from .ui.view import View
|
from .ui.view import View
|
||||||
from .app_commands.models import Choice, ChoiceT
|
from .app_commands.models import Choice, ChoiceT
|
||||||
from .ui.modal import Modal
|
from .ui.modal import Modal
|
||||||
from .channel import VoiceChannel, StageChannel, TextChannel, ForumChannel, CategoryChannel
|
from .channel import VoiceChannel, StageChannel, TextChannel, ForumChannel, CategoryChannel, DMChannel, GroupChannel
|
||||||
from .threads import Thread
|
from .threads import Thread
|
||||||
from .app_commands.commands import Command, ContextMenu
|
from .app_commands.commands import Command, ContextMenu
|
||||||
|
|
||||||
InteractionChannel = Union[
|
InteractionChannel = Union[
|
||||||
VoiceChannel, StageChannel, TextChannel, ForumChannel, CategoryChannel, Thread, PartialMessageable
|
VoiceChannel,
|
||||||
|
StageChannel,
|
||||||
|
TextChannel,
|
||||||
|
ForumChannel,
|
||||||
|
CategoryChannel,
|
||||||
|
Thread,
|
||||||
|
DMChannel,
|
||||||
|
GroupChannel,
|
||||||
]
|
]
|
||||||
|
|
||||||
MISSING: Any = utils.MISSING
|
MISSING: Any = utils.MISSING
|
||||||
@ -96,8 +106,10 @@ class Interaction(Generic[ClientT]):
|
|||||||
The interaction type.
|
The interaction type.
|
||||||
guild_id: Optional[:class:`int`]
|
guild_id: Optional[:class:`int`]
|
||||||
The guild ID the interaction was sent from.
|
The guild ID the interaction was sent from.
|
||||||
channel_id: Optional[:class:`int`]
|
channel: Optional[Union[:class:`abc.GuildChannel`, :class:`abc.PrivateChannel`, :class:`Thread`]]
|
||||||
The channel ID the interaction was sent from.
|
The channel the interaction was sent from.
|
||||||
|
|
||||||
|
Note that due to a Discord limitation, if sent from a DM channel :attr:`~DMChannel.recipient` is ``None``.
|
||||||
application_id: :class:`int`
|
application_id: :class:`int`
|
||||||
The application ID that the interaction was for.
|
The application ID that the interaction was for.
|
||||||
user: Union[:class:`User`, :class:`Member`]
|
user: Union[:class:`User`, :class:`Member`]
|
||||||
@ -128,7 +140,6 @@ class Interaction(Generic[ClientT]):
|
|||||||
'id',
|
'id',
|
||||||
'type',
|
'type',
|
||||||
'guild_id',
|
'guild_id',
|
||||||
'channel_id',
|
|
||||||
'data',
|
'data',
|
||||||
'application_id',
|
'application_id',
|
||||||
'message',
|
'message',
|
||||||
@ -148,7 +159,7 @@ class Interaction(Generic[ClientT]):
|
|||||||
'_original_response',
|
'_original_response',
|
||||||
'_cs_response',
|
'_cs_response',
|
||||||
'_cs_followup',
|
'_cs_followup',
|
||||||
'_cs_channel',
|
'channel',
|
||||||
'_cs_namespace',
|
'_cs_namespace',
|
||||||
'_cs_command',
|
'_cs_command',
|
||||||
)
|
)
|
||||||
@ -171,8 +182,24 @@ class Interaction(Generic[ClientT]):
|
|||||||
self.data: Optional[InteractionData] = data.get('data')
|
self.data: Optional[InteractionData] = data.get('data')
|
||||||
self.token: str = data['token']
|
self.token: str = data['token']
|
||||||
self.version: int = data['version']
|
self.version: int = data['version']
|
||||||
self.channel_id: Optional[int] = utils._get_as_snowflake(data, 'channel_id')
|
|
||||||
self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id')
|
self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id')
|
||||||
|
self.channel: Optional[InteractionChannel] = None
|
||||||
|
|
||||||
|
raw_channel = data.get('channel', {})
|
||||||
|
raw_ch_type = raw_channel.get('type')
|
||||||
|
if raw_ch_type is not None:
|
||||||
|
factory, ch_type = _threaded_channel_factory(raw_ch_type) # type is never None
|
||||||
|
if factory is None:
|
||||||
|
logging.info('Unknown channel type {type} for channel ID {id}.'.format_map(raw_channel))
|
||||||
|
else:
|
||||||
|
if ch_type in (ChannelType.group, ChannelType.private):
|
||||||
|
channel = factory(me=self._client.user, data=raw_channel, state=self._state) # type: ignore
|
||||||
|
else:
|
||||||
|
guild = self._state._get_or_create_unavailable_guild(self.guild_id) # type: ignore
|
||||||
|
channel = factory(guild=guild, state=self._state, data=raw_channel) # type: ignore
|
||||||
|
|
||||||
|
self.channel = channel
|
||||||
|
|
||||||
self.application_id: int = int(data['application_id'])
|
self.application_id: int = int(data['application_id'])
|
||||||
|
|
||||||
self.locale: Locale = try_enum(Locale, data.get('locale', 'en-US'))
|
self.locale: Locale = try_enum(Locale, data.get('locale', 'en-US'))
|
||||||
@ -227,21 +254,10 @@ class Interaction(Generic[ClientT]):
|
|||||||
"""Optional[:class:`Guild`]: The guild the interaction was sent from."""
|
"""Optional[:class:`Guild`]: The guild the interaction was sent from."""
|
||||||
return self._state and self._state._get_guild(self.guild_id)
|
return self._state and self._state._get_guild(self.guild_id)
|
||||||
|
|
||||||
@utils.cached_slot_property('_cs_channel')
|
@property
|
||||||
def channel(self) -> Optional[InteractionChannel]:
|
def channel_id(self) -> Optional[int]:
|
||||||
"""Optional[Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]]: The channel the interaction was sent from.
|
"""Optional[:class:`int`]: The ID of the channel the interaction was sent from."""
|
||||||
|
return self.channel.id if self.channel is not None else None
|
||||||
Note that due to a Discord limitation, DM channels are not resolved since there is
|
|
||||||
no data to complete them. These are :class:`PartialMessageable` instead.
|
|
||||||
"""
|
|
||||||
guild = self.guild
|
|
||||||
channel = guild and guild._resolve_channel(self.channel_id)
|
|
||||||
if channel is None:
|
|
||||||
if self.channel_id is not None:
|
|
||||||
type = ChannelType.text if self.guild_id is not None else ChannelType.private
|
|
||||||
return PartialMessageable(state=self._state, guild_id=self.guild_id, id=self.channel_id, type=type)
|
|
||||||
return None
|
|
||||||
return channel
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def permissions(self) -> Permissions:
|
def permissions(self) -> Permissions:
|
||||||
|
@ -150,16 +150,24 @@ class ForumChannel(_BaseTextChannel):
|
|||||||
GuildChannel = Union[TextChannel, NewsChannel, VoiceChannel, CategoryChannel, StageChannel, ThreadChannel, ForumChannel]
|
GuildChannel = Union[TextChannel, NewsChannel, VoiceChannel, CategoryChannel, StageChannel, ThreadChannel, ForumChannel]
|
||||||
|
|
||||||
|
|
||||||
class DMChannel(_BaseChannel):
|
class _BaseDMChannel(_BaseChannel):
|
||||||
type: Literal[1]
|
type: Literal[1]
|
||||||
last_message_id: Optional[Snowflake]
|
last_message_id: Optional[Snowflake]
|
||||||
|
|
||||||
|
|
||||||
|
class DMChannel(_BaseDMChannel):
|
||||||
recipients: List[PartialUser]
|
recipients: List[PartialUser]
|
||||||
|
|
||||||
|
|
||||||
|
class InteractionDMChannel(_BaseDMChannel):
|
||||||
|
recipients: NotRequired[List[PartialUser]]
|
||||||
|
|
||||||
|
|
||||||
class GroupDMChannel(_BaseChannel):
|
class GroupDMChannel(_BaseChannel):
|
||||||
type: Literal[3]
|
type: Literal[3]
|
||||||
icon: Optional[str]
|
icon: Optional[str]
|
||||||
owner_id: Snowflake
|
owner_id: Snowflake
|
||||||
|
recipients: List[PartialUser]
|
||||||
|
|
||||||
|
|
||||||
Channel = Union[GuildChannel, DMChannel, GroupDMChannel]
|
Channel = Union[GuildChannel, DMChannel, GroupDMChannel]
|
||||||
|
@ -27,7 +27,7 @@ from __future__ import annotations
|
|||||||
from typing import TYPE_CHECKING, Dict, List, Literal, TypedDict, Union
|
from typing import TYPE_CHECKING, Dict, List, Literal, TypedDict, Union
|
||||||
from typing_extensions import NotRequired
|
from typing_extensions import NotRequired
|
||||||
|
|
||||||
from .channel import ChannelTypeWithoutThread, ThreadMetadata
|
from .channel import ChannelTypeWithoutThread, ThreadMetadata, GuildChannel, InteractionDMChannel, GroupDMChannel
|
||||||
from .threads import ThreadType
|
from .threads import ThreadType
|
||||||
from .member import Member
|
from .member import Member
|
||||||
from .message import Attachment
|
from .message import Attachment
|
||||||
@ -204,6 +204,7 @@ class _BaseInteraction(TypedDict):
|
|||||||
version: Literal[1]
|
version: Literal[1]
|
||||||
guild_id: NotRequired[Snowflake]
|
guild_id: NotRequired[Snowflake]
|
||||||
channel_id: NotRequired[Snowflake]
|
channel_id: NotRequired[Snowflake]
|
||||||
|
channel: Union[GuildChannel, InteractionDMChannel, GroupDMChannel]
|
||||||
app_permissions: NotRequired[str]
|
app_permissions: NotRequired[str]
|
||||||
locale: NotRequired[str]
|
locale: NotRequired[str]
|
||||||
guild_locale: NotRequired[str]
|
guild_locale: NotRequired[str]
|
||||||
|
Reference in New Issue
Block a user