API Reference¶
Decorators¶
-
@
discord.ext.slash.
permit
(target: Union[discord.role.Role, discord.abc.User, discord.object.Object], perm: bool, guild_id: Optional[int] = Ellipsis, type: Optional[discord.ext.slash.simples.ApplicationCommandPermissionType] = None)¶ Decorator on top of a command that adds a permissions overwrite.
Classes¶
The Bot¶
-
class
discord.ext.slash.
SlashBot
(*args, **kwargs)¶ A bot that supports slash commands.
Constructor arguments in addition to those provided to
discord.ext.commands.Bot
are as follows:- Parameters
debug_guild (int) – While testing your bot, it may be useful to have instant command updates for global commands. Setting this to a guild ID will redirect all global commands to commands specific to that guild. Once in production, set this to
None
or do not set it at all.resolve_not_fetch (bool) – If
True
(the default), Discord objects passed in arguments will be resolved from the slash commands API, not retrieved or fetched.fetch_if_not_get (bool) – If
False
(the default), Discord objects passed in arguments will not be fetched from the API if retrieving them from cache fails.
-
app_info
: discord.AppInfo¶ Cached output of
application_info()
. Might not be present untilon_ready()
has fired at least once.
-
@
slash_cmd
(**kwargs)¶ Create a
Command
with the decorated coroutine and**kwargs
and add it toslash
.
-
@
slash_group
(**kwargs)¶ Create a
Group
with the decorated coroutine and**kwargs
and add it toslash
.
-
add_slash
(func, **kwargs)¶ Non-decorator version of
slash_cmd()
.If
func
is aCommand
it will be directly added.
-
add_slash_group
(func, **kwargs)¶ Non-decorator version of
slash_group()
.
-
add_slash_cog
(cog: type)¶ Add all attributes of
cog
that areCommand
orGroup
instances.- Parameters
cog (type) – The cog to read attributes from.
-
async
application_info
()¶ Equivalent to
discord.Client.application_info()
, but caches its output inapp_info
.
Interaction Context¶
-
class
discord.ext.slash.
Context
(*args, **kwargs)¶ Object representing an interaction.
-
guild
: Union[discord.Guild, discord.Object]¶ The guild where the interaction took place. Can be an
Object
with just the ID if the client is not in the guild.
-
channel
: Union[discord.TextChannel, discord.Object]¶ The channel where the command was run. Can be an
Object
with just the ID if the client is not in the guild.
The user who ran the command. If
guild
is anObject
, a lot ofMember
methods that require the guild will break and should not be relied on. Ifguild
isNone
then the command was run in DMs and this object will be aUser
instead.
-
options
: Mapping[str, Any]¶ The options passed to the command (including this context). More useful in groups and checks.
-
me
: Optional[discord.Member]¶ The bot, as a
Member
in that context. Can beNone
if the client is not in the guild.
-
webhook
: Optional[discord.Webhook]¶ Webhook used for sending followup messages.
None
until interaction response has been sent
-
async
respond
(content='', *, embed: Optional[discord.embeds.Embed] = None, embeds: Optional[Iterable[discord.embeds.Embed]] = None, allowed_mentions: Optional[discord.mentions.AllowedMentions] = None, file: Optional[discord.file.File] = None, ephemeral: bool = False, deferred: bool = False, flags: Optional[Union[discord.ext.slash.simples.CallbackFlags, int]] = None, rtype: discord.ext.slash.simples.InteractionCallbackType = <InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE: 4>)¶ Respond to the interaction. If called again, edits the response.
- Parameters
content (str) – The content of the message.
embed (discord.Embed) – Shorthand for
respond(embeds=[embed])
embeds (Iterable[discord.Embed]) – Up to 10 embeds (any more will be silently discarded)
allowed_mentions (discord.AllowedMentions) – Mirrors normal
allowed_mentions
insend()
file (discord.File) – Mirrors normal
file
insend()
ephemeral (bool) – Shortcut to setting
flags |=
CallbackFlags.EPHEMERAL
. If other flags are present, they are preserved.deferred (bool) – Shortcut to setting
rtype =
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
. Overridesrtype
unconditionally ifTrue
.flags (Union[CallbackFlags, int]) – Message flags, ORed together
rtype (InteractionCallbackType) – The type of response to send. See that class’s documentation.
- Raises
TypeError – if both
embed
andembeds
are specified.ValueError – if sending channel message without content.
-
async
delete
()¶ Delete the original interaction response message.
-
async
send
(*args, **kwargs)¶ Send a message in the channel where the the command was run. Equivalent to
send()
forContext.channel
.Only method that works after the interaction token has expired. Only works if client is present there as a bot user too.
-
-
discord.ext.slash.
Interaction
¶ alias of
discord.ext.slash.context.Context
Slash Commands¶
-
class
discord.ext.slash.
Command
(coro: Coroutine, **kwargs)¶ Represents a slash command.
The following constructor argument does not map to an attribute:
- Parameters
check (Coroutine) – A coroutine to run before calling the command. If it returns
False
(not falsy,False
), then the command is not run.
The following attributes are set by constructor arguments:
-
coro
: Coroutine¶ (Required) Original callback for the command.
-
id
: Optional[int]¶ ID of registered command. Can be None when not yet registered, or if not a top-level command.
-
default_permission
: bool = True¶ If
False
, this command is disabled by default when the bot is added to a new guild. It must be re-enabled per user or role using permissions.
- Raises
TypeError – if
coro
has a required argument (other thanself
) without an annotation.ValueError – if no
description
is specified andcoro
has no docstring.ValueError – if no arguments to
coro
are annotated withContext
or a subclass.
The following attributes are not set by constructor arguments:
-
options
: Mapping[str, Option]¶ Options for this command. Set by inspecting the function annotations.
-
permissions
: CommandPermissionsDict¶ Permission overrides for this command. A dict of guild IDs to dicts of: role or user or member objects (partial or real) to boolean enable/disable values to grant/deny permissions.
-
default
: bool = False¶ If
True
, invoking the base parent of this command translates into invoking this subcommand. (Not settable in arguments.)
-
@
check
¶ Set this command’s check to this coroutine.
-
property
qualname
¶ Fully qualified name of command, including group names.
-
add_perm
(target: Union[discord.role.Role, discord.abc.User, discord.object.Object], perm: bool, guild_id: Optional[int] = Ellipsis, type: Optional[discord.ext.slash.simples.ApplicationCommandPermissionType] = None)¶ Add a permission override.
- Parameters
target (Union[discord.Role, PartialRole, discord.Member, discord.User, PartialMember, discord.Object]) – The role or user to assign this permission to.
perm (bool) –
True
to grant permission,False
to deny itguild_id (Optional[int]) – The guild ID to set the permission for, or
None
to apply this to the defaults that all guilds inherit for this command. If specified, overridestarget.guild.id
. Must be specified iftarget
is aObject
or a guildlessUser
.type (ApplicationCommandPermissionType) – The type of permission grant this is,
ROLE
orUSER
. Must be specified iftarget
is aObject
.
Generally there are four ways of calling this:
add_perm(target, perm)
will inferguild_id
andtype
fromtarget.guild.id
and the type oftarget
(respectively).add_perm(target, perm, guild_id)
will infer the type, but manually set the guild ID (e.g. with aUser
and not aMember
).add_perm(discord.Object(id), perm, guild_id, type)
will manually set the guild ID and type since all you have is an ID.add_perm(..., guild_id=None)
will do any of the above but apply the permissions to the defaults that all specific-guild permissions will inherit from, instead of applying to any particular guild.
- Raises
ValueError – if
type
is unspecified but cannot be inferred.ValueError – if
guild_id
is unspecified but cannot be inferred.
-
class
discord.ext.slash.
Group
(coro: Coroutine, **kwargs)¶ Represents a group of slash commands. Attributes and constructor arguments are the same as
Command
unless documented below.- Parameters
coro (Coroutine) – (Required) Callback invoked when a subcommand of this group is called. (This is not a check! Register a check using
check()
.)
-
@
slash_cmd
(**kwargs)¶ See
SlashBot.slash_cmd()
.
-
@
slash_group
(**kwargs)¶
-
add_slash
(func, **kwargs)¶ See
SlashBot.add_slash()
.
-
add_slash_group
(func, **kwargs)¶
Data Classes¶
-
class
discord.ext.slash.
Option
(description: Union[str, Type[discord.ext.slash.simples.ChoiceEnum]], type: discord.ext.slash.simples.ApplicationCommandOptionType = <ApplicationCommandOptionType.STRING: 3>, **kwargs)¶ An argument to a
Command
. This must be passed as an annotation to the corresponding argument.Constructor arguments map directly to attributes, besides the ones below which have different type signatures:
- Parameters
description (Union[str, Type[ChoiceEnum]]) – Annotating a parameter with
EnumClass
has the same effect as withOption(description=EnumClass)
.choices (Optional[Iterable[Union[str, Mapping[str, str], Choice]]]) – Strings are converted into
Choice
objects with the samename
andvalue
.dict
objects are passed as kwargs to theChoice
constructor.channel_types (Optional[Iterable[Union[int, discord.ChannelType]]]) – Pass either the raw integers or the enum values.
channel_type (Optional[Union[int, discord.ChannelType]]) – A shortcut to
channel_types=[channel_type]
.
-
type
: ApplicationCommandOptionType = :attr:`ApplicationCommandOptionType.STRING`¶ The argument type.
-
channel_types
: Optional[set[discord.ChannelType]]¶ Sets
type
toApplicationCommandOptionType.CHANNEL
, additionally restricted to a set of specific channel types.
Miscellaneous¶
-
class
discord.ext.slash.
SlashWarning
¶ discord.ext.slash
-specific warning type.
Partial Objects¶
Objects resolved from the slash commands API often do not contain all the information that discord.py prefers (most notably guild information).
Enums¶
-
class
discord.ext.slash.
ApplicationCommandOptionType
(value)¶ Possible
Command
Option
types. Default isSTRING
.The type signatures of the below attributes mark the type that the argument value is passed as. For example, options of type
STRING
are passed asstr
.-
USER
: Union[discord.Member, discord.User, PartialMember, discord.Object]¶
-
CHANNEL
: Union[discord.TextChannel, discord.CategoryChannel, discord.VoiceChannel, PartialTextChannel, PartialCategoryChannel, PartialVoiceChannel, discord.Object]¶
-
ROLE
: Union[discord.Role, PartialRole, discord.Object]¶
-
MENTIONABLE
: Union[discord.Member, discord.User, PartialMember, discord.Role, PartialRole, discord.Object]¶
-
-
class
discord.ext.slash.
ApplicationCommandPermissionType
(value)¶ Possible types of permission grants. For use in
Command.add_perm()
andpermit()
.-
ROLE
¶ Specifies that this permission grant is to a role.
-
USER
¶ Specifies that this permission grant is to a user.
-
-
class
discord.ext.slash.
InteractionCallbackType
(value)¶ Possible ways to respond to an interaction. For use in
Context.respond()
.-
PONG
¶ Only used to ACK a
InteractionType.PING
, never valid here. Included only for completeness.
-
CHANNEL_MESSAGE_WITH_SOURCE
¶ Show user input and send a message. Default for slash commands.
-
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
¶ Show user input and display a “waiting for bot” system message. Send a response with this type and edit the response later if you need to do some asynchronous fetch or something.
-
DEFERRED_UPDATE_MESSAGE
¶ ACK a component interaction and edit the original message later. The user does not see a loading state.
-
UPDATE_MESSAGE
¶ Edit the original message a component is attached.
-
APPLICATION_COMMAND_AUTOCOMPLETE_RESULT
¶ Respond with autocomplete suggestions.
-
-
class
discord.ext.slash.
CallbackFlags
(value)¶ Flags to pass to the
flags
argument ofContext.respond()
.-
EPHEMERAL
¶ Only the user receiving the message can see it
-
-
class
discord.ext.slash.
ChoiceEnum
(value)¶ Callback parameters annotated with subclasses of this class will use the enums as choices. See the
/numbers
command in the demo bot for an example.
Events¶
-
discord.ext.slash.
on_interaction_create
(event: dict)¶ Triggered by Discord interactions. For internal use.
-
discord.ext.slash.
on_slash_permissions
()¶ Triggered immediately after
SlashBot.register_commands()
to give an opportunity to register dynamic permissions in code before pushing to the API. If overriding using @:meth:discord.Client.event, you must await-SlashBot.register_permissions()
at the end of the event handler. See/stop
indemo_bot.py
for an example.
-
discord.ext.slash.
on_before_slash_command_invoke
(ctx: Context)¶ Triggered immediately before a slash command is invoked, for logging etc.
-
discord.ext.slash.
on_after_slash_command_invoke
(ctx: Context)¶ Triggered immediately after a successful slash command invocation. Failed invocations will trigger
discord.on_command_error()
instead.