2018-08-31 15:58:25 +00:00
|
|
|
|
from operator import itemgetter
|
2018-09-01 00:56:07 +00:00
|
|
|
|
from threading import Timer
|
2018-09-01 09:52:04 +00:00
|
|
|
|
import Utils
|
2018-08-31 03:35:42 +00:00
|
|
|
|
import random
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-09-01 10:40:08 +00:00
|
|
|
|
DUCK_LIST = [
|
|
|
|
|
"・゜゜・。。・゜ ゜\_O< quack!",
|
|
|
|
|
"・゜゜・。。・゜ ゜\_o< QUACK!",
|
|
|
|
|
"・゜゜・。 。・゜゜\_ó< quack!",
|
|
|
|
|
"・゜゜・。 。・゜゜\_ó< quack quack!",
|
|
|
|
|
"・゜゜ 。 。・゜ \_ó< bawk!",
|
|
|
|
|
"・゜゜ 。 。・゜゜\_ó< squawk!",
|
|
|
|
|
"・ ゜・。 。・゜゜ \_ó< beep beep!"
|
|
|
|
|
]
|
2018-08-31 03:35:42 +00:00
|
|
|
|
|
2018-08-31 03:14:56 +00:00
|
|
|
|
class Module(object):
|
2018-08-31 11:55:52 +00:00
|
|
|
|
def __init__(self, bot, events):
|
2018-08-31 03:14:56 +00:00
|
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
|
self.events = events
|
2018-08-31 13:10:15 +00:00
|
|
|
|
self.decoy_hooked = 0
|
2018-09-01 00:56:07 +00:00
|
|
|
|
self.duck_timer = None
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("received.command.bef").hook(self.duck_bef,
|
2018-08-31 15:06:11 +00:00
|
|
|
|
help="Befriend a duck!")
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("received.command.bang").hook(self.duck_bang,
|
2018-08-31 15:06:11 +00:00
|
|
|
|
help="Shoot a duck! Meanie.")
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("received.command.decoy").hook(self.set_decoy,
|
2018-08-31 15:06:11 +00:00
|
|
|
|
help="Be a sneaky fellow and make a decoy duck.")
|
2018-08-31 05:27:41 +00:00
|
|
|
|
|
2018-08-31 13:36:25 +00:00
|
|
|
|
events.on("received.command.friends").hook(self.duck_friends,
|
2018-08-31 15:06:11 +00:00
|
|
|
|
help="See who the friendliest people to ducks are!")
|
2018-08-31 17:23:46 +00:00
|
|
|
|
events.on("received.command.killers").hook(self.duck_enemies,
|
|
|
|
|
help="See who shoots the most amount of ducks.")
|
2018-08-31 11:55:52 +00:00
|
|
|
|
# events.on("received.command.ducks").hook(self.duck_list,
|
2018-08-31 03:35:42 +00:00
|
|
|
|
# help="Shows a list of the most popular duck superstars.")
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
self.duck_times = {}
|
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("postboot").on("configure").on(
|
2018-08-31 03:14:56 +00:00
|
|
|
|
"channelset").assure_call(setting="ducks-enabled",
|
2018-09-01 17:45:01 +00:00
|
|
|
|
help="Toggles ducks!",
|
|
|
|
|
validate=Utils.bool_or_none)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("postboot").on("configure").on(
|
2018-08-31 03:14:56 +00:00
|
|
|
|
"channelset").assure_call(setting="min-duck-time",
|
2018-09-01 17:45:01 +00:00
|
|
|
|
help="Minimum seconds before a duck is "
|
|
|
|
|
"summoned",
|
|
|
|
|
validate=Utils.int_or_none)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("postboot").on("configure").on(
|
2018-08-31 03:14:56 +00:00
|
|
|
|
"channelset").assure_call(setting="max-duck-time",
|
2018-09-01 17:45:01 +00:00
|
|
|
|
help="Max seconds before a duck is "
|
|
|
|
|
"summoned",
|
|
|
|
|
validate=Utils.int_or_none)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
|
events.on("received.numeric.366").hook(self.bootstrap)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-09-01 00:56:07 +00:00
|
|
|
|
events.on("raw").on("376").hook(self.duck_loop_entry)
|
|
|
|
|
|
2018-09-01 11:24:00 +00:00
|
|
|
|
events.on("timer").on("duck-decoy").hook(self.duck_decoy)
|
|
|
|
|
events.on("timer").on("show-duck").hook(self.show_duck)
|
|
|
|
|
|
2018-09-01 00:56:07 +00:00
|
|
|
|
def duck_loop_entry(self, event):
|
|
|
|
|
wait = self.get_random_duck_time()
|
2018-09-01 08:09:02 +00:00
|
|
|
|
self.bot.log.info("Sending out a wave of ducks in %s seconds",
|
|
|
|
|
[wait])
|
2018-09-01 11:24:00 +00:00
|
|
|
|
self.bot.add_timer("show-duck", wait)
|
2018-09-01 00:56:07 +00:00
|
|
|
|
|
2018-08-31 03:14:56 +00:00
|
|
|
|
def bootstrap(self, event):
|
|
|
|
|
for server in self.bot.servers.values():
|
|
|
|
|
for channel in server.channels.values():
|
2018-09-01 17:45:01 +00:00
|
|
|
|
ducks_enabled = channel.get_setting("ducks-enabled", False)
|
2018-08-31 04:48:53 +00:00
|
|
|
|
|
2018-08-31 03:14:56 +00:00
|
|
|
|
min_time = "min-duck-time-%s" % channel.name
|
|
|
|
|
max_time = "max-duck-time-%s" % channel.name
|
|
|
|
|
|
2018-09-01 00:56:07 +00:00
|
|
|
|
min_duck_time = channel.get_setting("min-duck-time", 20)
|
|
|
|
|
max_duck_time = channel.get_setting("max-duck-time", 30)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-08-31 15:06:11 +00:00
|
|
|
|
min_duck_time = int(min_duck_time) if isinstance(min_duck_time,
|
|
|
|
|
str) else min_duck_time
|
|
|
|
|
max_duck_time = int(max_duck_time) if isinstance(max_duck_time,
|
|
|
|
|
str) else max_duck_time
|
2018-08-31 04:48:53 +00:00
|
|
|
|
|
2018-08-31 03:14:56 +00:00
|
|
|
|
self.duck_times[min_time] = min_duck_time
|
|
|
|
|
self.duck_times[max_time] = max_duck_time
|
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
if ducks_enabled == True:
|
|
|
|
|
channel.set_setting("active-duck", False)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
def duck_time(self, channel):
|
|
|
|
|
if isinstance(channel, str):
|
|
|
|
|
channel_name = channel
|
|
|
|
|
else:
|
|
|
|
|
channel_name = channel["target"].name
|
|
|
|
|
|
|
|
|
|
min = "min-duck-time-%s" % (channel_name)
|
|
|
|
|
max = "max-duck-time-%s" % (channel_name)
|
|
|
|
|
|
2018-08-31 15:06:11 +00:00
|
|
|
|
self.bot.log.debug("Attempting to set %s to %s",
|
2018-09-01 08:09:02 +00:00
|
|
|
|
[str(min), str(self.duck_times[min])])
|
2018-08-31 15:06:11 +00:00
|
|
|
|
self.bot.log.debug("Attempting to set %s to %s",
|
2018-09-01 08:09:02 +00:00
|
|
|
|
[str(max), str(self.duck_times[max])])
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
return random.randint(self.duck_times[min], self.duck_times[max])
|
|
|
|
|
|
2018-08-31 05:27:41 +00:00
|
|
|
|
def decoy_time(self):
|
2018-08-31 17:31:46 +00:00
|
|
|
|
return random.randint(60, 180)
|
2018-08-31 09:51:47 +00:00
|
|
|
|
|
2018-08-31 17:23:46 +00:00
|
|
|
|
def duck_enemies(self, event):
|
|
|
|
|
the_enemy = event["server"].find_all_user_channel_settings(
|
|
|
|
|
"ducks-shot")
|
|
|
|
|
|
|
|
|
|
notorious = {}
|
|
|
|
|
enemy_nicks = []
|
|
|
|
|
enemy_ducks = []
|
|
|
|
|
|
|
|
|
|
for i in the_enemy:
|
|
|
|
|
if i[1] in notorious.keys():
|
|
|
|
|
notorious[i[1]] += i[2]
|
|
|
|
|
else:
|
|
|
|
|
notorious[i[1]] = i[2]
|
|
|
|
|
|
|
|
|
|
for user, enemies in sorted(notorious.items(), key=itemgetter(1),
|
|
|
|
|
reverse=True):
|
|
|
|
|
enemy_nicks.append(user)
|
|
|
|
|
enemy_ducks.append(enemies)
|
|
|
|
|
|
|
|
|
|
sentence = "Most Notorious Users -- "
|
|
|
|
|
|
|
|
|
|
length = len(enemy_nicks) if len(enemy_nicks) < 11 else 11
|
|
|
|
|
|
|
|
|
|
for i in range(0, length):
|
|
|
|
|
sentence += enemy_nicks[i] + " (" + str(enemy_ducks[i]) + ")"
|
|
|
|
|
if i < 10:
|
|
|
|
|
sentence += ", "
|
|
|
|
|
|
|
|
|
|
sentence = sentence[0:-2]
|
|
|
|
|
|
|
|
|
|
event["stdout"].write(sentence)
|
|
|
|
|
|
2018-08-31 13:36:25 +00:00
|
|
|
|
def duck_friends(self, event):
|
2018-08-31 15:06:11 +00:00
|
|
|
|
friends = event["server"].find_all_user_channel_settings(
|
|
|
|
|
"ducks-befriended")
|
|
|
|
|
|
2018-08-31 15:58:25 +00:00
|
|
|
|
friendliest = {}
|
|
|
|
|
friend_nicks = []
|
|
|
|
|
friend_ducks = []
|
|
|
|
|
|
|
|
|
|
for i in friends:
|
|
|
|
|
if i[1] in friendliest.keys():
|
|
|
|
|
friendliest[i[1]] += i[2]
|
|
|
|
|
else:
|
|
|
|
|
friendliest[i[1]] = i[2]
|
|
|
|
|
|
|
|
|
|
for user, friends in sorted(friendliest.items(), key = itemgetter(1),
|
|
|
|
|
reverse = True):
|
|
|
|
|
friend_nicks.append(user)
|
|
|
|
|
friend_ducks.append(friends)
|
|
|
|
|
|
|
|
|
|
sentence = "Friendliest Users -- "
|
|
|
|
|
|
|
|
|
|
length = len(friend_nicks) if len(friend_nicks) < 11 else 11
|
|
|
|
|
|
|
|
|
|
for i in range(0, length):
|
|
|
|
|
sentence += friend_nicks[i] + " (" + str(friend_ducks[i]) + ")"
|
|
|
|
|
if i < 10:
|
|
|
|
|
sentence += ", "
|
|
|
|
|
|
|
|
|
|
sentence = sentence[0:-2]
|
|
|
|
|
|
2018-08-31 16:02:46 +00:00
|
|
|
|
event["stdout"].write(sentence)
|
2018-08-31 05:27:41 +00:00
|
|
|
|
|
2018-08-31 03:14:56 +00:00
|
|
|
|
def duck_bef(self, event):
|
2018-08-31 13:36:25 +00:00
|
|
|
|
user = event["user"]
|
|
|
|
|
target = user.nickname
|
|
|
|
|
id = user.id
|
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
active_duck = event["target"].get_setting("active-duck", False)
|
2018-08-31 04:48:53 +00:00
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
if active_duck == False:
|
2018-08-31 03:14:56 +00:00
|
|
|
|
if event["server"].has_user(target):
|
|
|
|
|
if not event["server"].is_own_nickname(target):
|
2018-08-31 15:06:11 +00:00
|
|
|
|
event["target"].send_kick(target,
|
|
|
|
|
"You tried befriending a non-existent duck. Creepy!")
|
2018-08-31 03:14:56 +00:00
|
|
|
|
else:
|
|
|
|
|
event["stderr"].write("Nope.")
|
|
|
|
|
else:
|
|
|
|
|
event["stderr"].write("That user is not in this channel")
|
|
|
|
|
else:
|
2018-08-31 13:36:25 +00:00
|
|
|
|
befriended_ducks = event["target"].get_user_setting(id,
|
2018-08-31 15:06:11 +00:00
|
|
|
|
"ducks-befriended",
|
|
|
|
|
0)
|
2018-08-31 13:36:25 +00:00
|
|
|
|
event["target"].set_user_setting(id,
|
2018-08-31 15:06:11 +00:00
|
|
|
|
"ducks-befriended",
|
|
|
|
|
befriended_ducks + 1)
|
2018-09-01 17:45:01 +00:00
|
|
|
|
event["target"].set_setting("active-duck", False)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
grammar = "" if befriended_ducks == 0 else "s"
|
|
|
|
|
|
2018-08-31 03:35:42 +00:00
|
|
|
|
event["stdout"].write(
|
2018-09-01 09:52:04 +00:00
|
|
|
|
target + ", you've befriended " + Utils.bold(str(
|
|
|
|
|
befriended_ducks + 1)) + " duck" + grammar + " in " +
|
|
|
|
|
Utils.bold(event[
|
|
|
|
|
"target"].name))
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-09-01 00:56:07 +00:00
|
|
|
|
self.duck_loop_entry(event)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
def duck_bang(self, event):
|
2018-08-31 13:36:25 +00:00
|
|
|
|
user = event["user"]
|
|
|
|
|
target = user.nickname
|
|
|
|
|
id = user.id
|
2018-09-01 17:45:01 +00:00
|
|
|
|
if not event["target"].get_setting("active-duck", False):
|
2018-08-31 03:14:56 +00:00
|
|
|
|
event["stderr"].set_prefix("Kick")
|
|
|
|
|
if event["server"].has_user(target):
|
|
|
|
|
if not event["server"].is_own_nickname(target):
|
2018-08-31 15:06:11 +00:00
|
|
|
|
event["target"].send_kick(target,
|
|
|
|
|
"You tried shooting a non-existent duck. Creepy!")
|
2018-08-31 03:14:56 +00:00
|
|
|
|
else:
|
|
|
|
|
event["stderr"].write("Nope.")
|
|
|
|
|
else:
|
|
|
|
|
event["stderr"].write("That user is not in this channel")
|
|
|
|
|
else:
|
2018-08-31 13:36:25 +00:00
|
|
|
|
shot_ducks = event["target"].get_user_setting(id, "ducks-shot", 0)
|
|
|
|
|
event["target"].set_user_setting(id, "ducks-shot", shot_ducks + 1)
|
2018-09-01 17:45:01 +00:00
|
|
|
|
event["target"].set_setting("active-duck", False)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
grammar = "" if shot_ducks == 0 else "s"
|
|
|
|
|
|
2018-08-31 03:35:42 +00:00
|
|
|
|
event["stdout"].write(
|
2018-09-01 09:52:04 +00:00
|
|
|
|
target + ", you've shot "
|
|
|
|
|
+ Utils.bold(str(shot_ducks + 1)) + " duck"
|
|
|
|
|
+ grammar + " in "
|
|
|
|
|
+ Utils.bold(event["target"].name))
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-09-01 00:56:07 +00:00
|
|
|
|
self.duck_loop_entry(event)
|
|
|
|
|
|
|
|
|
|
def get_random_duck_time(self):
|
|
|
|
|
return random.randint(120, 1200)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
|
|
|
|
def show_duck(self, event):
|
|
|
|
|
for server in self.bot.servers.values():
|
|
|
|
|
for channel in server.channels.values():
|
2018-09-01 17:45:01 +00:00
|
|
|
|
ducks_enabled = channel.get_setting("ducks-enabled", False)
|
|
|
|
|
|
|
|
|
|
if ducks_enabled == False:
|
2018-08-31 03:35:42 +00:00
|
|
|
|
continue
|
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
active_duck = channel.get_setting("active-duck", False)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
if ducks_enabled == True and active_duck == False:
|
2018-09-01 10:40:08 +00:00
|
|
|
|
channel.send_message(random.choice(DUCK_LIST))
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
channel.set_setting("active-duck", True)
|
2018-08-31 03:35:42 +00:00
|
|
|
|
|
2018-09-01 17:45:01 +00:00
|
|
|
|
elif ducks_enabled == True and active_duck == True:
|
2018-08-31 03:35:42 +00:00
|
|
|
|
pass
|
|
|
|
|
|
2018-08-31 03:14:56 +00:00
|
|
|
|
else:
|
2018-09-01 17:45:01 +00:00
|
|
|
|
channel.set_setting("active-duck", False)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
|
2018-08-31 05:27:41 +00:00
|
|
|
|
def duck_decoy(self, event):
|
2018-09-01 10:40:08 +00:00
|
|
|
|
event["stdout"].write(random.choice(DUCK_LIST))
|
2018-08-31 05:27:41 +00:00
|
|
|
|
|
|
|
|
|
def set_decoy(self, event):
|
2018-08-31 13:00:09 +00:00
|
|
|
|
channel = event["target"]
|
2018-08-31 12:58:44 +00:00
|
|
|
|
|
2018-08-31 05:27:41 +00:00
|
|
|
|
next_decoy_time = self.decoy_time()
|
2018-08-31 13:10:15 +00:00
|
|
|
|
|
2018-08-31 12:58:44 +00:00
|
|
|
|
self.bot.add_timer("duck-decoy", next_decoy_time, None, None, False,
|
|
|
|
|
channel=channel)
|