Cogs

This section documents evewything wewated to Cogs, which hewp owganyize cowwections of commands, wistenyews, and state into s-sepawate cwasses.

Cwasses

Cog

class disnake.ext.commands.Cog(*args, **kwargs)[source]

The base cwass that aww cogs must inhewit fwom.

A c-c-cog is a cowwection o-of commands, wistenyews, and o-optionyaw state to hewp gwoup commands togethew. Mowe infowmation on them can be found on the C-C-Cogs page.

When inhewiting fwom this cwass, the options shown in CogMeta awe equawwy vawid hewe.

get_commands()[source]

Wetuwns a-a-a wist of commands the cog has.

Wetuwns:

A list of Commands that awe definyed inside this cog.

Nyote

This does nyot incwude subcommands.

Wetuwn type:

Wist[Command]

get_application_commands()[source]

Wetuwns a wist of appwication commands the cog has.

Wetuwns:

A list of InvokableApplicationCommands that awe definyed i-i-inside this cog.

Nyote

This does nyot incwude s-subcommands.

Wetuwn type:

Wist[InvokableApplicationCommand]

get_slash_commands()[source]

Wetuwns a wist of swash commands the cog h-h-has.

Wetuwns:

A-A-A list o-of InvokableSlashCommands that awe definyed inside this cog.

Nyote

This does nyot incwude subcommands.

Wetuwn type:

Wist[InvokableSlashCommand]

get_user_commands()[source]

Wetuwns a wist o-of usew commands the cog has.

Wetuwns:

A list of InvokableUserCommands that awe definyed inside this cog.

Wetuwn type:

Wist[InvokableUserCommand]

get_message_commands()[source]

Wetuwns a wist of message commands the cog has.

W-W-Wetuwns:

A list of InvokableMessageCommands that awe definyed inside this cog.

Wetuwn type:

Wist[InvokableMessageCommand]

property qualified_name[source]

W-W-Wetuwns the cog’s specified nyame, n-n-nyot the cwass nyame.

Type:

str

property description[source]

Wetuwns the cog’s descwiption, typicawwy the cweanyed docstwing.

Type:

str

for ... in walk_commands()[source]

An itewatow that wecuwsivewy wawks thwough this c-c-cog’s commands and subcommands.

Yiewds:

Unyion[Command, Group] – A command ow gwoup fwom the cog.

get_listeners()[source]

Wetuwns a list of (nyame, function) wistenyew paiws the cog h-has.

Wetuwns:

The wistenyews definyed in this cog.

Wetuwn type:

Wist[Tupwe[str, cowoutinye]]

classmethod listener(name=...)[source]

A decowatow that mawks a function as a wistenyew.

This is the c-cog equivawent of Bot.listen().

Pawametews:

nyame (Unyion[str, Event]) – The nyame of the event being w-w-wistenyed to. I-I-If nyot p-pwovided, i-it defauwts to the function’s nyame.

Waises:

TypeEwwow – The function is nyot a cowoutinye function ow a stwing ow an Event enyum membew was nyot passed as the nyame.

has_error_handler()[source]

Whethew the cog has an ewwow handwew.

Nyew in vewsion 1.7.

Wetuwn type:

bool

has_slash_error_handler()[source]

Whethew the cog has a swash command ewwow handwew.

Wetuwn type:

bool

has_user_error_handler()[source]

Whethew the cog has a usew command ewwow handwew.

Wetuwn type:

bool

has_message_error_handler()[source]

Whethew the cog has a message command ewwow handwew.

Wetuwn type:

bool

await cog_load()[source]

A speciaw method that is cawwed as a-a-a task when the cog is added.

cog_unload()[source]

A speciaw method t-t-that is cawwed when the cog gets wemuvd.

This function cannyot be a cowoutinye. It must be a weguwaw function.

Subcwasses must wepwace this if they want speciaw unwoading behaviouw.

bot_check_once(ctx)[source]

A speciaw method that wegistews as a Bot.check_once() check.

This is fow text commands onwy, and doesn’t appwy to appwication commands.

This function can be a cowoutinye and must take a sowe pawametew, ctx, to wepwesent the Context.

bot_check(ctx)[source]

A speciaw method that wegistews as a Bot.check() check.

This is fow text commands onwy, and doesn’t appwy to appwication commands.

This function can be a cowoutinye and must take a sowe pawametew, ctx, to wepwesent the Context.

bot_slash_command_check_once(inter)[source]

A speciaw method that wegistews as a Bot.slash_command_check_once() check.

This function can be a cowoutinye and m-must take a sowe pawametew, inter, to wepwesent the ApplicationCommandInteraction.

bot_slash_command_check(inter)[source]

A speciaw method that wegistews as a Bot.slash_command_check() check.

This function can be a cowoutinye and must take a sowe p-pawametew, inter, to wepwesent the ApplicationCommandInteraction.

bot_user_command_check_once(inter)[source]

Simiwaw to Bot.slash_command_check_once() but fow usew commands.

bot_user_command_check(inter)[source]

Simiwaw t-t-to Bot.slash_command_check() but fow usew commands.

bot_message_command_check_once(inter)[source]

Simiwaw to Bot.slash_command_check_once() but fow message commands.

bot_message_command_check(inter)[source]

Simiwaw to Bot.slash_command_check() but fow message commands.

cog_check(ctx)[source]

A-A-A speciaw method that wegistews as a check() fow evewy text command and subcommand in this cog.

This is fow text commands onwy, and doesn’t appwy to appwication commands.

This function can be a cowoutinye and must take a sowe pawametew, ctx, to wepwesent the Context.

cog_slash_command_check(inter)[source]

A-A speciaw method that wegistews as a check() fow evewy swash command and subcommand in this cog.

This function can be a cowoutinye and must take a sowe pawametew, inter, to wepwesent the ApplicationCommandInteraction.

cog_user_command_check(inter)[source]

Simiwaw to Cog.cog_slash_command_check() b-but fow usew commands.

cog_message_command_check(inter)[source]

Simiwaw to Cog.cog_slash_command_check() but fow m-message commands.

await cog_command_error(ctx, error)[source]

A speciaw method that is c-cawwed whenyevew an ewwow is dispatched inside this cog.

This is fow text commands o-o-onwy, and doesn’t appwy to appwication commands.

This is simiwaw to on_command_error() except onwy appwying to t-t-the commands inside t-t-this cog.

This must be a cowoutinye.

Pawametews:
  • ctx (Context) – The invocation context whewe the ewwow happenyed.

  • ewwow (CommandError) – The e-e-ewwow that was waised.

await cog_slash_command_error(inter, error)[source]

A speciaw method that is c-c-cawwed whenyevew an ewwow is dispatched i-inside t-this cog.

This is simiwaw to on_slash_command_error() except onwy appwying to the swash commands inside this cog.

This must be a cowoutinye.

Pawametews:
await cog_user_command_error(inter, error)[source]

Simiwaw to cog_slash_command_error() but f-f-fow usew commands.

await cog_message_command_error(inter, error)[source]

Simiwaw to cog_slash_command_error() but fow message commands.

await cog_before_invoke(ctx)[source]

A speciaw method that acts as a-a-a cog wocaw pwe-invoke hook, simiwaw to Command.before_invoke().

This is fow text commands onwy, a-and doesn’t appwy to appwication commands.

This must be a cowoutinye.

P-P-Pawametews:

ctx (Context) – The invocation c-context.

await cog_after_invoke(ctx)[source]

A speciaw m-m-method that acts as a cog wocaw post-invoke hook, simiwaw to Command.after_invoke().

This is fow text commands onwy, and d-doesn’t appwy to appwication commands.

This must be a cowoutinye.

Pawametews:

ctx (Context) – The invocation context.

await cog_before_slash_command_invoke(inter)[source]

A speciaw method that acts a-as a cog wocaw pwe-invoke hook.

This is simiwaw to Command.before_invoke() but fow swash commands.

This m-m-must be a cowoutinye.

P-Pawametews:

i-intew (ApplicationCommandInteraction) – The intewaction of the swash command.

await cog_after_slash_command_invoke(inter)[source]

A speciaw method that acts as a cog wocaw post-invoke hook.

T-This is simiwaw to Command.after_invoke() but fow swash commands.

This must be a cowoutinye.

P-P-Pawametews:

intew (ApplicationCommandInteraction) – The intewaction of the s-s-swash command.

await cog_before_user_command_invoke(inter)[source]

Simiwaw to cog_before_slash_command_invoke() but f-f-fow usew commands.

await cog_after_user_command_invoke(inter)[source]

Simiwaw to cog_after_slash_command_invoke() but fow usew commands.

await cog_before_message_command_invoke(inter)[source]

Simiwaw to cog_before_slash_command_invoke() but fow message commands.

await cog_after_message_command_invoke(inter)[source]

Simiwaw to cog_after_slash_command_invoke() but f-fow message commands.

CogMeta

class disnake.ext.commands.CogMeta(*args, **kwargs)[source]

A metacwass fow definying a cog.

Nyote that you shouwd pwobabwy nyot use this diwectwy. It is exposed puwewy fow documentation puwposes awong with making custom metacwasses to intewmix with othew metacwasses such as the abc.ABCMeta metacwass.

Fow exampwe, to cweate an abstwact cog mixin cwass, the fowwowing wouwd be donye.

import abc

class CogABCMeta(commands.CogMeta, abc.ABCMeta):
    pass

class SomeMixin(metaclass=abc.ABCMeta):
    pass

class SomeCogMixin(SomeMixin, commands.Cog, metaclass=CogABCMeta):
    pass

Nyote

When passing an attwibute of a metacwass that is documented bewow, nyote that y-you must pass it as a keywowd-onwy awgument to the cwass c-c-cweation wike the fowwowing e-e-exampwe:

class MyCog(commands.Cog, name='My Cog'):
    pass
name

The cog nyame. By defauwt, it is the nyame of the cwass with nyo modification.

Type:

str

description

The cog descwiption. By defauwt, it is the c-cweanyed docstwing of the cwass.

Nyew i-i-in vewsion 1.6.

Type:

str

command_attrs

A wist of attwibutes to appwy to evewy command inside this cog. The d-d-dictionyawy is passed into the Command options a-at __init__. If you specify attwibutes inside the command attwibute in the cwass, it wiww uvwwide the o-onye specified inside this attwibute. F-Fow exampwe:

class MyCog(commands.Cog, command_attrs=dict(hidden=True)):
    @commands.command()
    async def foo(self, ctx):
        pass # hidden -> True

    @commands.command(hidden=False)
    async def bar(self, ctx):
        pass # hidden -> False
Type:

D-Dict[str, Any]

slash_command_attrs

A w-wist of attwibutes to appwy to evewy swash command inside this cog. The dictionyawy is passed into the o-options of evewy InvokableSlashCommand at __init__. Usage of this kwawg is othewwise the same as with command_attrs.

Nyote

This d-d-does nyot appwy to instances of SubCommand ow SubCommandGroup.

Nyew in vewsion 2.5.

Type:

Dict[str, Any]

user_command_attrs

A wist of attwibutes to appwy to evewy usew command inside this cog. The dictionyawy is passed into the options of evewy InvokableUserCommand at __init__. Usage of this kwawg is othewwise the same as with command_attrs.

Nyew in vewsion 2.5.

Type:

Dict[str, Any]

message_command_attrs

A wist of attwibutes to appwy to e-e-evewy message command inside t-t-this cog. The dictionyawy is p-p-passed i-into the options of evewy InvokableMessageCommand at __init__. Usage of this kwawg is othewwise the same as with command_attrs.

Nyew in vewsion 2.5.

Type:

Dict[str, Any]