Cogs

This section documents everything related to Cogs, which help organize collections of commands, listeners, and state into separate classes.

Classes

Cog

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

The base class that all cogs must inherit from.

A cog is a collection of commands, listeners, and optional state to help group commands together. More information on them can be found on the Cogs page.

When inheriting from this class, the options shown in CogMeta are equally valid here.

get_commands()[source]

Returns a list of commands the cog has.

Returns:

A list of Commands that are defined inside this cog.

Note

This does not include subcommands.

Return type:

List[Command]

get_application_commands()[source]

Returns a list of application commands the cog has.

Returns:

A list of InvokableApplicationCommands that are defined inside this cog.

Note

This does not include subcommands.

Return type:

List[InvokableApplicationCommand]

get_slash_commands()[source]

Returns a list of slash commands the cog has.

Returns:

A list of InvokableSlashCommands that are defined inside this cog.

Note

This does not include subcommands.

Return type:

List[InvokableSlashCommand]

get_user_commands()[source]

Returns a list of user commands the cog has.

Returns:

A list of InvokableUserCommands that are defined inside this cog.

Return type:

List[InvokableUserCommand]

get_message_commands()[source]

Returns a list of message commands the cog has.

Returns:

A list of InvokableMessageCommands that are defined inside this cog.

Return type:

List[InvokableMessageCommand]

property qualified_name[source]

Returns the cog’s specified name, not the class name.

Type:

str

property description[source]

Returns the cog’s description, typically the cleaned docstring.

Type:

str

for ... in walk_commands()[source]

An iterator that recursively walks through this cog’s commands and subcommands.

Yields:

Union[Command, Group] – A command or group from the cog.

get_listeners()[source]

Returns a list of (name, function) listener pairs the cog has.

Returns:

The listeners defined in this cog.

Return type:

List[Tuple[str, coroutine]]

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

A decorator that marks a function as a listener.

This is the cog equivalent of Bot.listen().

Parameters:

name (Union[str, Event]) – The name of the event being listened to. If not provided, it defaults to the function’s name.

Raises:

TypeError – The function is not a coroutine function or a string or an Event enum member was not passed as the name.

has_error_handler()[source]

Whether the cog has an error handler.

New in version 1.7.

Return type:

bool

has_slash_error_handler()[source]

Whether the cog has a slash command error handler.

Return type:

bool

has_user_error_handler()[source]

Whether the cog has a user command error handler.

Return type:

bool

has_message_error_handler()[source]

Whether the cog has a message command error handler.

Return type:

bool

await cog_load()[source]

A special method that is called as a task when the cog is added.

cog_unload()[source]

A special method that is called when the cog gets removed.

This function cannot be a coroutine. It must be a regular function.

Subclasses must replace this if they want special unloading behaviour.

bot_check_once(ctx)[source]

A special method that registers as a Bot.check_once() check.

This is for text commands only, and doesn’t apply to application commands.

This function can be a coroutine and must take a sole parameter, ctx, to represent the Context.

bot_check(ctx)[source]

A special method that registers as a Bot.check() check.

This is for text commands only, and doesn’t apply to application commands.

This function can be a coroutine and must take a sole parameter, ctx, to represent the Context.

bot_slash_command_check_once(inter)[source]

A special method that registers as a Bot.slash_command_check_once() check.

This function can be a coroutine and must take a sole parameter, inter, to represent the ApplicationCommandInteraction.

bot_slash_command_check(inter)[source]

A special method that registers as a Bot.slash_command_check() check.

This function can be a coroutine and must take a sole parameter, inter, to represent the ApplicationCommandInteraction.

bot_user_command_check_once(inter)[source]

Similar to Bot.slash_command_check_once() but for user commands.

bot_user_command_check(inter)[source]

Similar to Bot.slash_command_check() but for user commands.

bot_message_command_check_once(inter)[source]

Similar to Bot.slash_command_check_once() but for message commands.

bot_message_command_check(inter)[source]

Similar to Bot.slash_command_check() but for message commands.

cog_check(ctx)[source]

A special method that registers as a check() for every text command and subcommand in this cog.

This is for text commands only, and doesn’t apply to application commands.

This function can be a coroutine and must take a sole parameter, ctx, to represent the Context.

cog_slash_command_check(inter)[source]

A special method that registers as a check() for every slash command and subcommand in this cog.

This function can be a coroutine and must take a sole parameter, inter, to represent the ApplicationCommandInteraction.

cog_user_command_check(inter)[source]

Similar to Cog.cog_slash_command_check() but for user commands.

cog_message_command_check(inter)[source]

Similar to Cog.cog_slash_command_check() but for message commands.

await cog_command_error(ctx, error)[source]

A special method that is called whenever an error is dispatched inside this cog.

This is for text commands only, and doesn’t apply to application commands.

This is similar to on_command_error() except only applying to the commands inside this cog.

This must be a coroutine.

Parameters:
  • ctx (Context) – The invocation context where the error happened.

  • error (CommandError) – The error that was raised.

await cog_slash_command_error(inter, error)[source]

A special method that is called whenever an error is dispatched inside this cog.

This is similar to on_slash_command_error() except only applying to the slash commands inside this cog.

This must be a coroutine.

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

Similar to cog_slash_command_error() but for user commands.

await cog_message_command_error(inter, error)[source]

Similar to cog_slash_command_error() but for message commands.

await cog_before_invoke(ctx)[source]

A special method that acts as a cog local pre-invoke hook, similar to Command.before_invoke().

This is for text commands only, and doesn’t apply to application commands.

This must be a coroutine.

Parameters:

ctx (Context) – The invocation context.

await cog_after_invoke(ctx)[source]

A special method that acts as a cog local post-invoke hook, similar to Command.after_invoke().

This is for text commands only, and doesn’t apply to application commands.

This must be a coroutine.

Parameters:

ctx (Context) – The invocation context.

await cog_before_slash_command_invoke(inter)[source]

A special method that acts as a cog local pre-invoke hook.

This is similar to Command.before_invoke() but for slash commands.

This must be a coroutine.

Parameters:

inter (ApplicationCommandInteraction) – The interaction of the slash command.

await cog_after_slash_command_invoke(inter)[source]

A special method that acts as a cog local post-invoke hook.

This is similar to Command.after_invoke() but for slash commands.

This must be a coroutine.

Parameters:

inter (ApplicationCommandInteraction) – The interaction of the slash command.

await cog_before_user_command_invoke(inter)[source]

Similar to cog_before_slash_command_invoke() but for user commands.

await cog_after_user_command_invoke(inter)[source]

Similar to cog_after_slash_command_invoke() but for user commands.

await cog_before_message_command_invoke(inter)[source]

Similar to cog_before_slash_command_invoke() but for message commands.

await cog_after_message_command_invoke(inter)[source]

Similar to cog_after_slash_command_invoke() but for message commands.

CogMeta

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

A metaclass for defining a cog.

Note that you should probably not use this directly. It is exposed purely for documentation purposes along with making custom metaclasses to intermix with other metaclasses such as the abc.ABCMeta metaclass.

For example, to create an abstract cog mixin class, the following would be done.

import abc

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

class SomeMixin(metaclass=abc.ABCMeta):
    pass

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

Note

When passing an attribute of a metaclass that is documented below, note that you must pass it as a keyword-only argument to the class creation like the following example:

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

The cog name. By default, it is the name of the class with no modification.

Type:

str

description

The cog description. By default, it is the cleaned docstring of the class.

New in version 1.6.

Type:

str

command_attrs

A list of attributes to apply to every command inside this cog. The dictionary is passed into the Command options at __init__. If you specify attributes inside the command attribute in the class, it will override the one specified inside this attribute. For example:

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:

Dict[str, Any]

slash_command_attrs

A list of attributes to apply to every slash command inside this cog. The dictionary is passed into the options of every InvokableSlashCommand at __init__. Usage of this kwarg is otherwise the same as with command_attrs.

Note

This does not apply to instances of SubCommand or SubCommandGroup.

New in version 2.5.

Type:

Dict[str, Any]

user_command_attrs

A list of attributes to apply to every user command inside this cog. The dictionary is passed into the options of every InvokableUserCommand at __init__. Usage of this kwarg is otherwise the same as with command_attrs.

New in version 2.5.

Type:

Dict[str, Any]

message_command_attrs

A list of attributes to apply to every message command inside this cog. The dictionary is passed into the options of every InvokableMessageCommand at __init__. Usage of this kwarg is otherwise the same as with command_attrs.

New in version 2.5.

Type:

Dict[str, Any]