feat: basic commands + prototype of roll parser

This commit is contained in:
RatCornu 2026-03-24 00:24:58 +01:00
parent 4b47899a3d
commit 74f991af40
Signed by: ratcornu
GPG key ID: B3BE02E379E6E8E2
4 changed files with 84 additions and 1 deletions

34
dice.py Normal file
View file

@ -0,0 +1,34 @@
from random import randint
from pyparsing import Combine, OpAssoc, Word, infix_notation, nums, one_of
SEPARATOR = "|"
class Dice:
def __init__(self, tokens):
split_tokens = tokens[0].split(SEPARATOR)
self.dice, self.faces = split_tokens[0], split_tokens[2]
def roll_parse(roll):
integer = Word(nums)
dice = Combine(integer + "d" + integer, join_string="|")
dice.set_parse_action(Dice)
expr = infix_notation(
dice | integer,
[
("-", 1, OpAssoc.RIGHT),
(one_of("* /"), 2, OpAssoc.LEFT),
(one_of("+ -"), 2, OpAssoc.LEFT),
],
)
return expr.parse_string(roll, parse_all=True)
async def roll(bot, mtch, room, message):
await bot.api.send_text_message(
room.room_id, str(roll_parse(" ".join(mtch.args())))
)