Remove superfluous code from ducks.py and introduce dice.py (DND rolling function .roll 1d20)

This commit is contained in:
dngfx 2018-09-01 10:22:44 +01:00
parent 3d3cb9a598
commit 3f66940e77
2 changed files with 55 additions and 37 deletions

43
modules/dice.py Normal file
View file

@ -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 + "]")

View file

@ -2,22 +2,25 @@ from operator import itemgetter
from threading import Timer
import datetime
import random
import time
import IRCLogging
DUCK_LAST_SEEN = datetime.datetime.now()
class Module(object):
def __init__(self, bot, events):
self.log = IRCLogging.Log
self.bot = bot
self.events = events
self.active_duck = 0
self.decoy_hooked = 0
self.duck_timer = None
self.duck_list = [
"・゜゜・。。・゜ \_O< quack!",
"・゜゜・。。・゜ \_o< QUACK!",
"・゜゜・。 。・゜゜\_ó< quack!",
"・゜゜・。 。・゜゜\_ó< quack quack!",
"・゜゜ 。 。・゜ \_ó< bawk!",
"・゜゜ 。 。・゜゜\_ó< squawk!",
"・ ゜・。 。・゜゜ \_ó< beep beep!"
]
events.on("received.command.bef").hook(self.duck_bef,
help="Befriend a duck!")
events.on("received.command.bang").hook(self.duck_bang,
@ -32,15 +35,7 @@ class Module(object):
# events.on("received.command.ducks").hook(self.duck_list,
# help="Shows a list of the most popular duck superstars.")
now = datetime.datetime.now()
next_duck_time = self.get_random_duck_time()
self.duck_timer = None
self.duck_times = {}
self.decoys = {}
tricky = next_duck_time - now.second
tricky += ((next_duck_time - (now.minute + 1)) * 2)
events.on("postboot").on("configure").on(
"channelset").assure_call(setting="ducks-enabled",
@ -255,17 +250,7 @@ class Module(object):
str) else active_duck
if ducks_enabled == 1 and active_duck == 0:
ducks = [
"・゜゜・。。・゜ \_O< quack!",
"・゜゜・。。・゜ \_o< QUACK!",
"・゜゜・。 。・゜゜\_ó< quack!",
"・゜゜・。 。・゜゜\_ó< quack quack!",
"・゜゜ 。 。・゜ \_ó< bawk!",
"・゜゜ 。 。・゜゜\_ó< squawk!",
"・ ゜・。 。・゜゜ \_ó< beep beep!"
]
channel.send_message(random.choice(ducks))
channel.send_message(random.choice(self.duck_list))
channel.set_setting("active-duck", 1)
@ -276,17 +261,7 @@ class Module(object):
channel.set_setting("active-duck", 0)
def duck_decoy(self, event):
ducks = [
"・゜゜・。。・゜ \_O< quack!",
"・゜゜・。。・゜ \_o< QUACK!",
"・゜゜・。 。・゜゜\_ó< quack!",
"・゜゜・。 。・゜゜\_ó< quack quack!",
"・゜゜ 。 。・゜ \_ó< bawk!",
"・゜゜ 。 。・゜゜\_ó< squawk!",
"・ ゜・。 。・゜゜ \_ó< beep beep!"
]
event["stdout"].write(random.choice(ducks))
event["stdout"].write(random.choice(self.duck_list))
def set_decoy(self, event):
channel = event["target"]