Command
A Command wraps an async callback together with its metadata (name, description, usage, cooldown, and checks). Commands are normally created via the @bot.command() decorator rather than instantiated directly.
from matrix import Bot, Context
bot = Bot()
@bot.command("greet", description="Greet a user by name", usage="<name>")
async def greet(ctx: Context, name: str):
await ctx.reply(f"Hello, {name}!")
matrix.command.Command
Command(
func,
*,
name=None,
description=None,
prefix=None,
parent=None,
usage=None,
cooldown=None
)
Represents a command that can be executed with a context and arguments.
:param func: The coroutine that is executed when the command is invoked. :type func: Callable[..., Coroutine[Any, Any, Any]]
:param name: Optional name. Defaults to the function's name. :param description: Optional description of what the command does. :param prefix: Optional prefix for the command. :param parent: Optional parent command name for subcommands. :param usage: Optional usage string for the command. :param cooldown: Optional cooldown settings as a tuple of (rate, period).
:raises TypeError: If the provided name is not a string. :raises TypeError: If the provided callback is not a coroutine.
Source code in matrix/command.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
name
instance-attribute
name = name or __name__
checks
instance-attribute
checks = []
description
instance-attribute
description = description or ''
prefix
instance-attribute
prefix = prefix or ''
parent
instance-attribute
parent = parent or ''
usage
instance-attribute
usage = usage or _build_usage()
help
instance-attribute
help = _build_help()
cooldown_rate
instance-attribute
cooldown_rate = None
cooldown_period
instance-attribute
cooldown_period = None
cooldown_calls
instance-attribute
cooldown_calls = defaultdict(deque)
callback
property
writable
callback
Returns the coroutine function for this command.
:return: The command's coroutine function. :rtype: Callback
check
check(func)
Register a check callback
:param func: The check callback :type func: Callback
:raises TypeError: If the function is not a coroutine.
Source code in matrix/command.py
185 186 187 188 189 190 191 192 193 194 195 196 197 | |
set_cooldown
set_cooldown(rate, period)
Source code in matrix/command.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
before_invoke
before_invoke(func)
Registers a coroutine to be called before the command is invoked.
:param func: The coroutine function to call before command invocation. :type func: Callback
:raises TypeError: If the function is not a coroutine.
Source code in matrix/command.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
after_invoke
after_invoke(func)
Registers a coroutine to be called after the command is invoked.
:param func: The coroutine function to call after command execution. :type func: Callback
:raises TypeError: If the function is not a coroutine.
Source code in matrix/command.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
error
error(exception=None)
Decorator used to register an error handler for this command.
:param exception: Exception type to register the handler for. :type exception: Optional[Exception] :return: A decorator that registers the provided coroutine as an error handler and returns the original function. :rtype: Callable
Source code in matrix/command.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
on_error
async
on_error(ctx, error)
Executes the registered error handler if present.
:param ctx: The command execution context. :type ctx: Context :param error: The exception that was raised. :type error: Exception
Source code in matrix/command.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
invoke
async
invoke(ctx)
Source code in matrix/command.py
302 303 304 | |