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()))) )