def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) if content_type != 'text' or 'forward_from' in msg: return for command in reversed(COMMANDS): if msg['text'].startswith(command): expression = msg['text'][len(command):] break else: return #print("Command:", msg['text']) print(expression) try: tokens, reason = parse(expression) print(expression, tokens, reason) rolled = roll(tokens) result = count(rolled) print(tokens) if reason: reason += '\n' bot.sendMessage(chat_id, '🎲 %s%s -> %s = <b>%d</b>' % (reason, ' '.join(tokens), ' '.join(map(mystr, rolled)), result), parse_mode='HTML') except Exception as e: print_exc() bot.sendMessage(chat_id, '‼ Какая-то хрень ‼')
def test_parse_comments(self): self.assertEqual(parse('2d6-1 test строка'), (['2d6', '-', '1'], 'test строка')) self.assertEqual(parse('+2 test'), (['2d6', '+', '2'], 'test')) self.assertEqual(parse('+bonus'), (['2d6'], '+bonus')) self.assertEqual(parse('d2 d3'), (['d2'], 'd3')) self.assertEqual(parse('d2d4'), (['d2'], 'd4')) self.assertEqual(parse('destroy'), (['2d6'], 'destroy')) self.assertEqual(parse('d3stroy'), (['d3'], 'stroy')) self.assertEqual(parse('2к6-1 test строка!'), (['2d6', '-', '1'], 'test строка!')) self.assertEqual(parse('2д6-1 test строка!'), (['2d6', '-', '1'], 'test строка!'))
def testMultiDiceOnly(self): seed(1234) self.assertEqual(roll_parser.parse('3d6'), 6)
def test_parse_empty(self): self.assertEqual(parse(''), (['2d6'], ''))
def test_parse_plus(self): self.assertEqual(parse('+2'), (['2d6', '+', '2'], ''))
def test_parse_subtract(self): self.assertEqual(parse('1-2d4'), (['1', '-', '2d4'], ''))
def test_parse_spaces(self): self.assertEqual(parse('1 + 1'), (['1', '+', '1'], ''))
def test_parse_constant(self): self.assertEqual(parse('1'), (['1'], ''))