🏡 index : ~doyle/dave.git

author Jordan Doyle <jordan@doyle.wf> 2017-07-22 23:20:53.0 +01:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-07-22 23:20:53.0 +01:00:00
commit
21d113377faf34f4dc776903b6dd4be5764ff5dc [patch]
tree
650c09c280e7a719c7c9e36aaabb8f3070512c24
parent
7a2abbac7665f4a45accf28142f01cc7404b6e08
download
21d113377faf34f4dc776903b6dd4be5764ff5dc.tar.gz

Add last seen module



Diff

 requirements.txt     |  5 ++---
 dave/dave.py         |  9 ++++-----
 dave/modules/seen.py | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/requirements.txt b/requirements.txt
index 1472ff2..98e962c 100644
--- a/requirements.txt
+++ a/requirements.txt
@@ -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
diff --git a/dave/dave.py b/dave/dave.py
index f521f42..96279b2 100644
--- a/dave/dave.py
+++ a/dave/dave.py
@@ -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:
            # we matched a command
            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:
                    # modules that should always be run regardless of priority
                    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()
diff --git a/dave/modules/seen.py b/dave/modules/seen.py
new file mode 100644
index 0000000..f7002bb 100644
--- /dev/null
+++ a/dave/modules/seen.py
@@ -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]
    }))