feat: basic commands + prototype of roll parser
This commit is contained in:
parent
4b47899a3d
commit
74f991af40
4 changed files with 84 additions and 1 deletions
34
dice.py
Normal file
34
dice.py
Normal 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())))
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue