Cleaned up tell module

This commit is contained in:
mrhanky 2017-08-22 11:58:32 +02:00
parent 387dd2cef5
commit 4fdcad950c
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8

View File

@ -19,19 +19,16 @@ class Tell(DatabasePlugin):
super().__init__(bot)
self.tell_queue = {}
# Fetch tells from database
self.cur.execute('''
select
SELECT
to_nick, from_nick, message, created_at
from
FROM
tells
''')
# Add tells to queue
for res in self.cur.fetchall():
nick = res['to_nick'].lower()
# Create list in queue and add tell
if nick not in self.tell_queue:
self.tell_queue[nick] = []
self.tell_queue[nick].append(res[1:])
@ -42,57 +39,44 @@ class Tell(DatabasePlugin):
%%tell <nick> <message>...
"""
nick = args['<nick>']
nick_lower = nick.lower()
tell = [nick, mask.nick, ' '.join(args['<message>']).strip(),
datetime.now()]
nick = args['<nick>'].lower()
tell = [nick, mask.nick, ' '.join(args['<message>']).strip(), datetime.utcnow()]
# Create list in queue and add tell
if nick_lower not in self.tell_queue:
self.tell_queue[nick_lower] = []
self.tell_queue[nick_lower].append(tell[1:])
if nick not in self.tell_queue:
self.tell_queue[nick] = []
self.tell_queue[nick].append(tell[1:])
try:
# Insert tell into database
self.cur.execute('''
insert into
INSERT INTO
tells (to_nick, from_nick, message, created_at)
values
VALUES
(%s, %s, %s, %s)
''', tell)
self.con.commit()
except Error:
# Rollback transaction on error
except Error as ex:
self.log.error(ex)
self.con.rollback()
@irc3.event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
def check(self, mask: str):
def on_message(self, mask: str):
"""If activ nick has tells, forward and delete them."""
nick = IrcString(mask).nick
nick_lower = nick.lower()
nick = IrcString(mask).nick.lower()
if nick_lower in self.tell_queue:
if nick in self.tell_queue:
# Forward all tells for nick
for tell in self.tell_queue[nick_lower]:
# TODO: format time
self.bot.privmsg(nick, '[Tell] Message from {nick} at {time}: '
'{message}'.format(nick=tell[0],
time=tell[2],
message=tell[1]))
for tell in self.tell_queue[nick]:
self.bot.privmsg(nick, '[Tell] Message from {nick} at {time}: {message}'.format(
nick=tell[0],
time=tell[1], # TODO: format time
message=tell[2],
))
# Remove nick from queue
del self.tell_queue[nick_lower]
try:
# Remove tells from database
self.cur.execute('''
delete from
tells
where
lower(to_nick) = lower(%s)
''', [nick])
self.con.commit()
except Error as ex:
print(ex)
# Rollback transaction on error
self.con.rollback()
del self.tell_queue[nick]
self.cur.execute('''
DELETE FROM
tells
WHERE
to_nick = %s
''', [nick])
self.con.commit()