Decorator to register a coroutine function as a command handler.
The command name defaults to the function name unless
explicitly provided.
Example
@bot.group("math")
async def math(ctx: Context) -> None:
await ctx.send_help()
@math.command()
async def add(ctx: Context, a: int, b: int) -> None:
await ctx.reply(f"{a + b}")
@math.command("sub")
async def subtract(ctx: Context, a: int, b: int) -> None:
await ctx.reply(f"{a - b}")
Source code in matrix/group.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 | def command(self, name: Optional[str] = None) -> Callable[[Callback], Command]:
"""
Decorator to register a coroutine function as a command handler.
The command name defaults to the function name unless
explicitly provided.
## Example
```python
@bot.group("math")
async def math(ctx: Context) -> None:
await ctx.send_help()
@math.command()
async def add(ctx: Context, a: int, b: int) -> None:
await ctx.reply(f"{a + b}")
@math.command("sub")
async def subtract(ctx: Context, a: int, b: int) -> None:
await ctx.reply(f"{a - b}")
```
"""
def wrapper(func: Callback) -> Command:
cmd = Command(func, name=name, prefix=self.prefix, parent=self.name)
return self.register_command(cmd)
return wrapper
|