curious.commands.manager

Contains the class for the commands manager for a client.

Classes

CommandsManager(client, *[, message_check]) A manager that handles commands for a client.
class curious.commands.manager.CommandsManager(client: curious.core.client.Client, *, message_check=None, command_prefix: str = None)[source]

Bases: object

A manager that handles commands for a client.

First, you need to create the manager and attach it to a client:

# form 1, automatically register with the client
manager = CommandsManager.with_client(bot)

# form 2, manually register
manager = CommandsManager(bot)
manager.register_events()

This is required to add the handler events to the client.

Next, you need to register a message check handler. This is a callable that is called for every message to try and extract the command from a message, if it matches. By default, the manager provides an easy way to use a simple command prefix:

# at creation time
manager = CommandsManager(bot, command_prefix="!")

# or set it on the manager
manager.command_prefix = "!"

At this point, the command prefix will be available on the manager with either Manager.command_prefix or Manager.message_check.prefix.

If you need more complex message checking, you can use message_check:

manager = CommandsManager(bot, message_check=my_message_checker)
# or
manager.message_check = my_message_checker

Finally, you can register plugins or modules containing plugins with the manager:

@bot.event("ready")
async def load_plugins(ctx: EventContext):
    # load plugin explicitly
    await manager.load_plugin(PluginClass, arg1)
    # load plugins from a module
    await manager.load_plugins_from("my.plugin.module")

You can also add free-standing commands that aren’t bound to a plugin with CommandsManager.add_command():

@command()
async def ping(ctx: CommandsContext):
    await ctx.channel.messages.send(content="Ping!")

manager.add_command(ping)

These will then be available to the client.

Parameters:
  • client (Client) – The Client to use with this manager.
  • message_check

    The message check function for this manager.

    This should take two arguments, the client and message, and should return either None or a 2-item tuple:

    • The command word matched
    • The tokens after the command word
_lookup_command(name)[source]

Does a lookup in plugin and standalone commands.

add_command(command)[source]

Adds a command.

Parameters:command – A command function.
await default_command_error(ev_ctx, ctx, err)[source]

Handles command errors by default.

await event_hook(ctx, *args, **kwargs)[source]

The event hook for the commands manager.

get_command(command_name)[source]

Gets a command from the internal command storage. If provided a string separated by spaces, a subcommand lookup will be attempted.

Parameters:command_name (str) – The name of the command to lookup.
await handle_commands(ctx, message)[source]

Handles commands for a message.

await handle_message(ctx, message)[source]

Registered as the event handler in a client for handling commands.

await load_plugin(klass, *args, module=None)[source]

Loads a plugin.

Note

The client instance will automatically be provided to the Plugin’s __init__.

Parameters:
  • klass (Type[Plugin]) – The plugin class to load.
  • args – Any args to provide to the plugin.
  • module (Optional[str]) – The module name provided with this plugin. Only used interally.
await load_plugins_from(import_path)[source]

Loads plugins from the specified module.

Parameters:import_path (str) – The import path to import.
register_events()[source]

Copies the events to the client specified on this manager.

Return type:None
remove_command(command)[source]

Removes a command.

Parameters:command – The name of the command, or the command function.
await unload_plugin(klass)[source]

Unloads a plugin.

Parameters:klass (Union[Plugin, str]) – The plugin class or name of plugin to unload.
await unload_plugins_from(import_path)[source]

Unloads plugins from the specified module. This will delete the module from sys.path.

Parameters:import_path (str) – The import path.
classmethod with_client(**kwargs)[source]

Creates a manager and automatically registers events.