added regex for dice rolls

This commit is contained in:
2020-07-27 16:38:26 +03:00
parent c5a482c9b1
commit 42f0463e26
2 changed files with 12 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
from typing import Dict
import re
import random
commands: Dict = {}
@@ -15,17 +15,20 @@ def command(*commandlist):
def handle_command(kwargs):
c = kwargs['message'].split()[0]
params = kwargs['message'].replace(c, '').strip()
if c in commands:
commands[c](kwargs)
commands[c](kwargs, params)
else:
error_response(kwargs)
error_response(kwargs, params)
@command('/roll')
def custom_roll(kwargs):
print(f'/roll received {kwargs}')
def custom_roll(kwargs, params):
pattern = re.compile('(?P<dice_num>(?:\+|\-)?\d+)(?i:d)(?P<dice_val>\d+)(?P<maths>(?:(?:\+|\-)\d+)*(?!(?i:d)))')
rolls = re.findall(pattern, params)
print(f'/roll received {kwargs}. Roll parameters: {params}. Rolls recognized: {rolls}')
def error_response(kwargs):
def error_response(kwargs, params):
print(f'Error, received unknown command: {kwargs}')