Skip to content

Checks

Checks are decorators that gate command execution. They run before the command callback and can block invocation by raising a CheckError. matrix.py ships with a built-in cooldown check; you can also write custom checks with Registry.add_check.

from matrix import Bot, Context
from matrix.checks import cooldown

bot = Bot()

@bot.command("roll")
@cooldown(rate=1, period=10.0)
async def roll(ctx: Context):
    await ctx.reply("🎲 You rolled a 6!")

matrix.checks

cooldown

cooldown(rate, period)

Decorator to cooldown a command.

Example
@cooldown(rate=3, period=10)
@bot.command("hello")
async def hello(ctx: Context) -> None:
    await ctx.reply("Hello!")

@hello.error(CooldownError)
async def hello_error(ctx: Context, error: CooldownError) -> None:
    await ctx.reply(f"Slow down! Try again in {error.retry:.1f}s.")
Source code in matrix/checks.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def cooldown(rate: int, period: float) -> Callable:
    """
    Decorator to cooldown a command.

    ## Example

    ```python
    @cooldown(rate=3, period=10)
    @bot.command("hello")
    async def hello(ctx: Context) -> None:
        await ctx.reply("Hello!")

    @hello.error(CooldownError)
    async def hello_error(ctx: Context, error: CooldownError) -> None:
        await ctx.reply(f"Slow down! Try again in {error.retry:.1f}s.")
    ```
    """

    def wrapper(cmd: "Command") -> "Command":
        cmd.set_cooldown(rate, period)
        return cmd

    return wrapper