bitbot-3.11-fork/modules/in.py
jesopo dc102f258d Add Database.servers.get_by_alias, move IRCBot.get_server to
IRCBot.get_server_by_id, add IRCBot.get_server_by_alias and change
!connect/!disconnect to take aliases instead of IDs
2019-04-24 12:07:30 +01:00

73 lines
2.7 KiB
Python

import time
from src import ModuleManager, utils
SECONDS_MAX = utils.SECONDS_WEEKS*8
SECONDS_MAX_DESCRIPTION = "8 weeks"
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.in", min_args=2)
def in_command(self, event):
"""
:help: Set a reminder
:usage: <time> <message>
"""
seconds = utils.from_pretty_time(event["args_split"][0])
message = " ".join(event["args_split"][1:])
if seconds:
if seconds <= SECONDS_MAX:
due_time = int(time.time())+seconds
self.timers.add_persistent("in", seconds, due_time=due_time,
target=event["target"].name, server_id=event["server"].id,
nickname=event["user"].nickname, message=message)
event["stdout"].write("Saved")
else:
event["stderr"].write(
"The given time is above the max (%s)" % (
SECONDS_MAX_DESCRIPTION))
else:
event["stderr"].write(
"Please provided a valid time above 0 seconds")
@utils.hook("timer.in")
def timer_due(self, event):
server = self.bot.get_server_by_id(event["server_id"])
if server:
message = "%s: this is your reminder: %s" % (
event["nickname"], event["message"])
target = server.get_target(event["target"])
self.events.on("send.stdout").call(target=target, module_name="In",
server=server, message=message)
@utils.hook("received.command.inlist")
def in_list(self, event):
"""
:help: List reminders
:usage: [index]
"""
timers = self.timers.find_all("in")
found = []
for timer in timers:
nickname_match = (event["server"].irc_lower(
timer.kwargs["nickname"]) == event["user"].nickname_lower)
target_match = timer.kwargs["target"] == event["target"].name
if nickname_match and target_match:
found.append(timer)
if len(event["args_split"]) > 0:
index = event["args_split"][0]
if not index.isdigit() or index == "0":
raise utils.EventError("Please provide a valid reminder index")
index = int(index)
actual_index = index-1
if actual_index > len(found):
raise utils.EventError("You do not have that many reminders")
timer = found[actual_index]
event["stdout"].write("Reminder %d: %s" % (index,
timer.kwargs["message"]))
else:
event["stdout"].write("%s: you have %d reminders" % (
event["user"].nickname, len(found)))