Skip to content

Commit 3672c12

Browse files
authored
Fixed issues with Interaction.channel
New-Version: 1.7.5.4 - Fixed issues with Interaction.channel - added a `created_at` attribute to `Interaction`
1 parent b731315 commit 3672c12

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

discord/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
__author__ = 'Rapptz & mccoderpy'
1616
__license__ = 'MIT'
1717
__copyright__ = 'Copyright 2015-present Rapptz'
18-
__version__ = '1.7.5.3'
18+
__version__ = '1.7.5.4'
1919

2020
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
2121

discord/interactions.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def __init__(self, state, data):
7676
self.user_id = int(self._user['id'])
7777
self.__interaction_id = int(data.get('id'))
7878
self.guild_id = int(data.get('guild_id', 0))
79+
self._guild = None
80+
self._channel = None
7981
self.channel_id = int(data.get('channel_id', 0))
8082
self.__application_id = int(data.get('application_id'))
8183
self.message: typing.Union[Message, EphemeralMessage] = EphemeralMessage() if self.message_is_hidden else None
@@ -89,20 +91,37 @@ def __init__(self, state, data):
8991
# maybe ``later`` this library will also supports Slash-Commands
9092
# self.command = None
9193

92-
__slots__ = ('_state', '_http', '_data', 'member', '_member', '_user', 'user_id', 'guild_id', 'channel_id', 'message_id',
93-
'message_flags', '__application_id', '__token', 'user', 'guild', 'channel', 'message', '_message',
94-
'deferred', 'deferred_hidden', 'callback_message', '_component', 'component_type', '__application_id',
95-
'__interaction_id', '_interaction_type')
96-
9794
def __repr__(self):
9895
"""Represents a :class:`discord.Interaction`-object."""
9996
return f'<Interaction {", ".join(["%s=%s" % (a, getattr(self, a)) for a in self.__slots__ if a[0] != "_"])}>'
10097

10198
async def defer(self, response_type: typing.Literal[5, 6] = InteractionCallbackType.deferred_update_msg, hidden: bool = False) -> None:
10299
"""
100+
|coro|
101+
103102
'Defers' the response.
104-
If :attr:`response_type` is `InteractionCallbackType.deferred_msg_with_source` it shows a loading state to the user.
105103
104+
If :attr:`response_type` is `InteractionCallbackType.deferred_msg_with_source` it shows a loading state to the user.
105+
106+
:param response_type: Optional[typing.Literal[5, 6]]
107+
The type to response with, aiter :class:`InteractionCallbackType.deferred_msg_with_source` or :class:`InteractionCallbackType.deferred_update_msg` (e.g. 5 or 6)
108+
109+
:param hidden: Optional[bool]
110+
Whether to defer ephemerally(only the :attr:`author` of the interaction can see the message)
111+
112+
.. note::
113+
Only for :class:`InteractionCallbackType.deferred_msg_with_source`.
114+
115+
.. important::
116+
If you doesn't respond with an message using :meth:`respond`
117+
or edit the original message using :meth:`edit` within less than 3 seconds,
118+
discord will indicates that the interaction failed and the interaction-token will be invalidated.
119+
To provide this us this method
120+
121+
.. note::
122+
A Token will be Valid for 15 Minutes so you could edit the original :attr:`message` with :meth:`edit`, :meth:`respond` or doing anything other with this interaction for 15 minutes.
123+
after that time you have to edit the original message with the Methode :meth:`edit` of the :attr:`message` and sending new messages with the :meth:`send` Methode of :attr:`channel`
124+
(you could not do this hidden as it isn't an respond anymore).
106125
"""
107126

108127
if isinstance(response_type, int):
@@ -129,7 +148,7 @@ async def edit(self, **fields) -> Message:
129148
'Defers' if it isn't yet and edit the message
130149
"""
131150
if not self.channel:
132-
setattr(self, 'channel', self._state.add_dm_channel(data=await self._http.get_channel(self.channel_id)))
151+
self._channel = self._state.add_dm_channel(data=await self._http.get_channel(self.channel_id))
133152
await self.message.edit(__is_interaction_response=True, __deferred=False if (not self.deferred or self.callback_message) else True, __use_webhook=False,
134153
__interaction_id=self.__interaction_id, __interaction_token=self.__token,
135154
__application_id=self.__application_id, **fields)
@@ -146,7 +165,7 @@ async def respond(self, content=None, *, tts=False, embed=None, embeds=None, com
146165
interaction by setting the `hidden` parameter to :bool:`True`.
147166
"""
148167
if not self.channel:
149-
setattr(self, 'channel', self._state.add_dm_channel(data=await self._http.get_channel(self.channel_id)))
168+
self._channel = self._state.add_dm_channel(data=await self._http.get_channel(self.channel_id))
150169
msg = await self.channel.send(content, tts=tts, embed=embed, embeds=embeds, components=components, file=file,
151170
files=files, delete_after=delete_after, nonce=nonce,
152171
allowed_mentions=allowed_mentions, reference=reference,
@@ -171,14 +190,30 @@ async def get_original_callback(self):
171190
This is a API-Call and should use carefully"""
172191
return await self._state.http.get_original_interaction_response(self.__token, self.__application_id)
173192

193+
@property
194+
def created_at(self):
195+
"""
196+
Returns the Interaction’s creation time in UTC.
197+
198+
:return: datetime.datetime
199+
"""
200+
return utils.snowflake_time(self.__interaction_id)
201+
174202
@property
175203
def author(self) -> typing.Union[Member, User]:
176204
return self.member if self.member is not None else self.user
177-
205+
206+
@property
207+
def channel(self):
208+
return self._channel if self._channel else self.message.channel
209+
210+
@property
211+
def guild(self):
212+
return self._guild
213+
178214
@property
179215
def message_is_dm(self) -> bool:
180-
if self.message:
181-
return isinstance(self.channel, DMChannel)
216+
return not self.guild_id
182217

183218
@property
184219
def message_is_hidden(self) -> bool:

discord/state.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,23 +540,22 @@ def parse_message_update(self, data):
540540
self.dispatch('raw_message_edit', raw)
541541

542542
def parse_interaction_create(self, data):
543+
self.dispatch('interaction_create', data)
543544
if data.get('type', data.get('t', 0)) < 3:
544545
return
545546
interaction = Interaction(state=self, data=data)
546547
interaction.message = self._get_message(interaction.message_id) if interaction.message is None else interaction.message
547548
interaction.user = self.store_user(interaction._user)
548549
if interaction.guild_id:
549-
interaction.guild = self._get_guild(interaction.guild_id)
550-
interaction.channel = interaction.guild.get_channel(interaction.channel_id)
550+
interaction._guild = self._get_guild(interaction.guild_id)
551+
interaction._channel = interaction.guild.get_channel(interaction.channel_id)
551552
interaction.member = interaction.guild.get_member(interaction.user_id)
552553
if interaction.member is None:
553-
# This can only be the case if member-intents are not activated.
554+
# This can only be the case if member-intents are not activated. or the member is not in the guild-cache right now
554555
interaction.member = Member(guild=interaction.guild, data=interaction._member, state=self)
555556
else:
556-
interaction.channel = self._get_private_channel(interaction.channel_id)
557+
interaction._channel = self._get_private_channel(interaction.channel_id)
557558
if interaction.message is not None:
558-
self.dispatch('interaction_create', interaction)
559-
self.dispatch('raw_interaction_create', interaction)
560559
if interaction._interaction_type == InteractionType.Component:
561560
if interaction.component_type == 2:
562561
self.dispatch('button_click', interaction, interaction.component)
@@ -566,7 +565,6 @@ def parse_interaction_create(self, data):
566565
self.dispatch('raw_selection_select', interaction, interaction.component)
567566
else:
568567
interaction.message = Message(state=self, channel=interaction.channel, data=interaction._message)
569-
self.dispatch('raw_interaction_create', interaction)
570568
if interaction._interaction_type == InteractionType.Component:
571569
if interaction.component_type == 2:
572570
self.dispatch('raw_button_click', interaction, interaction.component)

0 commit comments

Comments
 (0)