2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
2019-09-26 13:04:59 +00:00
|
|
|
import random, re
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
ERROR_FORMAT = "Incorrect format! Format must be [number]d[number], e.g. 1d20"
|
2019-09-26 13:04:59 +00:00
|
|
|
RE_DICE = re.compile("([1-9]\d*)d([1-9]\d*)((?:[-+]\d+)*)", re.I)
|
|
|
|
RE_MODIFIERS = re.compile("([-+]\d+)")
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.roll", min_args=1)
|
2019-09-26 13:06:58 +00:00
|
|
|
@utils.hook("received.command.dice", alias_of="roll")
|
2018-09-01 09:22:44 +00:00
|
|
|
def roll_dice(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-06-18 16:33:42 +00:00
|
|
|
:help: Roll some dice, DND style
|
2019-09-26 13:09:59 +00:00
|
|
|
:usage: [1-6]d[1-30]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-09-26 13:04:59 +00:00
|
|
|
match = RE_DICE.match(event["args_split"][0])
|
|
|
|
if match:
|
|
|
|
roll = match.group(0)
|
|
|
|
dice_count = int(match.group(1))
|
|
|
|
side_count = int(match.group(2))
|
|
|
|
modifiers = RE_MODIFIERS.findall(match.group(3))
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2019-09-26 13:04:59 +00:00
|
|
|
if dice_count > 6:
|
|
|
|
raise utils.EventError("Max number of dice is 6")
|
2019-09-26 13:09:59 +00:00
|
|
|
if side_count > 30:
|
|
|
|
raise utils.EventError("Max number of sides is 30")
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2019-09-26 13:04:59 +00:00
|
|
|
results = random.choices(range(1, side_count+1), k=dice_count)
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2019-09-26 13:04:59 +00:00
|
|
|
total_n = sum(results)
|
|
|
|
for modifier in modifiers:
|
|
|
|
if modifier[0] == "+":
|
|
|
|
total_n += int(modifier[1:])
|
|
|
|
else:
|
|
|
|
total_n -= int(modifier[1:])
|
2018-11-17 12:18:26 +00:00
|
|
|
|
2019-09-26 13:04:59 +00:00
|
|
|
total = ""
|
|
|
|
if len(results) > 1:
|
|
|
|
total = " (total: %d)" % total_n
|
|
|
|
|
|
|
|
results_str = ", ".join(str(r) for r in results)
|
|
|
|
event["stdout"].write("Rolled %s and got %s%s" % (
|
|
|
|
roll, results_str, total))
|