Commands Package

This package acts almost identically to discord.ext.commands; i.e. all of the attributes from discord.py’s are also in ours. Some of these attributes, however, have been slightly modified, while others have been added to extend functionlities used throughout the bot, as outlined below.

redbot.core.commands.command(name=None, cls=<class 'redbot.core.commands.commands.Command'>, **attrs)[source]

A decorator which transforms an async function into a Command.

Same interface as discord.ext.commands.command.

redbot.core.commands.group(name=None, cls=<class 'redbot.core.commands.commands.Group'>, **attrs)[source]

A decorator which transforms an async function into a Group.

Same interface as discord.ext.commands.group.

class redbot.core.commands.Command(*args, **kwargs)[source]

Bases: redbot.core.commands.commands.CogCommandMixin, discord.ext.commands.core.Command

Command class for Red.

This should not be created directly, and instead via the decorator.

This class inherits from discord.ext.commands.Command. The attributes listed below are simply additions to the ones listed with that class.

checks

A list of check predicates which cannot be overridden, unlike Requires.checks.

Type:List[coroutine function]
translator

A translator for this command’s help docstring.

Type:Translator
allow_for(model_id, guild_id)[source]

Actively allow this command for the given model.

Parameters:
  • model_id (Union[int, str]) – Must be an int if supplying an ID. str is only valid for “default”.
  • guild_id (int) – The guild ID to allow this cog or command in. For global rules, use 0.
await can_run(ctx, *, check_all_parents=False, change_permission_state=False)[source]

Check if this command can be run in the given context.

This function first checks if the command can be run using discord.py’s method discord.ext.commands.Command.can_run, then will return the result of Requires.verify.

Keyword Arguments:
 
  • check_all_parents (bool) – If True, this will check permissions for all of this command’s parents and its cog as well as the command itself. Defaults to False.
  • change_permission_state (bool) – Whether or not the permission state should be changed as a result of this call. For most cases this should be False. Defaults to False.
await can_see(ctx)[source]

Check if this command is visible in the given context.

In short, this will verify whether the user can run the command, and also whether the command is hidden or not.

Parameters:ctx (Context) – The invocation context to check with.
Returns:True if this command is visible in the given context.
Return type:bool
clear_rule_for(model_id, guild_id)[source]

Clear the rule which is currently set for this model.

Parameters:
  • model_id (Union[int, str]) – Must be an int if supplying an ID. str is only valid for “default”.
  • guild_id (int) – The guild ID. For global rules, use 0.
disable_in(guild)[source]

Disable this command in the given guild.

Parameters:guild (discord.Guild) – The guild to disable the command in.
Returns:True if the command wasn’t already disabled.
Return type:bool
await do_conversion(ctx, converter, argument, param)[source]

Convert an argument according to its type annotation.

Raises:ConversionFailure – If doing the conversion failed.
Returns:The converted argument.
Return type:Any
enable_in(guild)[source]

Enable this command in the given guild.

Parameters:guild (discord.Guild) – The guild to enable the command in.
Returns:True if the command wasn’t already enabled.
Return type:bool
error(coro)[source]

A decorator that registers a coroutine as a local error handler.

A local error handler is an on_command_error() event limited to a single command.

The on_command_error event is still dispatched for commands with a dedicated error handler.

Red’s global error handler will ignore commands with a registered error handler.

To have red handle specific errors with the default behavior, call Red.on_command_error with unhandled_by_cog set to True.

Due to how discord.py wraps exceptions, the exception you are expecting here is likely in error.original despite that the normal event handler for bot wide command error handling has no such wrapping.

For example:

@a_command.error
async def a_command_error_handler(self, ctx, error):

    if isinstance(error.original, MyErrrorType):
        self.log_exception(error.original)
    else:
        await ctx.bot.on_command_error(ctx, error.original, unhandled_by_cog=True)
Parameters:coro (coroutine function) – The coroutine to register as the local error handler.
Raises:discord.ClientException – The coroutine is not actually a coroutine.
help

Help string for this command.

If the help kwarg was passed into the decorator, it will default to that. If not, it will attempt to translate the docstring of the command’s callback function.

parents

Returns all parent commands of this command.

This is sorted by the length of qualified_name from highest to lowest. If the command has no parents, this will be an empty list.

Type:List[commands.Group]
class redbot.core.commands.Group(*args, **kwargs)[source]

Bases: redbot.core.commands.commands.GroupMixin, redbot.core.commands.commands.Command, redbot.core.commands.commands.CogGroupMixin, discord.ext.commands.core.Group

Group command class for Red.

This class inherits from Command, with GroupMixin and discord.ext.commands.Group mixed in.

class redbot.core.commands.Context(**attrs)[source]

Bases: discord.ext.commands.context.Context

Command invocation context for Red.

All context passed into commands will be of this type.

This class inherits from discord.ext.commands.Context.

clean_prefix

The command prefix, but a mention prefix is displayed nicer.

Type:str
await embed_colour()[source]

Helper function to get the colour for an embed.

Returns:The colour to be used
Return type:discord.Colour
await embed_requested()[source]

Simple helper to call bot.embed_requested with logic around if embed permissions are available

Returns:True if an embed is requested
Return type:bool
await maybe_send_embed(message)[source]

Simple helper to send a simple message to context without manually checking ctx.embed_requested This should only be used for simple messages.

Parameters:

message (str) – The string to send

Returns:

the message which was sent

Return type:

discord.Message

Raises:
me

The bot member or user object.

If the context is DM, this will be a discord.User object.

Type:discord.abc.User
await react_quietly(reaction)[source]

Adds a reaction to to the command message. :returns: True if adding the reaction succeeded. :rtype: bool

await send(content=None, **kwargs)[source]

Sends a message to the destination with the content given.

This acts the same as discord.ext.commands.Context.send, with one added keyword argument as detailed below in Other Parameters.

Parameters:

content (str) – The content of the message to send.

Other Parameters:
 
Returns:

The message that was sent.

Return type:

discord.Message

await send_help(command=None)[source]

Send the command help message.

await send_interactive(messages, box_lang=None, timeout=15)[source]

Send multiple messages interactively.

The user will be prompted for whether or not they would like to view the next message, one at a time. They will also be notified of how many messages are remaining on each prompt.

Parameters:
  • messages (iterable of str) – The messages to send.
  • box_lang (str) – If specified, each message will be contained within a codeblock of this language.
  • timeout (int) – How long the user has to respond to the prompt before it times out. After timing out, the bot deletes its prompt message.
await tick()[source]

Add a tick reaction to the command message.

Returns:True if adding the reaction succeeded.
Return type:bool

commands.requires

This module manages the logic of resolving command permissions and requirements. This includes rules which override those requirements, as well as custom checks which can be overriden, and some special checks like bot permissions checks.

class redbot.core.commands.requires.PrivilegeLevel[source]

Bases: enum.IntEnum

Enumeration for special privileges.

ADMIN = 3

User has the admin role.

BOT_OWNER = 5

User is a bot owner.

GUILD_OWNER = 4

User is the guild level.

MOD = 2

User has the mod role.

NONE = 1

No special privilege level.

class redbot.core.commands.requires.PermState[source]

Bases: enum.Enum

Enumeration for permission states used by rules.

ACTIVE_ALLOW = 1

This command has been actively allowed, default user checks should be ignored.

ACTIVE_DENY = 5

This command has been actively denied, terminate the command chain.

ALLOWED_BY_HOOK = 6

This command has been actively allowed by a permission hook. check validation doesn’t need this, but is useful to developers

CAUTIOUS_ALLOW = 4

This command has been actively denied, but there exists a subcommand in the ACTIVE_ALLOW state. This occurs when PASSIVE_ALLOW and ACTIVE_DENY are combined.

DENIED_BY_HOOK = 7

This command has been actively denied by a permission hook check validation doesn’t need this, but is useful to developers

NORMAL = 2

No overrides have been set for this command, make determination from default user checks.

PASSIVE_ALLOW = 3

There exists a subcommand in the ACTIVE_ALLOW state, continue down the subcommand tree until we either find it or realise we’re on the wrong branch.

class redbot.core.commands.requires.Requires(privilege_level, user_perms, bot_perms, checks)[source]

Bases: object

This class describes the requirements for executing a specific command.

The permissions described include both bot permissions and user permissions.

checks

A list of checks which can be overridden by rules. Use Command.checks if you would like them to never be overridden.

Type:List[Callable[[Context], Union[bool, Awaitable[bool]]]]
privilege_level

The required privilege level (bot owner, admin, etc.) for users to execute the command. Can be None, in which case the user_perms will be used exclusively, otherwise, for levels other than bot owner, the user can still run the command if they have the required user_perms.

Type:PrivilegeLevel
ready_event

Event for when this Requires object has had its rules loaded. If permissions is loaded, this should be set when permissions has finished loading rules into this object. If permissions is not loaded, it should be set as soon as the command or cog is added.

Type:asyncio.Event
user_perms

The required permissions for users to execute the command. Can be None, in which case the privilege_level will be used exclusively, otherwise, it will pass whether the user has the required privilege_level _or_ user_perms.

Type:Optional[discord.Permissions]
bot_perms

The required bot permissions for a command to be executed. This is not overrideable by other conditions.

Type:discord.Permissions
DEFAULT = 'default'

The key for the default rule in a rules dict.

GLOBAL = 0

Should be used in place of a guild ID when setting/getting global rules.

clear_all_rules(guild_id, *, preserve_default_rule=True)[source]

Clear all rules of a particular scope.

Parameters:guild_id (int) – The guild ID to clear rules for. If set to Requires.GLOBAL, this will clear all global rules and leave all guild rules untouched.
Other Parameters:
 preserve_default_rule (bool) – Whether to preserve the default rule or not. This defaults to being preserved
get_rule(model, guild_id)[source]

Get the rule for a particular model.

Parameters:
  • model (Union[int, str, PermissionModel]) – The model to get the rule for. str is only valid for Requires.DEFAULT.
  • guild_id (int) – The ID of the guild for the rule’s scope. Set to Requires.GLOBAL for a global rule.
Returns:

The state for this rule. See the PermState class for an explanation.

Return type:

PermState

reset()[source]

Reset this Requires object to its original state.

This will clear all rules, including defaults. It also resets the Requires.ready_event.

set_rule(model_id, rule, guild_id)[source]

Set the rule for a particular model.

Parameters:
  • model_id (Union[str, int]) – The model to add a rule for. str is only valid for Requires.DEFAULT.
  • rule (PermState) – Which state this rule should be set as. See the PermState class for an explanation.
  • guild_id (int) – The ID of the guild for the rule’s scope. Set to Requires.GLOBAL for a global rule.
await verify(ctx)[source]

Check if the given context passes the requirements.

This will check the bot permissions, overrides, user permissions and privilege level.

Parameters:

ctx ("Context") – The invkokation context to check with.

Returns:

True if the context passes the requirements.

Return type:

bool

Raises:
  • BotMissingPermissions – If the bot is missing required permissions to run the command.
  • CommandError – Propogated from any permissions checks.