Skip to content

[FAQ] regex command

Housa Hitomi edited this page Apr 22, 2022 · 2 revisions

TL;DR

@bot.command(regex=r'/(\d+)d(\d+)')
async def dice(m: Message, _: Bot, n: int, maxi: int):
    await m.reply(f'you got {[random.randint(0, max(0, maxi)) for _ in range(n)]}')
    # /2d20
    # you got [11, 4]

custom additional params

likes normal command, regex command accepts custom additional params

capturing in regex is used to get params from message content

take the example: r'/(\d+)d(\d+)' contains two capturing groups, the first corresponds to the 1st custom param: n: int, and the second is put into the following maxi: int