Compare commits

..

2 Commits

Author SHA1 Message Date
4140f52c69 .help returns an error if command not found 2018-05-29 13:59:38 -04:00
447e0be03c added --context to seendb 2018-05-29 13:58:03 -04:00
2 changed files with 24 additions and 3 deletions

View File

@ -15,7 +15,7 @@ def help(bot, trigger):
name = trigger.group(2)
name = name.lower()
if name not in bot.commands:
return
return bot.msg("Command not found.")
cmd = bot.commands[name]
docstring, examples = cmd.doc
if examples:

View File

@ -2,6 +2,7 @@
"""
When was this user last seen.
"""
import os
import time
import argparse
import threading
@ -11,7 +12,7 @@ from sqlite3 import OperationalError
from requests.structures import CaseInsensitiveDict
from tools.time import relativeTime
from module import commands, example, hook, require_chanmsg
from module import commands, example, hook, require_chanmsg, rate
def load_database(bot):
@ -49,7 +50,7 @@ def setup(bot):
bot.memory["seen"] = load_database(bot)
bot.memory["seen_last_dump"] = time.time()
@rate(1)
@commands('seen')
@example(".seen Nigger -l", "Last heard from Nigger at [1997-03-12 16:30:00] "\
+"with \"Just going to the store for some smokes babe I'll be right back\"")
@ -60,6 +61,7 @@ def seen(bot, trigger):
--last [-l] reports when the user was last seen. This is the default.
--first [-f] reports when the user was first seen.
--message [-m] includes the first/last message the user sent.
--context [-c] includes irc logs before and after their last message as context. Implies --message.
"""
if not trigger.group(2):
return bot.reply("Seen who?")
@ -69,6 +71,7 @@ def seen(bot, trigger):
parser.add_argument("-l", "--last", action="store_true", default=True)
parser.add_argument("-f", "--first", action="store_true")
parser.add_argument("-m", "--message", action="store_true")
parser.add_argument("-c", "--context", action="store_true")
args = parser.parse_args(trigger.group[3:])
if args.nick == bot.nick:
@ -100,6 +103,24 @@ def seen(bot, trigger):
bot.msg(msg)
if args.context:
num_con_lines = 2
fname = os.path.join(bot.log_path, bot.hostname,channel,timestamp[1:11])
fname += ".log"
if not os.path.isfile(fname):
return bot.msg("Context not available.")
with open(fname, "r") as file:
data = file.read().splitlines()
matches = [line for line in data if line.startswith(timestamp)]
if not matches:
return bot.msg("Context not available")
index = data.index(matches[0]) # a bit lazy, but w/e
start = max([index - num_con_lines, 0])
end = min([index + num_con_lines+1, len(data) - 1])
bot.msg("Context:")
bot.msg("\n".join(data[start:end]))
del data
def dump_seen_db(bot):
"""