From 3f66940e772702068288d072c96eb93020bd4873 Mon Sep 17 00:00:00 2001 From: dngfx Date: Sat, 1 Sep 2018 10:22:44 +0100 Subject: Remove superfluous code from ducks.py and introduce dice.py (DND rolling function .roll 1d20) --- modules/dice.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 modules/dice.py (limited to 'modules/dice.py') diff --git a/modules/dice.py b/modules/dice.py new file mode 100644 index 00000000..9862b172 --- /dev/null +++ b/modules/dice.py @@ -0,0 +1,43 @@ +import random + + +class Module(object): + def __init__(self, bot, events): + events.on("received.command.roll").hook( + self.roll_dice, + min_args=1, + help="Roll some dice, DND style!", + usage="[1-5]d[1-20]" + ) + + self.err_msg = "Incorrectly formatted dice! Format must be [number]d[number], for example, 1d20" + + def roll_dice(self, event): + raw_input = event["args_split"][0] + roll = raw_input.split("d") + results = [] + + if len(roll) is not 2: + event["stderr"].write(self.err_msg) + return + + if roll[0].isdigit() is False or roll[1].isdigit() is False: + event["stderr"].write(self.err_msg) + 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)) + + event["stdout"].write("Rolled " + str_roll + " for a total " + + "of " + str(total) + + ": [" + results + "]") -- cgit v1.3.1-10-gc9f91 From 0a96a79077d82de51533d914296b7a51ca19eb3b Mon Sep 17 00:00:00 2001 From: dngfx Date: Sat, 1 Sep 2018 10:52:04 +0100 Subject: Cosmetic enhancement for ducks and dice. --- modules/dice.py | 8 ++++---- modules/ducks.py | 16 +++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'modules/dice.py') diff --git a/modules/dice.py b/modules/dice.py index 9862b172..2829cf48 100644 --- a/modules/dice.py +++ b/modules/dice.py @@ -1,5 +1,5 @@ import random - +import Utils class Module(object): def __init__(self, bot, events): @@ -38,6 +38,6 @@ class Module(object): total = sum(results) results = ', '.join(map(str, results)) - event["stdout"].write("Rolled " + str_roll + " for a total " - + "of " + str(total) - + ": [" + results + "]") + event["stdout"].write("Rolled " + Utils.bold(str_roll) + " for a total " + + "of " + Utils.bold(str(total)) + + ": " + results) diff --git a/modules/ducks.py b/modules/ducks.py index fac698f1..e50cc055 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -1,6 +1,6 @@ from operator import itemgetter from threading import Timer -import datetime +import Utils import random @@ -197,9 +197,10 @@ class Module(object): grammar = "" if befriended_ducks == 0 else "s" event["stdout"].write( - target + ", you've befriended " + str( - befriended_ducks + 1) + " duck" + grammar + " in " + event[ - "target"].name) + target + ", you've befriended " + Utils.bold(str( + befriended_ducks + 1)) + " duck" + grammar + " in " + + Utils.bold(event[ + "target"].name)) self.duck_loop_entry(event) @@ -225,9 +226,10 @@ class Module(object): grammar = "" if shot_ducks == 0 else "s" event["stdout"].write( - target + ", you've shot " + str( - shot_ducks + 1) + " duck" + grammar + " in " + event[ - "target"].name) + target + ", you've shot " + + Utils.bold(str(shot_ducks + 1)) + " duck" + + grammar + " in " + + Utils.bold(event["target"].name)) self.duck_loop_entry(event) -- cgit v1.3.1-10-gc9f91