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

View File

@@ -42,8 +42,9 @@ def public_message(kwargs):
if 'message' in kwargs: if 'message' in kwargs:
if kwargs['message'][0] == '/': if kwargs['message'][0] == '/':
chatCommands.handle_command(kwargs) chatCommands.handle_command(kwargs)
print(kwargs) else:
sio.emit('public message', kwargs) print(kwargs)
sio.emit('public message', kwargs)
if __name__ == '__main__': if __name__ == '__main__':
sio.run(app, port=5005) sio.run(app, port=5005)