Updated seen plugin to pgsql

This commit is contained in:
mrhanky 2017-06-29 23:04:21 +02:00
parent d1911ca4f2
commit 4a0a24e38a
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8

View File

@ -15,31 +15,35 @@ class Seen(DatabasePlugin):
requires = ['irc3.plugins.command',
'nxy.plugins.database']
@command(options_first=True)
def seen(self, mask: IrcString, channel: IrcString, args: DocOptDict):
@command
def seen(self, mask: IrcString, target: IrcString, args: DocOptDict):
"""Get last seen date and message for a nick.
%%seen [<nick>]
"""
nick = args.get('<nick>') or mask.nick
if nick == mask.nick:
return '{nick}, have you seen in the mirror?'.format(nick=nick)
self.cur.execute('select * from seens where nick = ?', [nick])
self.cur.execute('select * from seens where nick = %s', [nick])
seen = self.cur.fetchone()
if not seen:
return 'I\'ve never seen {nick}'.format(nick=nick)
return '{nick} was last seen {delta} saying: {message}'.format(
nick=seen['nick'],
# TODO: relate string delta
delta=datetime.now() - seen['last_seen'],
# TODO: relative string delta
delta=datetime.now() - seen['seen_at'],
message=re.sub(r'\x01ACTION (.*)\x01', r'/me \1', seen['message']),
)
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<channel>\S+) :(?P<msg>.*)')
def save(self, mask: str, channel: str, msg: str):
nick = IrcString(mask).nick
if nick != self.bot.nick:
self.cur.execute('insert into seens (nick, channel, message, '
'last_seen) values (?, ?, ?, ?)',
[nick, channel.lower(), msg, datetime.now()])
self.con.commit()
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)')
def save(self, mask: str, target: str, msg: str):
values = [IrcString(mask).nick, target.lower(), msg, datetime.now()]
self.cur.execute('''insert into seens (nick, channel, message,
seen_at) values (%s, %s, %s, %s) on conflict (nick) do update set
channel = excluded.channel, seen_at = excluded.seen_at,
message = excluded.message''', values)
self.con.commit()