bitbot-3.11-fork/modules/dice.py

30 lines
928 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)
2018-11-17 12:18:26 +00:00
count_n = min(int(roll[0]), 5)
sides_n = min(int(roll[1]), 20)
2018-11-17 12:18:26 +00:00
reults = random.choices(range(1, die_sides), k=die_count)
total_n = sum(results)
total = ""
if len(results) > 1:
total = " (total: %d)" % total_n
2018-11-17 12:18:26 +00:00
event["stdout"].write("Rolled %s and got %s%s" % (
2018-11-17 12:18:26 +00:00
roll, total_n, ", ".join(results)))