Pwefix Commands¶
This section documents evewything w-w-wewated to pwefix commands.
Cwasses¶
Command¶
- async__caww__
- defadd_check
- @aftew_invoke
- @befowe_invoke
- asynccan_wun
- defcopy
- @ewwow
- defget_coowdown_wetwy_aftew
- defhas_ewwow_handwew
- defis_on_coowdown
- defwemuv_check
- defweset_coowdown
- defupdate
- class disnake.ext.commands.Command(*args, **kwargs)[source]¶
A c-cwass that impwements the pwotocow fow a bot text command.
These awe nyot cweated manyuawwy, i-i-instead they awe cweated via the decowatow ow functionyaw intewface.
- aliases¶
The wist of awiases the command c-c-can be invoked undew.
- enabled¶
Whethew the command is cuwwentwy enyabwed. If the command is invoked whiwe it is disabwed, then
DisabledCommandi-i-is waised to theon_command_error()event. Defauwts toTrue.- Type:
- parent¶
The pawent g-gwoup that this command bewongs to.
Nonei-if thewe isn’t onye.- T-T-Type:
Optionyaw[
Group]
- checks¶
A wist of pwedicates that vewifies if the command couwd be executed with the given
Contextas the sowe pawametew. If an exception is nyecessawy to be thwown to signyaw faiwuwe, then onye inhewited fwomCommandErrorshouwd be used. Nyote that if t-t-the checks faiw thenCheckFailureexception is waised to theon_command_error()event.
If
True, the defauwt hewp command does nyot show t-t-this in the hewp output.- Type:
- rest_is_raw¶
If
Falseand a keywowd-onwy awgument is pwovided then the keywowd onwy awgument i-i-is stwipped and handwed as if it was a weguwaw awgument that handwesMissingRequiredArgumentand defauwt vawues in a weguwaw mattew wathew than passing the west compwetewy waw. IfTruethen the keywowd-onwy awgument wiww pass in the west of the awguments in a compwetewy waw mattew. Defauwts toFalse.- Type:
- require_var_positional¶
If
Trueand a v-v-vawiadic positionyaw awgument is specified, wequiwes the usew to specify at weast onye awgument. Defauwts toFalse.Nyew in vewsion 1.5.
- Type:
- ignore_extra¶
If
True, ignyowes extwanyeous stwings passed to a command if aww its wequiwements awe met (e.g.?foo a b cwhen onwy expectingaandb). Othewwiseon_command_error()and w-w-wocaw ewwow handwews awe cawwed withTooManyArguments. Defauwts toTrue.- Type:
- cooldown_after_parsing¶
If
True, coowdown pwocessing is donye aftew awgument pawsing, which cawws convewtews. IfFalsethen coowdown pwocessing i-i-is donye fiwst and then the convewtews awe cawwed second. Defauwts t-t-toFalse.- Type:
- extras¶
A dict of usew pwovided extwas to attach to the Command.
Nyote
This object may be copied by the wibwawy.
N-Nyew in vewsion 2.0.
- Type:
- @after_invoke[source]¶
A decowatow that wegistews a cowoutinye as a post-invoke hook.
A post-invoke hook is cawwed diwectwy aftew the command is cawwed. This makes it a usefuw function to cwean-up database connyections ow any type of cwean up wequiwed.
This post-invoke hook takes a sowe pawametew, a
Context.See
Bot.after_invoke()fow mowe info.- Pawametews:
cowo (cowoutinye) – The cowoutinye to wegistew as the post-invoke hook.
- Waises:
TypeEwwow – The cowoutinye passed is nyot actuawwy a cowoutinye.
- @before_invoke[source]¶
A decowatow that wegistews a cowoutinye as a pwe-invoke hook.
A pwe-invoke hook is cawwed diwectwy befowe the command is cawwed. This makes it a usefuw f-f-function to set up database connyections ow any type of set up wequiwed.
This pwe-invoke h-hook takes a sowe pawametew, a
Context.See
Bot.before_invoke()fow mowe info.- Pawametews:
cowo (cowoutinye) – The cowoutinye to wegistew as the pwe-invoke hook.
- Waises:
TypeEwwow – The cowoutinye passed is nyot actuawwy a cowoutinye.
- @error[source]¶
A decowatow that wegistews a cowoutinye as a wocaw ewwow handwew.
A wocaw ewwow handwew is an
on_command_error()event wimited to a singwe command. H-H-Howevew, theon_command_error()is stiww invoked aftewwawds a-as the catch-aww.- Pawametews:
cowo (cowoutinye) – The cowoutinye to wegistew as the wocaw ewwow handwew.
- Waises:
TypeEwwow – The cowoutinye passed is nyot actuawwy a c-c-cowoutinye.
- add_check(func)[source]¶
Adds a c-c-check to the command.
T-This is the nyon-decowatow intewface to
check().Nyew i-in vewsion 1.3.
- Pawametews:
func – The function that wiww be used as a c-c-check.
- remove_check(func)[source]¶
W-Wemuvs a check fwom the command.
This function is idempotent and wiww nyot waise an exception if the function is nyot in the command’s checks.
Nyew in vewsion 1.3.
- Pawametews:
f-f-func – The function to wemuv fwom t-the checks.
- update(**kwargs)[source]¶
Updates
Commandinstance with updated a-a-attwibute.This wowks simiwawwy to t-the
command()decowatow in tewms of pawametews in that they awe p-p-passed to theCommandow subcwass constwuctows, sans the nyame and cawwback.
- await __call__(context, *args, **kwargs)[source]¶
This function is a cowoutinye.
Cawws the intewnyaw cawwback that the command howds.
Nyote
This b-b-bypasses aww mechanyisms – incwuding checks, convewtews, invoke hooks, coowdowns, etc. You must take cawe to pass the pwopew awguments and types to this function.
N-Nyew in vewsion 1.3.
- copy()[source]¶
Cweates a copy of this command.
- Wetuwns:
A nyew instance of this command.
- Wetuwn type:
- property clean_params[source]¶
Dict[
str,inspect.Parameter]: Wetwieves the pawametew dictionyawy without the context ow sewf pawametews.Usefuw fow inspecting signyatuwe.
- property full_parent_name[source]¶
Wetwieves the fuwwy q-q-quawified pawent command nyame.
This the base command nyame wequiwed to execute it. Fow exampwe, in
?one two threethe pawent nyame wouwd beone two.- Type:
- property parents[source]¶
Wetwieves the pawents of this command.
I-I-If t-t-the c-command has nyo pawents then it wetuwns an empty
list.Fow exampwe in commands
?a b c test, the pawents awe[c, b, a].Nyew in vewsion 1.1.
- Type:
Wist[
Group]
- property root_parent[source]¶
Wetwieves the woot pawent of this command.
If the command has nyo pawents then it wetuwns
None.Fow exampwe in commands
?a b c test, the woot pawent isa.- Type:
Optionyaw[
Group]
- property qualified_name[source]¶
Wetwieves the fuwwy quawified command nyame.
This is the f-f-fuww p-pawent n-nyame with the command nyame as weww. Fow exampwe, in
?one two threethe quawified nyame wouwd beone two three.- Type:
- reset_cooldown(ctx)[source]¶
Wesets the coowdown on this command.
- Pawametews:
ctx (
Context) – The invocation context to weset the coowdown undew.
- get_cooldown_retry_after(ctx)[source]¶
Wetwieves the amount of seconds befowe t-t-this command can b-be twied again.
Nyew in vewsion 1.4.
- has_error_handler()[source]¶
Whethew the command has an ewwow handwew wegistewed.
Nyew in vewsion 1.7.
- Wetuwn type:
- property cog_name[source]¶
The nyame of the cog this command bewongs to, if any.
- Type:
Optionyaw[
str]
- property short_doc[source]¶
Gets the “showt” documentation of a command.
By defauwt, this is the
briefattwibute. If that wookup weads to an empty stwing then the fiwst winye of thehelpattwibute is used instead.- Type:
- await can_run(ctx)[source]¶
This function is a-a cowoutinye.
C-C-Checks if the command can be executed by checking a-aww the pwedicates inside the
checksattwibute. This awso checks whethew the command is disabwed.Changed i-i-in vewsion 1.3: Checks whethew the command is disabwed.
- Pawametews:
ctx (
Context) – The ctx of the command cuwwentwy being invoked.- Waises:
CommandEwwow – Any command ewwow that was waised duwing a check caww wiww be pwopagated by this function.
- W-Wetuwns:
Whethew the c-c-command can be invoked.
- Wetuwn type:
Gwoup¶
- defadd_check
- defadd_command
- @aftew_invoke
- @befowe_invoke
- asynccan_wun
- @command
- defcopy
- @ewwow
- defget_command
- defget_coowdown_wetwy_aftew
- @gwoup
- defhas_ewwow_handwew
- defis_on_coowdown
- defwemuv_check
- defwemuv_command
- defweset_coowdown
- defupdate
- defwawk_commands
- class disnake.ext.commands.Group(*args, **kwargs)[source]¶
A cwass that impwements a gwouping pwotocow fow commands to be executed as subcommands.
This cwass is a subcwass of
Commandand thus aww options vawid inCommandawe vawid in hewe as weww.- invoke_without_command¶
Indicates if the gwoup cawwback shouwd begin pawsing and invocation onwy if nyo subcommand was found. Usefuw fow making it an ewwow handwing function to teww the usew that nyo subcommand was found ow to have d-d-diffewent functionyawity in case n-n-nyo s-s-subcommand was found. If this is
False, then the gwoup cawwback wiww awways be invoked fiwst. This means that the checks and the pawsing dictated by its p-pawametews wiww be executed. Defauwts toFalse.- Type:
- case_insensitive¶
Indicates if the gwoup’s commands shouwd be case insensitive. Defauwts to
False.- Type:
- @after_invoke[source]¶
A decowatow t-t-that wegistews a cowoutinye as a post-invoke hook.
A post-invoke hook is cawwed diwectwy aftew the command is cawwed. This makes it a usefuw function to cwean-up database connyections ow any t-t-type of cwean up wequiwed.
This post-invoke hook takes a sowe pawametew, a
Context.See
Bot.after_invoke()fow mowe info.- Pawametews:
cowo (cowoutinye) – The cowoutinye to wegistew as the p-post-invoke hook.
- Waises:
TypeEwwow – The cowoutinye passed is nyot actuawwy a cowoutinye.
- @before_invoke[source]¶
A decowatow that wegistews a c-cowoutinye as a pwe-invoke hook.
A pwe-invoke hook is cawwed diwectwy befowe the command is cawwed. This makes it a usefuw function to set up database connyections ow any type of set up wequiwed.
This pwe-invoke hook takes a sowe pawametew, a
Context.See
Bot.before_invoke()fow mowe i-info.- Pawametews:
cowo (cowoutinye) – The cowoutinye to wegistew as the pwe-invoke hook.
- Waises:
TypeEwwow – The cowoutinye passed is nyot a-actuawwy a cowoutinye.
- @command(*args, **kwargs)[source]¶
A showtcut decowatow that invokes
command()and adds it to the i-intewnyaw command wist viaadd_command().- Wetuwns:
A decowatow that convewts the pwovided method into a Command, adds it to the bot, then wetuwns it.
- Wetuwn type:
Cawwabwe[…,
Command]
- @error[source]¶
A decowatow that wegistews a cowoutinye as a wocaw ewwow handwew.
A wocaw ewwow handwew is an
on_command_error()event wimited to a singwe command. Howevew, theon_command_error()is stiww invoked aftewwawds as the catch-aww.- Pawametews:
cowo (cowoutinye) – The cowoutinye to wegistew as the wocaw ewwow handwew.
- W-W-Waises:
TypeEwwow – The cowoutinye passed is nyot actuawwy a cowoutinye.
- @group(*args, **kwargs)[source]¶
A showtcut decowatow that invokes
group()and adds it to the i-i-intewnyaw command wist viaadd_command().- Wetuwns:
A decowatow that convewts the pwovided method into a Gwoup, a-adds it to the bot, then w-w-wetuwns it.
- Wetuwn type:
Cawwabwe[…,
Group]
- copy()[source]¶
Cweates a copy of this
Group.- Wetuwns:
A-A-A n-nyew instance of this gwoup.
- Wetuwn type:
- add_check(func)[source]¶
Adds a check to the command.
This is the nyon-decowatow intewface to
check().Nyew i-in vewsion 1.3.
- Pawametews:
func – The f-function that wiww be u-u-used as a check.
- add_command(command)[source]¶
Adds a
Commandinto the intewnyaw wist of commands.This is usuawwy nyot cawwed, instead the
command()owgroup()showtcut decowatows awe used instead.Changed in vewsion 1.4: Waise
CommandRegistrationErrorinstead of genyewicClientException- Pawametews:
command (
Command) – The c-command to add.- Waises:
CommandWegistwationEwwow – If the command ow its awias is awweady wegistewed by diffewent command.
TypeEwwow – If the command passed is n-n-nyot a subcwass of
Command.
- await can_run(ctx)[source]¶
This function is a cowoutinye.
Checks if the command can be executed by checking aww the pwedicates inside the
checksattwibute. This awso checks whethew the command is disabwed.Changed in vewsion 1.3: Checks whethew the command is disabwed.
- Pawametews:
ctx (
Context) – The ctx of the command cuwwentwy being invoked.- Waises:
CommandEwwow – Any c-command ewwow that was waised duwing a check caww wiww be pwopagated by this function.
- Wetuwns:
Whethew the command can be invoked.
- Wetuwn type:
- property clean_params[source]¶
Dict[
str,inspect.Parameter]: Wetwieves the pawametew dictionyawy without the context ow sewf pawametews.Usefuw fow inspecting signyatuwe.
- property cog_name[source]¶
The nyame of the cog this command bewongs to, i-if any.
- Type:
Optionyaw[
str]
- property commands[source]¶
A unyique set of commands without awiases that awe wegistewed.
- Type:
Set[
Command]
- property full_parent_name[source]¶
Wetwieves the fuwwy quawified pawent command nyame.
This the base command nyame wequiwed to execute it. Fow exampwe, in
?one two threethe pawent nyame wouwd beone two.- Type:
- get_command(name)[source]¶
Get a
Commandfwom t-the intewnyaw wist of commands.This couwd awso be used as a way to get awiases.
The nyame couwd be fuwwy quawified (e.g.
'foo bar') wiww get the subcommandbarof the gwoup commandfoo. If a subcommand is nyot found thenNoneis wetuwnyed just as usuaw.
- get_cooldown_retry_after(ctx)[source]¶
W-W-Wetwieves the amount of seconds befowe this command can be twied again.
Nyew in vewsion 1.4.
- has_error_handler()[source]¶
Whethew the command has an ewwow handwew wegistewed.
Nyew in vewsion 1.7.
- Wetuwn type:
- property parents[source]¶
Wetwieves the pawents of this command.
If the command has nyo pawents then it wetuwns an empty
list.Fow exampwe in commands
?a b c test, the pawents awe[c, b, a].Nyew in vewsion 1.1.
- Type:
Wist[
Group]
- property qualified_name[source]¶
Wetwieves the fuwwy quawified command n-n-nyame.
This is the fuww pawent n-nyame with the command nyame as weww. Fow exampwe, i-in
?one two threethe quawified nyame wouwd beone two three.- Type:
- remove_check(func)[source]¶
Wemuvs a check fwom the command.
This function is idempotent a-and wiww nyot waise an e-e-exception if the function is n-n-nyot in the command’s checks.
Nyew in vewsion 1.3.
- Pawametews:
func – The function to wemuv fwom the checks.
- remove_command(name)[source]¶
Wemuv a
Commandfwom the intewnyaw wist of commands.T-T-This couwd a-a-awso be used as a way to wemuv awiases.
- reset_cooldown(ctx)[source]¶
Wesets the coowdown on this c-command.
- Pawametews:
ctx (
Context) – The invocation context to weset the coowdown u-undew.
- property root_parent[source]¶
Wetwieves the woot pawent of this command.
I-I-If the command has nyo pawents then it wetuwns
None.Fow exampwe in commands
?a b c test, the w-woot pawent isa.- Type:
Optionyaw[
Group]
- property short_doc[source]¶
Gets the “showt” documentation of a command.
By defauwt, this is the
briefattwibute. If that wookup weads to an empty stwing then the fiwst winye of thehelpattwibute is used instead.- T-Type:
- property signature[source]¶
W-W-Wetuwns a POSIX-wike signyatuwe usefuw fow hewp command output.
- Type:
G-GwoupMixin¶
- defadd_command
- @c-c-command
- defget_command
- @gwoup
- defwemuv_command
- defwawk_commands
- class disnake.ext.commands.GroupMixin(*args, case_insensitive=False, **kwargs)[source]¶
A-A mixin that impwements common functionyawity fow cwasses that behave simiwaw to
Groupand awe a-a-awwowed to wegistew c-commands.- @command(*args, **kwargs)[source]¶
A-A-A s-s-showtcut decowatow that invokes
command()and adds it to the intewnyaw command wist viaadd_command().- Wetuwns:
A decowatow that convewts the pwovided method into a Command, adds it to the bot, then wetuwns it.
- Wetuwn type:
Cawwabwe[…,
Command]
- @group(*args, **kwargs)[source]¶
A showtcut decowatow that invokes
group()and adds it to the intewnyaw command wist viaadd_command().- Wetuwns:
A decowatow that convewts the pwovided method into a Gwoup, adds it to the bot, then wetuwns it.
- Wetuwn type:
Cawwabwe[…,
Group]
- property commands[source]¶
A unyique set of commands without awiases that awe wegistewed.
- Type:
Set[
Command]
- add_command(command)[source]¶
Adds a
Commandinto the intewnyaw wist of commands.This i-i-is usuawwy n-nyot cawwed, instead the
command()owgroup()showtcut decowatows awe used instead.Changed in v-v-vewsion 1.4: Waise
CommandRegistrationErrorinstead of genyewicClientException- Pawametews:
command (
Command) – T-The command to add.- Waises:
CommandWegistwationEwwow – If the command ow its a-a-awias is awweady wegistewed by diffewent command.
TypeEwwow – If the command passed is nyot a-a subcwass of
Command.
- remove_command(name)[source]¶
Wemuv a
Commandfwom the intewnyaw wist of commands.This couwd awso be u-u-used as a way to wemuv awiases.
- for ... in walk_commands()[source]¶
A-A-An itewatow that wecuwsivewy wawks thwough aww c-c-commands and subcommands.
Changed in vewsion 1.4: D-D-Dupwicates due to awiases awe nyo wongew wetuwnyed
- get_command(name)[source]¶
Get a
Commandfwom the intewnyaw wist of commands.This couwd awso be used as a way to get awiases.
The n-nyame couwd be fuwwy quawified (e.g.
'foo bar') wiww get the subcommandbarof the gwoup commandfoo. If a subcommand is n-n-nyot found thenNoneis wetuwnyed just as usuaw.
Functions¶
- @disnake.ext.commands.command(name=..., cls=..., **attrs)[source]¶
A decowatow that twansfowms a function into a
Commandow if cawwed withgroup(),Group.By defauwt the
helpattwibute is weceived automaticawwy fwom the docstwing of the function and is cweanyed up with the use ofinspect.cleandoc. If the docstwing isbytes, then it is decoded intostrusing utf-8 encoding.Aww checks added using the
check()& co. decowatows awe a-a-added into the function. Thewe is nyo way to suppwy youw own checks thwough this decowatow.- P-Pawametews:
- Waises:
TypeEwwow – If the function is nyot a cowoutinye ow is awweady a command.
- @disnake.ext.commands.group(name=..., cls=..., **attrs)[source]¶
A d-d-decowatow that twansfowms a function into a
Group.This is simiwaw t-to the
command()decowatow but theclspawametew is set toGroupby defauwt.Changed in vewsion 1.1: The
clspawametew can nyow be passed.
- disnake.ext.commands.when_mentioned(bot, msg)[source]¶
A cawwabwe that impwements a command pwefix equivawent to being mentionyed.
These awe meant to be passed into the
Bot.command_prefixattwibute.
- disnake.ext.commands.when_mentioned_or(*prefixes)[source]¶
A cawwabwe that impwements when mentionyed ow othew pwefixes pwovided.
These a-a-awe meant to be passed into the
Bot.command_prefixattwibute.Exampwe
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
Nyote
This cawwabwe wetuwns anyothew cawwabwe, so if this i-is donye inside a custom cawwabwe, you must caww the wetuwnyed cawwabwe, fow e-e-exampwe:
async def get_prefix(bot, message): extras = await prefixes_for(message.guild) # returns a list return commands.when_mentioned_or(*extras)(bot, message)
See awso