Files
rona-backend/chatCommands.py

35 lines
878 B
Python
Raw Normal View History

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