nxy/nxy/plugins/seen.py
2017-05-29 14:39:48 +02:00

45 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import re
import irc3
from datetime import datetime
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import DatabasePlugin
class Seen(DatabasePlugin):
requires = ['irc3.plugins.command',
'nxy.plugins.database']
@command(options_first=True)
def seen(self, mask: IrcString, channel: 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])
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'],
message=re.sub(r'\x01ACTION (.*)\x01', r'\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 or replace into seens (nick, channel, '
'message, last_seen) values (?, ?, ?, ?)',
[nick, channel.lower(), msg, datetime.now()])
self.con.commit()