2020-07-23 20:05:13 +03:00
|
|
|
from typing import Dict
|
2020-07-27 16:38:26 +03:00
|
|
|
import re
|
2020-07-27 18:07:00 +03:00
|
|
|
import numpy as np
|
2020-07-23 20:05:13 +03:00
|
|
|
|
2020-07-23 20:53:13 +03:00
|
|
|
commands: Dict = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def command(*commandlist):
|
|
|
|
|
def add_command(function):
|
|
|
|
|
for command in commandlist:
|
|
|
|
|
commands[command] = function
|
|
|
|
|
return function
|
|
|
|
|
return add_command
|
|
|
|
|
|
2020-07-23 20:05:13 +03:00
|
|
|
|
|
|
|
|
def handle_command(kwargs):
|
2020-07-23 20:53:13 +03:00
|
|
|
c = kwargs['message'].split()[0]
|
2020-07-27 18:07:00 +03:00
|
|
|
params = kwargs['message'].replace(c, '')
|
|
|
|
|
params = re.sub(r'\s+', '', params)
|
2020-07-23 20:05:13 +03:00
|
|
|
|
2020-07-23 20:53:13 +03:00
|
|
|
if c in commands:
|
2020-07-27 16:38:26 +03:00
|
|
|
commands[c](kwargs, params)
|
2020-07-23 20:53:13 +03:00
|
|
|
else:
|
2020-07-27 16:38:26 +03:00
|
|
|
error_response(kwargs, params)
|
2020-07-23 20:05:13 +03:00
|
|
|
|
|
|
|
|
|
2020-07-23 20:53:13 +03:00
|
|
|
@command('/roll')
|
2020-07-27 16:38:26 +03:00
|
|
|
def custom_roll(kwargs, params):
|
2020-07-27 18:07:00 +03:00
|
|
|
pattern = re.compile('(?P<sign>(?:\+|\-)?)(?P<dice_num>\d+)(?i:d)(?P<dice_val>\d+)(?P<maths>(?:(?:\+|\-)\d+)*(?!(?i:d)))')
|
2020-07-27 16:38:26 +03:00
|
|
|
rolls = re.findall(pattern, params)
|
2020-07-27 18:07:00 +03:00
|
|
|
|
|
|
|
|
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)
|
2020-07-27 16:38:26 +03:00
|
|
|
print(f'/roll received {kwargs}. Roll parameters: {params}. Rolls recognized: {rolls}')
|
2020-07-23 20:05:13 +03:00
|
|
|
|
|
|
|
|
|
2020-07-27 16:38:26 +03:00
|
|
|
def error_response(kwargs, params):
|
2020-07-23 20:53:13 +03:00
|
|
|
print(f'Error, received unknown command: {kwargs}')
|