discord-interactions

discord_slash.context module

class discord_slash.context.InteractionContext(_http: discord_slash.http.SlashCommandRequest, _json: dict, _discord: Union[discord.client.Client, discord.ext.commands.bot.Bot], logger)

Bases: object

Base context for interactions.

In some ways similar with discord.ext.commands.Context.

Warning

Do not manually init this model.

Variables
  • message – Message that invoked the slash command.

  • interaction_id – Interaction ID of the command message.

  • bot – discord.py client.

  • _httphttp.SlashCommandRequest of the client.

  • _logger – Logger instance.

  • data – The raw data of the interaction.

  • values – The values sent with the interaction. Currently for selects.

  • deferred – Whether the command is current deferred (loading state)

  • _deferred_hidden – Internal var to check that state stays the same

  • responded – Whether you have responded with a message to the interaction.

  • guild_id – Guild ID of the command message. If the command was invoked in DM, then it is None

  • author_id – User ID representing author of the command message.

  • channel_id – Channel ID representing channel of the command message.

  • author – User or Member instance of the command invoke.

property deffered
property guild: Optional[discord.guild.Guild]

Guild instance of the command invoke. If the command was invoked in DM, then it is None

Returns

Optional[discord.Guild]

property channel: Optional[Union[discord.channel.TextChannel, discord.channel.DMChannel]]

Channel instance of the command invoke.

Returns

Optional[Union[discord.abc.GuildChannel, discord.abc.PrivateChannel]]

property voice_client: Optional[discord.voice_client.VoiceProtocol]

VoiceClient instance of the command invoke. If the command was invoked in DM, then it is None. If the bot is not connected to any Voice/Stage channels, then it is None.

Returns

Optional[discord.VoiceProtocol]

property me: Union[discord.member.Member, discord.user.ClientUser]

Bot member instance of the command invoke. If the command was invoked in DM, then it is discord.ClientUser.

Returns

Union[discord.Member, discord.ClientUser]

async defer(hidden: bool = False)

‘Defers’ the response, showing a loading state to the user

Parameters

hidden – Whether the deferred response should be ephemeral . Default False.

async send(content: str = '', *, embed: Optional[discord.embeds.Embed] = None, embeds: Optional[List[discord.embeds.Embed]] = None, tts: bool = False, file: Optional[discord.file.File] = None, files: Optional[List[discord.file.File]] = None, allowed_mentions: Optional[discord.mentions.AllowedMentions] = None, hidden: bool = False, delete_after: Optional[float] = None, components: Optional[List[dict]] = None) discord_slash.model.SlashMessage

Sends response of the interaction.

Warning

  • Since Release 1.0.9, this is completely changed. If you are migrating from older version, please make sure to fix the usage.

  • You can’t use both embed and embeds at the same time, also applies to file and files.

  • If you send files in the initial response, this will defer if it’s not been deferred, and then PATCH with the message

Parameters
  • content (str) – Content of the response.

  • embed (discord.Embed) – Embed of the response.

  • embeds (List[discord.Embed]) – Embeds of the response. Maximum 10.

  • tts (bool) – Whether to speak message using tts. Default False.

  • file (discord.File) – File to send.

  • files (List[discord.File]) – Files to send.

  • allowed_mentions (discord.AllowedMentions) – AllowedMentions of the message.

  • hidden (bool) – Whether the message is hidden, which means message content will only be seen to the author.

  • delete_after (float) – If provided, the number of seconds to wait in the background before deleting the message we just sent. If the deletion fails, then it is silently ignored.

  • components (List[dict]) – Message components in the response. The top level must be made of ActionRows.

Returns

Union[discord.Message, dict]

async reply(content: str = '', *, embed: Optional[discord.embeds.Embed] = None, embeds: Optional[List[discord.embeds.Embed]] = None, tts: bool = False, file: Optional[discord.file.File] = None, files: Optional[List[discord.file.File]] = None, allowed_mentions: Optional[discord.mentions.AllowedMentions] = None, hidden: bool = False, delete_after: Optional[float] = None, components: Optional[List[dict]] = None) discord_slash.model.SlashMessage

Sends response of the interaction. This is currently an alias of the .send() method.

Warning

  • Since Release 1.0.9, this is completely changed. If you are migrating from older version, please make sure to fix the usage.

  • You can’t use both embed and embeds at the same time, also applies to file and files.

  • If you send files in the initial response, this will defer if it’s not been deferred, and then PATCH with the message

Parameters
  • content (str) – Content of the response.

  • embed (discord.Embed) – Embed of the response.

  • embeds (List[discord.Embed]) – Embeds of the response. Maximum 10.

  • tts (bool) – Whether to speak message using tts. Default False.

  • file (discord.File) – File to send.

  • files (List[discord.File]) – Files to send.

  • allowed_mentions (discord.AllowedMentions) – AllowedMentions of the message.

  • hidden (bool) – Whether the message is hidden, which means message content will only be seen to the author.

  • delete_after (float) – If provided, the number of seconds to wait in the background before deleting the message we just sent. If the deletion fails, then it is silently ignored.

  • components (List[dict]) – Message components in the response. The top level must be made of ActionRows.

Returns

Union[discord.Message, dict]

class discord_slash.context.SlashContext(_http: discord_slash.http.SlashCommandRequest, _json: dict, _discord: Union[discord.client.Client, discord.ext.commands.bot.Bot], logger)

Bases: discord_slash.context.InteractionContext

Context of a slash command. Has all attributes from InteractionContext, plus the slash-command-specific ones below.

Variables
  • name – Name of the command.

  • args – List of processed arguments invoked with the command.

  • kwargs – Dictionary of processed arguments invoked with the command.

  • subcommand_name – Subcommand of the command.

  • subcommand_group – Subcommand group of the command.

  • command_id – ID of the command.

property slash: client.SlashCommand

Returns the associated SlashCommand object created during Runtime.

Returns

client.SlashCommand

property cog: Optional[discord.ext.commands.cog.Cog]

Returns the cog associated with the command invoked, if any.

Returns

Optional[commands.Cog]

async invoke(*args, **kwargs)

Invokes a command with the arguments given.

Similar to d.py’s ctx.invoke function and documentation.

Note

This does not handle converters, checks, cooldowns, pre-invoke, or after-invoke hooks in any matter. It calls the internal callback directly as-if it was a regular function.

You must take care in passing the proper arguments when using this function.

Warning

The first parameter passed must be the command being invoked. While using ctx.defer, if the command invoked includes usage of that command, do not invoke ctx.defer before calling this function. It can not defer twice.

Parameters
  • args – Args for the command.

  • kwargs – Keyword args for the command.

Raises

TypeError

class discord_slash.context.ComponentContext(_http: discord_slash.http.SlashCommandRequest, _json: dict, _discord: Union[discord.client.Client, discord.ext.commands.bot.Bot], logger)

Bases: discord_slash.context.InteractionContext

Context of a component interaction. Has all attributes from InteractionContext, plus the component-specific ones below.

Variables
  • custom_id – The custom ID of the component (has alias component_id).

  • component_type – The type of the component.

  • component – Component data retrieved from the message. Not available if the origin message was ephemeral.

  • origin_message – The origin message of the component. Not available if the origin message was ephemeral.

  • origin_message_id – The ID of the origin message.

  • selected_options – The options selected (only for selects)

async defer(hidden: bool = False, edit_origin: bool = False, ignore: bool = False)

‘Defers’ the response, showing a loading state to the user

Parameters
  • hidden – Whether the deferred response should be ephemeral. Default False.

  • edit_origin – Whether the type is editing the origin message. If False, the deferred response will be for a follow up message. Defaults False.

  • ignore – Whether to just ignore and not edit or send response. Using this can avoid showing interaction loading state. Default False.

async send(content: str = '', *, embed: Optional[discord.embeds.Embed] = None, embeds: Optional[List[discord.embeds.Embed]] = None, tts: bool = False, file: Optional[discord.file.File] = None, files: Optional[List[discord.file.File]] = None, allowed_mentions: Optional[discord.mentions.AllowedMentions] = None, hidden: bool = False, delete_after: Optional[float] = None, components: Optional[List[dict]] = None) discord_slash.model.SlashMessage

Sends response of the interaction.

Warning

  • Since Release 1.0.9, this is completely changed. If you are migrating from older version, please make sure to fix the usage.

  • You can’t use both embed and embeds at the same time, also applies to file and files.

  • If you send files in the initial response, this will defer if it’s not been deferred, and then PATCH with the message

Parameters
  • content (str) – Content of the response.

  • embed (discord.Embed) – Embed of the response.

  • embeds (List[discord.Embed]) – Embeds of the response. Maximum 10.

  • tts (bool) – Whether to speak message using tts. Default False.

  • file (discord.File) – File to send.

  • files (List[discord.File]) – Files to send.

  • allowed_mentions (discord.AllowedMentions) – AllowedMentions of the message.

  • hidden (bool) – Whether the message is hidden, which means message content will only be seen to the author.

  • delete_after (float) – If provided, the number of seconds to wait in the background before deleting the message we just sent. If the deletion fails, then it is silently ignored.

  • components (List[dict]) – Message components in the response. The top level must be made of ActionRows.

Returns

Union[discord.Message, dict]

async edit_origin(**fields)

Edits the origin message of the component. Refer to discord.Message.edit() and InteractionContext.send() for fields.

class discord_slash.context.MenuContext(_http: discord_slash.http.SlashCommandRequest, _json: dict, _discord: Union[discord.client.Client, discord.ext.commands.bot.Bot], logger)

Bases: discord_slash.context.InteractionContext

Context of a context menu interaction. Has all attributes from InteractionContext, plus the context-specific ones below.

Variables
  • context_type – The type of context menu command.

  • _resolved – The data set for the context menu.

  • target_message – The targeted message of the context menu command if present. Defaults to None.

  • target_id – The target ID of the context menu command.

  • target_author – The author targeted from the context menu command.

property cog: Optional[discord.ext.commands.cog.Cog]

Returns the cog associated with the command invoked, if any.

Returns

Optional[commands.Cog]

async defer(hidden: bool = False, edit_origin: bool = False, ignore: bool = False)

‘Defers’ the response, showing a loading state to the user

Parameters
  • hidden – Whether the deferred response should be ephemeral. Default False.

  • edit_origin – Whether the type is editing the origin message. If False, the deferred response will be for a follow up message. Defaults False.

  • ignore – Whether to just ignore and not edit or send response. Using this can avoid showing interaction loading state. Default False.

async send(content: str = '', *, embed: Optional[discord.embeds.Embed] = None, embeds: Optional[List[discord.embeds.Embed]] = None, tts: bool = False, file: Optional[discord.file.File] = None, files: Optional[List[discord.file.File]] = None, allowed_mentions: Optional[discord.mentions.AllowedMentions] = None, hidden: bool = False, delete_after: Optional[float] = None, components: Optional[List[dict]] = None) discord_slash.model.SlashMessage

Sends response of the interaction.

Warning

  • Since Release 1.0.9, this is completely changed. If you are migrating from older version, please make sure to fix the usage.

  • You can’t use both embed and embeds at the same time, also applies to file and files.

  • If you send files in the initial response, this will defer if it’s not been deferred, and then PATCH with the message

Parameters
  • content (str) – Content of the response.

  • embed (discord.Embed) – Embed of the response.

  • embeds (List[discord.Embed]) – Embeds of the response. Maximum 10.

  • tts (bool) – Whether to speak message using tts. Default False.

  • file (discord.File) – File to send.

  • files (List[discord.File]) – Files to send.

  • allowed_mentions (discord.AllowedMentions) – AllowedMentions of the message.

  • hidden (bool) – Whether the message is hidden, which means message content will only be seen to the author.

  • delete_after (float) – If provided, the number of seconds to wait in the background before deleting the message we just sent. If the deletion fails, then it is silently ignored.

  • components (List[dict]) – Message components in the response. The top level must be made of ActionRows.

Returns

Union[discord.Message, dict]