Added persistent tell command

This commit is contained in:
mrhanky 2017-05-16 18:34:29 +02:00
parent 84a03573b0
commit 4414a3ade7
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8
2 changed files with 46 additions and 1 deletions

View File

@ -5,7 +5,7 @@ import asyncio
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from irc3 import IrcBot
from irc3 import IrcBot, event
import irc3
from . import DatabasePlugin
@ -21,6 +21,7 @@ class Futures(DatabasePlugin):
def __init__(self, bot: IrcBot):
super().__init__(bot)
self.tell_queue = {}
# Restore timers from database
self.cur.execute('select * from timers')
for res in self.cur.fetchall():
@ -51,6 +52,27 @@ class Futures(DatabasePlugin):
else:
self.bot.privmsg(channel, 'Invalid timer delay')
# noinspection PyUnusedLocal
@command
def tell(self, mask: IrcString, channel: IrcString, args: DocOptDict):
"""Saves a message for nick to forward on activity
%%tell <nick> <message>...
"""
nick = args['<nick>']
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,
})
async def _timer(self, mask: IrcString, channel: IrcString,
delta: timedelta, message: str, delay: str, row_id: int):
"""Async function, sleeps for `delay` seconds and sends notification"""
@ -62,3 +84,18 @@ 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()

View File

@ -20,3 +20,11 @@ create table if not exists timers (
until timestamp not null,
created timestamp not null default current_timestamp
);
create table if not exists tells (
id integer primary key autoincrement,
mask text not null,
user text not null,
message text not null,
created timestamp not null default current_timestamp
);