Add last seen module
Diff
requirements.txt | 5 ++---
dave/dave.py | 9 ++++-----
dave/modules/seen.py | 34 ++++++++++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 8 deletions(-)
@@ -14,6 +14,5 @@
lxml==3.6.0
pysocks==1.5.7
wolframalpha==2.4
nltk==3.2.1
markovify==0.4.3
babel==2.3.4
babel==2.3.4
humanize==0.5.1
@@ -10,7 +10,7 @@
import config
import datetime
from twisted.internet import reactor
from twisted.internet.threads import deferToThreadPool
from twisted.internet.threads import deferToThread
class Dave(irc.IRCClient):
@@ -76,14 +76,12 @@
if method[1] is not None:
deferToThreadPool(reactor, reactor.getThreadPool(), method[1], self,
method[2], nick, channel)
deferToThread(method[1], self, method[2], nick, channel)
if not (hasattr(method[1], "dont_always_run") and method[1].dont_always_run):
for m in run:
deferToThreadPool(reactor, reactor.getThreadPool(), m[0], self,
m[1], nick, channel)
deferToThread(m[0], self, m[1], nick, channel)
def irc_unknown(self, prefix, command, params):
if command == "INVITE":
@@ -100,4 +98,5 @@
factory.protocol = Dave
reactor.connectSSL(config.config["irc"]["host"], config.config["irc"]["port"],
factory, ssl.ClientContextFactory())
reactor.suggestThreadPoolSize(30)
reactor.run()
@@ -1,0 +1,34 @@
import pickle
import dave.module
import dave.config
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
from humanize import naturaltime
from datetime import datetime
@dave.module.help("Syntax: seen [username]. Checks when we last saw a user.")
@dave.module.command(["seen", "lastseen"], r"([A-Za-z_\-\[\]\\^{}|`][A-Za-z0-9_\-\[\]\\^{}|`]*)$")
def seen(bot, args, sender, source):
if not dave.config.redis.exists("lastseen:{}".format(args[0].lower())):
bot.reply(source, sender, "I've never seen that user before.")
return
seen = pickle.loads(dave.config.redis.get("lastseen:{}".format(args[0].lower())))
print(seen)
bot.reply(source, sender, "{} was last seen {} saying: {}".format(
seen["name"],
naturaltime(datetime.utcnow() - seen["when"]),
seen["msg"]
))
@dave.module.match(r'(.*)')
@dave.module.always_run()
def log_last_seen(bot, args, sender, source):
"""Whenever someone sends a new message, log what they sent and the time it was sent"""
dave.config.redis.set("lastseen:{}".format(sender.lower()), pickle.dumps({
"name": sender,
"when": datetime.utcnow(),
"msg": args[0]
}))