Add command @scheduler remind

This is a safe command for admins to allow as it's limited to text reminders,
unlike the equivalent scheduler.add (+ misc.echo) which usually needs to be
restricted to prevent abuse
This commit is contained in:
mogad0n 2020-10-08 01:12:53 +05:30 committed by GitHub
parent 4213d95356
commit df6ebc78a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 5 deletions

View File

@ -90,7 +90,12 @@ class Scheduler(callbacks.Plugin):
# though we'll never know for sure, because other
# plugins can schedule stuff, too.
n = int(name)
self._add(network, event['msg'], event['time'], event['command'], n)
# Here we use event.get() method instead of event[]
# This is to maintain compatibility with older bots
# lacking 'is_reminder' in their event dicts
is_reminder = event.get('is_reminder', False)
self._add(network, event['msg'], event['time'], event['command'],
is_reminder, n)
elif event['type'] == 'repeat': # repeating event
now = time.time()
first_run = event.get('first_run')
@ -145,11 +150,25 @@ class Scheduler(callbacks.Plugin):
self.Proxy(irc, msg, tokens)
return f
def _add(self, network, msg, t, command, name=None):
def _makeReminderFunction(self, network, msg, text):
"""Makes a function suitable for scheduling text"""
def f():
# If the network isn't available, pick any other one
irc = world.getIrc(network) or world.ircs[0]
replyIrc = callbacks.ReplyIrcProxy(irc, msg)
replyIrc.reply(_('Reminder: %s') % text, msg=msg, prefixNick=True)
del self.events[str(f.eventId)]
return f
def _add(self, network, msg, t, command, is_reminder=False, name=None):
if is_reminder:
f = self._makeReminderFunction(network, msg, command)
else:
f = self._makeCommandFunction(network, msg, command)
id = schedule.addEvent(f, t, name)
f.eventId = id
self.events[str(id)] = {'command':command,
'is_reminder':is_reminder,
'msg':msg,
'network':network,
'time':t,
@ -171,6 +190,19 @@ class Scheduler(callbacks.Plugin):
irc.replySuccess(format(_('Event #%i added.'), id))
add = wrap(add, ['positiveInt', 'text'])
@internationalizeDocstring
def remind(self, irc, msg, args, seconds, text):
""" <seconds> <text>
Sets a reminder with string <text> to run <seconds> seconds in the
future. For example, 'scheduler remind [seconds 30m] "Hello World"'
will return '<nick> Reminder: Hello World' 30 minutes after being set.
"""
t = time.time() + seconds
id = self._add(irc.network, msg, t, text, is_reminder=True)
irc.replySuccess(format(_('Reminder Event #%i added.'), id))
remind = wrap(remind, ['positiveInt', 'text'])
@internationalizeDocstring
def remove(self, irc, msg, args, id):
"""<id>

View File

@ -74,6 +74,21 @@ class SchedulerTestCase(ChannelPluginTestCase):
timeFastForward(5)
self.assertNoResponse(' ', timeout=1)
def testRemind(self):
self.assertNotError('scheduler remind 5 testRemind')
self.assertResponse(
'scheduler list',
'3 (in 4 seconds): "testRemind"')
timeFastForward(3)
self.assertNoResponse(' ', timeout=1)
timeFastForward(3)
self.assertResponse(' ', 'Reminder: testRemind')
timeFastForward(5)
self.assertNoResponse(' ', timeout=1)
self.assertResponse(
'scheduler list', 'There are currently no scheduled commands.')
def testRepeat(self):
self.assertRegexp('scheduler repeat repeater 5 echo testRepeat',
'testRepeat')