2018-09-08 15:15:43 +00:00
|
|
|
import random
|
2019-05-03 14:34:54 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-08-31 03:14:56 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
DUCK = "・゜゜・。。・゜゜\_o< QUACK!"
|
|
|
|
NO_DUCK = "There was no duck!"
|
2018-09-08 15:15:43 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "ducks-enabled",
|
2019-05-03 14:34:54 +00:00
|
|
|
"help": "Whether or not to spawn ducks", "validate": utils.bool_or_none})
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "ducks-min-messages",
|
2019-05-03 14:34:54 +00:00
|
|
|
"help": "Minimum messages between ducks spawning",
|
2018-10-03 12:22:37 +00:00
|
|
|
"validate": utils.int_or_none})
|
2019-05-03 14:34:54 +00:00
|
|
|
@utils.export("channelset", {"setting": "ducks-kick",
|
|
|
|
"help": "Whether or not to kick someone talking to non-existent ducks",
|
|
|
|
"validate": utils.bool_or_none})
|
2018-10-12 17:07:23 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("new.channel")
|
2018-09-08 21:01:47 +00:00
|
|
|
def new_channel(self, event):
|
2019-05-03 14:42:39 +00:00
|
|
|
self.bootstrap_channel(event["channel"])
|
|
|
|
|
|
|
|
def bootstrap_channel(self, channel):
|
|
|
|
if not hasattr(event["channel"], "duck_active"):
|
|
|
|
event["channel"].duck_active = False
|
|
|
|
event["channel"].duck_lines = 0
|
2018-09-09 07:59:36 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
def _activity(self, channel):
|
2019-05-03 14:42:39 +00:00
|
|
|
self.bootstrap_channel(channel)
|
|
|
|
|
2018-09-08 15:15:43 +00:00
|
|
|
ducks_enabled = channel.get_setting("ducks-enabled", False)
|
2019-05-03 14:42:39 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
if ducks_enabled and not channel.duck_active:
|
|
|
|
channel.duck_lines += 1
|
|
|
|
min_lines = channel.get_setting("ducks-min-messages", 20)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
if channel.duck_lines >= min_lines:
|
|
|
|
show_duck = random.SystemRandom().randint(1, 20) == 1
|
2018-08-31 17:23:46 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
if show_duck:
|
|
|
|
self._trigger_duck(channel)
|
2018-08-31 17:23:46 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
@utils.hook("received.join")
|
|
|
|
def join(self, event):
|
|
|
|
self._activity(event["channel"])
|
|
|
|
@utils.hook("received.message.channel")
|
2018-09-08 15:15:43 +00:00
|
|
|
def channel_message(self, event):
|
2019-05-03 14:34:54 +00:00
|
|
|
self._activity(event["channel"])
|
2018-09-08 15:15:43 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
def _trigger_duck(self, channel):
|
|
|
|
channel.duck_active = True
|
|
|
|
channel.send_message(DUCK)
|
2018-08-31 05:27:41 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
def _duck_action(self, channel, user, action, setting):
|
|
|
|
channel.duck_active = False
|
2018-08-31 13:36:25 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
user_id = user.get_id()
|
|
|
|
action_count = channel.get_user_setting(user_id, setting, 0)
|
|
|
|
action_count += 1
|
|
|
|
channel.set_user_setting(user_id, setting, action_count)
|
2018-08-31 04:48:53 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
return "%s %s a duck! You've %s %d ducks in %s!" % (
|
|
|
|
user.nickname, action, action, action_count, channel.name)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
def _no_duck(self, channel, user, stderr, action):
|
|
|
|
if channel.get_setting("ducks-kick"):
|
|
|
|
channel.send_kick(user.nickname, NO_DUCK)
|
2018-09-08 15:15:43 +00:00
|
|
|
else:
|
2019-05-03 14:34:54 +00:00
|
|
|
stderr.write(NO_DUCK)
|
2018-08-31 03:14:56 +00:00
|
|
|
|
2019-05-03 14:34:54 +00:00
|
|
|
@utils.hook("received.command.bef", alias_of="befriend")
|
|
|
|
@utils.hook("received.command.befriend", channel_only=True)
|
2018-09-08 15:15:43 +00:00
|
|
|
def befriend(self, event):
|
2019-05-03 14:34:54 +00:00
|
|
|
if event["target"].duck_active:
|
|
|
|
action = self._duck_action(event["target"], event["user"], "saved",
|
|
|
|
"ducks-befriended")
|
|
|
|
event["stdout"].write(action)
|
|
|
|
else:
|
|
|
|
self._no_duck(event["target"], event["user"], event["stderr"],
|
|
|
|
"befriend")
|
|
|
|
|
|
|
|
@utils.hook("received.command.bang", channel_only=True)
|
|
|
|
def bang(self, event):
|
|
|
|
if event["target"].duck_active:
|
|
|
|
action = self._duck_action(event["target"], event["user"], "shot",
|
|
|
|
"ducks-shot")
|
|
|
|
event["stdout"].write(action)
|
|
|
|
else:
|
|
|
|
self._no_duck(event["target"], event["user"], event["stderr"],
|
|
|
|
"shoot")
|
2018-09-08 15:15:43 +00:00
|
|
|
|