48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
from typing import Dict
|
|
import re
|
|
import numpy as np
|
|
|
|
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, '')
|
|
params = re.sub(r'\s+', '', params)
|
|
|
|
if c in commands:
|
|
commands[c](kwargs, params)
|
|
else:
|
|
error_response(kwargs, params)
|
|
|
|
|
|
@command('/roll')
|
|
def custom_roll(kwargs, params):
|
|
pattern = re.compile('(?P<sign>(?:\+|\-)?)(?P<dice_num>\d+)(?i:d)(?P<dice_val>\d+)(?P<maths>(?:(?:\+|\-)\d+)*(?!(?i:d)))')
|
|
rolls = re.findall(pattern, params)
|
|
|
|
rng = np.random.default_rng()
|
|
results = []
|
|
for roll in rolls:
|
|
result = []
|
|
if int(roll[2]) < 2:
|
|
result.append(int(roll[2]))
|
|
else:
|
|
result.append(rng.integers(1, int(roll[2]), size=int(roll[1])).tolist())
|
|
result.append(roll[3])
|
|
results.append(result)
|
|
print(results)
|
|
print(f'/roll received {kwargs}. Roll parameters: {params}. Rolls recognized: {rolls}')
|
|
|
|
|
|
def error_response(kwargs, params):
|
|
print(f'Error, received unknown command: {kwargs}')
|