2018-09-01 09:22:44 +00:00
|
|
|
import random
|
2018-09-26 17:27:17 +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-09-30 16:29:09 +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-09-01 09:22:44 +00:00
|
|
|
raw_input = event["args_split"][0]
|
|
|
|
roll = raw_input.split("d")
|
|
|
|
results = []
|
|
|
|
|
|
|
|
if len(roll) is not 2:
|
2018-09-26 17:27:17 +00:00
|
|
|
event["stderr"].write(ERROR_FORMAT)
|
2018-09-01 09:22:44 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if roll[0].isdigit() is False or roll[1].isdigit() is False:
|
2018-09-26 17:27:17 +00:00
|
|
|
event["stderr"].write(ERROR_FORMAT)
|
2018-09-01 09:22:44 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
roll = [int(roll[0]), int(roll[1])]
|
|
|
|
|
|
|
|
num_of_die = 5 if roll[0] > 5 else roll[0]
|
|
|
|
sides_of_die = 20 if roll[1] > 20 else roll[1]
|
|
|
|
|
|
|
|
str_roll = str(num_of_die) + "d" + str(sides_of_die)
|
|
|
|
|
|
|
|
for i in range(0, num_of_die):
|
|
|
|
results.append(random.randint(1, sides_of_die))
|
|
|
|
|
|
|
|
total = sum(results)
|
|
|
|
results = ', '.join(map(str, results))
|
|
|
|
|
2018-09-01 09:52:04 +00:00
|
|
|
event["stdout"].write("Rolled " + Utils.bold(str_roll) + " for a total "
|
|
|
|
+ "of " + Utils.bold(str(total))
|
|
|
|
+ ": " + results)
|