Formatting of timer plugin
This commit is contained in:
parent
9d8405092e
commit
8dce06b793
|
@ -25,6 +25,36 @@ class Timer(DatabasePlugin):
|
|||
self.set_timers()
|
||||
crontab('0 * * * *', func=self.set_timers)
|
||||
|
||||
@command
|
||||
def timer(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
||||
"""Sets a timer, delay can be: s, m, h, d, w, mon, y
|
||||
|
||||
%%timer <delay> <message>...
|
||||
"""
|
||||
delay = args['<delay>']
|
||||
message = ' '.join(args['<message>'])
|
||||
|
||||
if not re.match(r'\d+[smhdwy]|mon', delay):
|
||||
return 'Invalid timer delay: {}'.format(delay)
|
||||
|
||||
try:
|
||||
self.cur.execute('''
|
||||
INSERT INTO
|
||||
timers (mask, target, message, delay, ends_at)
|
||||
VALUES
|
||||
(%s, %s, %s, %s, now() + INTERVAL %s)
|
||||
RETURNING
|
||||
*
|
||||
''', [mask, target, message, delay, delay])
|
||||
self.con.commit()
|
||||
|
||||
asyncio.ensure_future(self.exec_timer(self.cur.fetchone()))
|
||||
|
||||
self.bot.notice(mask.nick, 'Timer in {delay} set: {message}'.format(delay=delay, message=message))
|
||||
except Error as ex:
|
||||
self.log.error(ex)
|
||||
self.con.rollback()
|
||||
|
||||
def set_timers(self):
|
||||
"""Function which queries all timers in the next hour and schedules them."""
|
||||
self.log.debug('Fetching timers')
|
||||
|
@ -41,37 +71,6 @@ class Timer(DatabasePlugin):
|
|||
for timer in self.cur.fetchall():
|
||||
asyncio.ensure_future(self.exec_timer(timer))
|
||||
|
||||
@command
|
||||
def timer(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
||||
"""Sets a timer, delay can be: s, m, h, d, w, mon, y
|
||||
|
||||
%%timer <delay> <message>...
|
||||
"""
|
||||
delay = args['<delay>']
|
||||
message = ' '.join(args['<message>'])
|
||||
|
||||
if not re.match(r'\d+[smhdwy]|mon', delay):
|
||||
return 'Invalid timer delay: {}'.format(delay)
|
||||
|
||||
values = [mask, target, message, delay]
|
||||
|
||||
try:
|
||||
self.cur.execute('''
|
||||
INSERT INTO
|
||||
timers (mask, target, message, delay, ends_at)
|
||||
VALUES
|
||||
(%s, %s, %s, %s, now() + INTERVAL %s)
|
||||
RETURNING *
|
||||
''', values + [delay])
|
||||
self.con.commit()
|
||||
|
||||
asyncio.ensure_future(self.exec_timer(self.cur.fetchone()))
|
||||
|
||||
self.bot.notice(mask.nick, 'Timer in {delay} set: {message}'.format(delay=delay, message=message))
|
||||
except Error as ex:
|
||||
self.log.error(ex)
|
||||
self.con.rollback()
|
||||
|
||||
async def exec_timer(self, timer: DictRow):
|
||||
"""Sets the actual timer (sleeps until it fires), sends the reminder and deletes the timer from database."""
|
||||
if timer['id'] in self.timers:
|
||||
|
|
Loading…
Reference in New Issue
Block a user