Optimized futures code

This commit is contained in:
mrhanky 2017-05-16 18:58:27 +02:00
parent 459651de2f
commit b0f36a1924
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8

View File

@ -23,20 +23,34 @@ class Futures(DatabasePlugin):
super().__init__(bot)
self.tell_queue = {}
# Restore timers from database
self.cur.execute('select * from timers')
self.cur.execute('select id, mask, channel, message, delay, until '
'from timers')
for res in self.cur.fetchall():
delta = res['until'] - datetime.utcnow()
args = (IrcString(res['mask']), res['channel'], delta,
res['message'], res['delay'], res['id'])
asyncio.ensure_future(self._timer(*args))
# Restore tells from database
self.cur.execute('select * from tells')
self.cur.execute('select mask, user, message from tells')
for res in self.cur.fetchall():
user = res['user']
if user not in self.tell_queue:
self.tell_queue[user] = []
self.tell_queue[user].append(res)
@event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
def tell_check(self, mask: str):
"""If activ user has tells, forward and delete them."""
nick = IrcString(mask).nick
if nick in self.tell_queue:
for tell in self.tell_queue[nick]:
self.bot.privmsg(nick, '[Tell] Message from {nick}: {message}'
.format(nick=IrcString(tell['mask']).nick,
message=tell['message']))
del self.tell_queue[nick]
self.cur.execute('delete from tells where user = ?', [nick])
self.con.commit()
@command
def timer(self, mask: IrcString, channel: IrcString, args: DocOptDict):
"""Sets a timer, delay can be: s, m, h, w, mon, y(
@ -70,15 +84,14 @@ class Futures(DatabasePlugin):
message = ' '.join(args['<message>'])
if nick not in self.tell_queue:
self.tell_queue[nick] = []
self.cur.execute('insert into tells (mask, user, message) values '
'(?, ?, ?)', [mask, nick, message])
self.con.commit()
self.tell_queue[nick].append({
'id': self.cur.lastrowid,
'mask': mask,
'user': nick,
'message': message,
})
self.cur.execute('insert into tells (mask, user, message) values '
'(?, ?, ?)', [mask, nick, message])
self.con.commit()
async def _timer(self, mask: IrcString, channel: IrcString,
delta: timedelta, message: str, delay: str, row_id: int):
@ -91,18 +104,3 @@ class Futures(DatabasePlugin):
self.bot.privmsg(channel, text)
self.cur.execute('delete from timers where id = ?', [row_id])
self.con.commit()
@event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
def tell_check(self, mask: str):
nick = IrcString(mask).nick
if nick in self.tell_queue:
ids = []
for tell in self.tell_queue[nick]:
ids.append(tell['id'])
self.bot.privmsg(nick, '[Tell] Message from {nick}: {message}'
.format(nick=IrcString(tell['mask']).nick,
message=tell['message']))
del self.tell_queue[nick]
self.cur.execute('delete from tells where id in ({})'
.format(', '.join('?' * len(ids))), ids)
self.con.commit()