nxy/bot/seen.py

64 lines
1.7 KiB
Python
Raw Normal View History

2017-05-29 12:39:48 +00:00
# -*- coding: utf-8 -*-
import re
2017-06-30 12:04:50 +00:00
import irc3
2017-08-22 13:57:57 +00:00
from docopt import Dict
2017-05-29 12:39:48 +00:00
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import DatabasePlugin
class Seen(DatabasePlugin):
2017-06-29 21:04:21 +00:00
@command
2017-08-22 13:57:57 +00:00
def seen(self, mask: IrcString, target: IrcString, args: Dict):
"""Get last seen date and message for a nick
2017-05-29 12:39:48 +00:00
%%seen [<nick>]
"""
nick = args.get('<nick>', mask.nick)
2017-06-29 21:04:21 +00:00
2017-06-30 12:04:50 +00:00
# Don't be stupid
2017-05-29 12:39:48 +00:00
if nick == mask.nick:
return '{}, look in the mirror faggot!'.format(nick)
2017-06-29 21:04:21 +00:00
2017-06-30 12:04:50 +00:00
# Fetch seen from database
self.cur.execute('''
SELECT
2017-06-30 12:04:50 +00:00
*
FROM
2017-06-30 12:04:50 +00:00
seens
WHERE
lower(nick) = lower(%s)
2017-06-30 12:04:50 +00:00
''', [nick])
2017-05-29 12:39:48 +00:00
seen = self.cur.fetchone()
2017-06-29 21:04:21 +00:00
2017-06-30 12:04:50 +00:00
# No result
2017-05-29 12:39:48 +00:00
if not seen:
return 'I\'ve never seen {}'.format(nick)
2017-06-29 21:04:21 +00:00
2017-06-30 12:04:50 +00:00
# Return result
2017-05-29 12:39:48 +00:00
return '{nick} was last seen {delta} saying: {message}'.format(
nick=seen['nick'],
2017-06-30 12:04:50 +00:00
# TODO: relative string delta?
delta=seen['seen_at'],
2017-05-29 16:44:26 +00:00
message=re.sub(r'\x01ACTION (.*)\x01', r'/me \1', seen['message']),
2017-05-29 12:39:48 +00:00
)
2017-06-29 21:04:21 +00:00
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)')
def save(self, mask: str, target: str, msg: str):
mask = IrcString(mask)
self.cur.execute('''
INSERT INTO
seens (nick, host, channel, message)
VALUES
(%s, %s, %s, %s)
ON CONFLICT (nick) DO UPDATE SET
host = excluded.host,
channel = excluded.channel,
seen_at = now(),
message = excluded.message
''', [mask.nick, mask.host, target, msg])
self.con.commit()