Cogs

class discord.Cog(*args: Any, **kwargs: Any)[ソース]

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()[ソース]
戻り値:

A list of ApplicationCommands that are defined inside this cog.

注釈

This does not include subcommands.

戻り値の型:

list[ApplicationCommand]

property qualified_name: str

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

property description: str

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

walk_commands()[ソース]

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

列挙:

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

戻り値の型:

Generator[ApplicationCommand]

get_listeners()[ソース]

Returns a list of (name, function) listener pairs that are defined in this cog.

戻り値:

The listeners defined in this cog.

戻り値の型:

list[tuple[str, Callable[..., Any]]]

classmethod listener(name=..., once=False)[ソース]

A decorator that marks a function as a listener.

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

パラメータ:
  • name (str) -- The name of the event being listened to. If not provided, it defaults to the function's name.

  • once (bool) -- If this listener should only be called once after each cog load. Defaults to false.

例外:

TypeError -- The function is not a coroutine function or a string was not passed as the name.

戻り値の型:

Callable[[TypeVar(FuncT, bound= Callable[..., Any])], TypeVar(FuncT, bound= Callable[..., Any])]

has_error_handler()[ソース]

Checks whether the cog has an error handler.

Added in version 1.7.

戻り値の型:

bool

cog_unload()[ソース]

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.

戻り値の型:

None

bot_check_once(ctx)[ソース]

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

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

パラメータ:

ctx (ApplicationContext) -- The invocation context.

戻り値の型:

bool

bot_check(ctx)[ソース]

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

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

パラメータ:

ctx (ApplicationContext) -- The invocation context.

戻り値の型:

bool

cog_check(ctx)[ソース]

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

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

パラメータ:

ctx (ApplicationContext) -- The invocation context.

戻り値の型:

bool

await cog_command_error(ctx, error)[ソース]

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

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

This must be a coroutine.

パラメータ:
戻り値の型:

None

await cog_before_invoke(ctx)[ソース]

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

This is similar to ApplicationCommand.before_invoke().

This must be a coroutine.

パラメータ:

ctx (ApplicationContext) -- The invocation context.

戻り値の型:

None

await cog_after_invoke(ctx)[ソース]

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

This is similar to ApplicationCommand.after_invoke().

This must be a coroutine.

パラメータ:

ctx (ApplicationContext) -- The invocation context.

戻り値の型:

None

class discord.CogMeta(*args, **kwargs)[ソース]

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(discord.CogMeta, abc.ABCMeta):
    pass

class SomeMixin(metaclass=abc.ABCMeta):
    pass

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

注釈

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(discord.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.

Added 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(discord.Cog, command_attrs=dict(hidden=True)):
    @discord.slash_command()
    async def foo(self, ctx):
        pass # hidden -> True

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

dict

guild_ids

A shortcut to command_attrs, what guild_ids should all application commands have in the cog. You can override this by setting guild_ids per command.

Added in version 2.0.

Type:

Optional[List[int]]

パラメータ: