nxy/nxy/plugins/seen.py
2017-05-30 13:51:45 +02:00

44 lines
1.5 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']
table = 'seens'
@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'/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._insert(nick=nick, channel=channel.lower(), message=msg,
last_seen=datetime.now())