Files
rona-backend/chatCommands.py
2020-07-27 16:38:26 +03:00

35 lines
878 B
Python

from typing import Dict
import re
import random
commands: Dict = {}
def command(*commandlist):
def add_command(function):
for command in commandlist:
commands[command] = function
return function
return add_command
def handle_command(kwargs):
c = kwargs['message'].split()[0]
params = kwargs['message'].replace(c, '').strip()
if c in commands:
commands[c](kwargs, params)
else:
error_response(kwargs, params)
@command('/roll')
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, params):
print(f'Error, received unknown command: {kwargs}')