added regex for dice rolls
This commit is contained in:
@@ -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<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}')
|
||||
|
||||
5
main.py
5
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)
|
||||
|
||||
Reference in New Issue
Block a user