Skip to main content
Version: Latest

Command Handlers

Here you will learn how to create command handlers!

To create a handler, you will need the object cmd which can be obtained from the module object.

cmd is a decorator.

Parameters:

  • commands (str|List[str]) - the command(s) to respond to.

Example

Module/__init__.py
from utils import *

async def main(app: Client, mod: Module):

cmd = mod.cmd

@cmd(['cmd', 'my_command'])
async def _cmd(_, msg: M):
pass

warning

Use ONLY mod.cmd for registering commands! It is strongly not recommended to use custom handlers!

warning

Command functions (and other handlers) must be unique.

tip

Name handler functions starting with _. For example, _my_command or _hello.


Error Handling

When using mod.cmd, errors will automatically be logged to the console.

Example Output
18-11-2024 21:31:41 RimTUB [0] ERROR: Error in command test (module TestModule):
Traceback (most recent call last):
File "/plugins/TestModule/__init__.py", line 10, in
raise ModuleError('test error')
ModuleError: test error

However, if you want errors to be displayed to the user, you should handle them manually:

Module/__init__.py
from utils import *
from traceback import format_exc

async def main(app: Client, mod: Module):

cmd = mod.cmd

@cmd('test')
async def _test(_, msg: M):
try:
raise ModuleError('test error')
except:
# log the traceback to the console
mod.logger.error('Oops! An error occurred!', exc_info=True)
# display it to the user by uploading it to pastes.dev
await msg.edit(f"Error! {await paste(format_exc())}")