diff options
| author | Rapptz <[email protected]> | 2021-03-28 11:07:51 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2021-04-06 01:11:36 -0400 |
| commit | 8cece19b2220bd6b7f61438219a0b88d0768a079 (patch) | |
| tree | d82c331964790add2a4f11f7fc0342074a2f4521 /discord/interactions.py | |
| parent | Add support for setting application_id (diff) | |
| download | discord.py-8cece19b2220bd6b7f61438219a0b88d0768a079.tar.xz discord.py-8cece19b2220bd6b7f61438219a0b88d0768a079.zip | |
Add on_interaction event and Interaction class.
This is the first pass at the functionality. It's currently a bit
incomplete.
Diffstat (limited to 'discord/interactions.py')
| -rw-r--r-- | discord/interactions.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/discord/interactions.py b/discord/interactions.py new file mode 100644 index 00000000..a235a0a6 --- /dev/null +++ b/discord/interactions.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- + +""" +The MIT License (MIT) + +Copyright (c) 2015-present Rapptz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +""" + +from __future__ import annotations + +from . import utils +from .enums import try_enum, InteractionType + +__all__ = ( + 'Interaction', +) + +class Interaction: + """Represents a Discord interaction. + + An interaction happens when a user does an action that needs to + be notified. Current examples are slash commands but future examples + include forms and buttons. + + .. versionadded:: 2.0 + + Attributes + ----------- + id: :class:`int` + The interaction's ID. + type: :class:`InteractionType` + The interaction type. + guild_id: Optional[:class:`int`] + The guild ID the interaction was sent from. + channel_id: Optional[:class:`int`] + The channel ID the interaction was sent from. + application_id: :class:`int` + The application ID that the interaction was for. + user: Optional[Union[:class:`User`, :class:`Member`]] + The user or member that sent the interaction. + token: :class:`str` + The token to continue the interaction. These are valid + for 15 minutes. + """ + __slots__ = ( + 'id', + 'type', + 'guild_id', + 'channel_id', + 'data', + 'application_id', + 'user', + 'token', + 'version', + '_state', + ) + + def __init__(self, *, data, state=None): + self._state = state + self._from_data(data) + + def _from_data(self, data): + self.id = int(data['id']) + self.type = try_enum(InteractionType, data['type']) + self.data = data.get('data') + self.token = data['token'] + self.version = data['version'] + self.channel_id = utils._get_as_snowflake(data, 'channel_id') + self.guild_id = utils._get_as_snowflake(data, 'guild_id') + self.application_id = utils._get_as_snowflake(data, 'application_id') + + @property + def guild(self): + """Optional[:class:`Guild`]: The guild the interaction was sent from.""" + return self._state and self._state.get_guild(self.guild_id) + + @property + def channel(self): + """Optional[:class:`abc.GuildChannel`]: The channel the interaction was sent from. + + Note that due to a Discord limitation, DM channels are not resolved since there is + no data to complete them. + """ + guild = self.guild + return guild and guild.get_channel(self.channel_id) + |