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(?:\+|\-)?\d+)(?i:d)(?P\d+)(?P(?:(?:\+|\-)\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}')