From 42f0463e26a3d21ea0b95e2db28d6e3a0c3e5e1d Mon Sep 17 00:00:00 2001 From: yes Date: Mon, 27 Jul 2020 16:38:26 +0300 Subject: [PATCH] added regex for dice rolls --- chatCommands.py | 15 +++++++++------ main.py | 5 +++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/chatCommands.py b/chatCommands.py index 4fd18c4..8038dc4 100644 --- a/chatCommands.py +++ b/chatCommands.py @@ -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(?:\+|\-)?\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): +def error_response(kwargs, params): print(f'Error, received unknown command: {kwargs}') diff --git a/main.py b/main.py index 36dc316..a08b3c9 100644 --- a/main.py +++ b/main.py @@ -42,8 +42,9 @@ def public_message(kwargs): if 'message' in kwargs: if kwargs['message'][0] == '/': chatCommands.handle_command(kwargs) - print(kwargs) - sio.emit('public message', kwargs) + else: + print(kwargs) + sio.emit('public message', kwargs) if __name__ == '__main__': sio.run(app, port=5005)