bitbot-3.11-fork/modules/dice.py

30 lines
937 B
Python
Raw Normal View History

import random
from src import ModuleManager, utils
ERROR_FORMAT = "Incorrect format! Format must be [number]d[number], e.g. 1d20"
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.roll", min_args=1)
def roll_dice(self, event):
"""
:help: Roll some dice, DND style!
:usage: [1-5]d[1-20]
"""
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():
raise utils.EventError(ERROR_FORMAT)
count_n = min(5, int(count))
sides_n = min(20, int(sides))
2018-11-17 12:29:24 +00:00
results = random.choices(range(1, sides_n+1), k=count_n)
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))