2018-09-01 09:22:44 +00:00
|
|
|
import random
|
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"
|
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)
|
2018-09-01 09:22:44 +00:00
|
|
|
def roll_dice(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Roll some dice, DND style!
|
|
|
|
:usage: [1-5]d[1-20]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-11-17 12:18:26 +00:00
|
|
|
roll = event["args_split"][0].lower()
|
2018-11-17 12:19:32 +00:00
|
|
|
count, _, sides = roll.partition("d")
|
2018-11-17 12:18:26 +00:00
|
|
|
if not count.isdigit() or not sides.isdigit():
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError(ERROR_FORMAT)
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2018-11-17 12:21:09 +00:00
|
|
|
count_n = min(5, int(count))
|
|
|
|
sides_n = min(20, int(sides))
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2018-11-17 12:29:24 +00:00
|
|
|
results = random.choices(range(1, sides_n+1), k=count_n)
|
2018-09-01 09:22:44 +00:00
|
|
|
|
2018-11-16 17:39:33 +00:00
|
|
|
total_n = sum(results)
|
|
|
|
total = ""
|
|
|
|
if len(results) > 1:
|
|
|
|
total = " (total: %d)" % total_n
|
2018-11-17 12:18:26 +00:00
|
|
|
|
2018-11-17 12:26:21 +00:00
|
|
|
event["stdout"].write("Rolled %s and got %s%s" % (
|
|
|
|
roll, ", ".join(str(r) for r in results), total))
|