Selaa lähdekoodia

Bugfixes and a !remind help option.

tags/v0.2
Ben Kurtovic 10 vuotta sitten
vanhempi
commit
801d8a3c2d
2 muutettua tiedostoa jossa 37 lisäystä ja 17 poistoa
  1. +34
    -13
      earwigbot/commands/remind.py
  2. +3
    -4
      earwigbot/commands/threads.py

+ 34
- 13
earwigbot/commands/remind.py Näytä tiedosto

@@ -29,9 +29,9 @@ import time

from earwigbot.commands import Command

DISPLAY = ["display", "show", "list", "info"]
CANCEL = ["cancel", "stop", "delete", "del"]
SNOOZE = ["snooze", "delay", "reset", "adjust"]
DISPLAY = ["display", "show", "list", "info", "details"]
CANCEL = ["cancel", "stop", "delete", "del", "stop"]
SNOOZE = ["snooze", "delay", "reset", "adjust", "modify", "change"]

class Remind(Command):
"""Set a message to be repeated to you in a certain amount of time."""
@@ -120,11 +120,11 @@ class Remind(Command):
else:
self.reminders[user] = [reminder]
msg = "Set reminder \x0303{0}\x0F ({1})."
self.reply(data, msg.format(rid, wait, reminder.end_time))
self.reply(data, msg.format(rid, reminder.end_time))

def _display_reminder(self, data, reminder):
"""Display a particular reminder's information."""
msg = 'Reminder \x0303{0}\x0F: {1} seconds ({2}): "{3}". !remind [cancel|snooze] {0}.'
msg = 'Reminder \x0303{0}\x0F: {1} seconds ({2}): "{3}".'
msg = msg.format(reminder.id, reminder.wait, reminder.end_time,
reminder.message)
self.reply(data, msg)
@@ -140,6 +140,7 @@ class Remind(Command):

def _snooze_reminder(self, data, reminder, arg=None):
"""Snooze a reminder to be re-triggered after a period of time."""
verb = "snoozed" if reminder.end < time.time() else "adjusted"
if arg:
try:
duration = self._parse_time(data.args[arg])
@@ -147,9 +148,9 @@ class Remind(Command):
except (IndexError, ValueError):
pass
reminder.start()
end_time = time.strftime("%b %d %H:%M:%S %Z", reminder.end)
msg = "Reminder \x0303{0}\x0F snoozed until {1}."
self.reply(data, msg.format(reminder.id, end_time))
end = time.strftime("%b %d %H:%M:%S %Z", time.localtime(reminder.end))
msg = "Reminder \x0303{0}\x0F {1} until {2}."
self.reply(data, msg.format(reminder.id, verb, end))

def _handle_command(self, command, data, user, reminder, arg=None):
"""Handle a reminder-processing subcommand."""
@@ -172,9 +173,9 @@ class Remind(Command):

if user in self.reminders:
rlist = ", ".join(fmt(robj) for robj in self.reminders[user])
msg = "Your reminders: {1}.".format(rlist)
msg = "Your reminders: {0}.".format(rlist)
else:
msg = "You have no reminders. Set one with !remind <time> <msg>."
msg = "You have no reminders. Set one with \x0306!remind [time] [message]\x0F. See also: \x0306!remind help\x0F."
self.reply(data, msg)

def _process_snooze_command(self, data, user):
@@ -192,6 +193,20 @@ class Remind(Command):
if reminder:
self._snooze_reminder(data, reminder, 1)

def _show_help(self, data):
"""Reply to the user with help for all major subcommands."""
parts = [
("Add new", "!remind [time] [message]"),
("List all", "!reminders"),
("Get info", "!remind [id]"),
("Cancel", "!remind cancel [id]"),
("Adjust", "!remind adjust [id] [time]"),
("Restart", "!snooze [id]")
]
extra = "In all cases, \x0306[id]\x0F can be omitted if you have only one reminder."
joined = " ".join("{0}: \x0306{1}\x0F.".format(k, v) for k, v in parts)
self.reply(data, joined + " " + extra)

def setup(self):
self.reminders = {}

@@ -204,6 +219,8 @@ class Remind(Command):
user = data.host
if len(data.args) == 1:
command = data.args[0]
if command == "help":
return self._show_help(data)
if command in DISPLAY + CANCEL + SNOOZE:
if user not in self.reminders:
msg = "You have no reminders to {0}."
@@ -254,10 +271,14 @@ class _Reminder(object):
"""Internal callback function to be executed by the reminder thread."""
thread = self._thread
while time.time() < thread.end:
time.sleep(1)
if thread.abort:
return
self._cmdobj.reply(self._data, self.message)
for i in xrange(60):
time.sleep(1)
time.sleep(60)
if thread.abort:
return
try:
self._cmdobj.reminders[self._user].remove(self)
if not self._cmdobj.reminders[self._user]:
@@ -269,8 +290,8 @@ class _Reminder(object):
def end_time(self):
"""Return a string representing the end time of a reminder."""
if self.end >= time.time():
end_time = time.strftime("%b %d %H:%M:%S %Z", self.end)
return "ends {0}".format(end_time)
ends = time.strftime("%b %d %H:%M:%S %Z", time.localtime(self.end))
return "ends {0}".format(ends)
return "expired"

def start(self):


+ 3
- 4
earwigbot/commands/threads.py Näytä tiedosto

@@ -75,10 +75,9 @@ class Threads(Command):
elif tname in self.config.components:
t = "\x0302{0}\x0F (id {1})"
normal_threads.append(t.format(tname, thread.ident % 10000))
elif tname.startswith("reminder"):
tname = tname.replace("reminder ", "")
t = "\x0302reminder\x0F (until {0})"
normal_threads.append(t.format(tname))
elif tname.startswith("remind-"):
t = "\x0302reminder\x0F (id {0})"
daemon_threads.append(t.format(tname[len("remind-"):]))
else:
tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0]
t = "\x0302{0}\x0F (id {1}, since {2})"


Ladataan…
Peruuta
Tallenna