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