Add !grabquote [user] to quote a user's last line

This commit is contained in:
megaproxy 2026-06-04 17:36:03 +01:00
parent 52e579e364
commit 0a4edfe245
3 changed files with 40 additions and 3 deletions

33
bot.py
View file

@ -113,6 +113,10 @@ class IRCBot:
self.store = store
self.sock: socket.socket | None = None
self._recv_buf = b""
# Last non-command channel line per user, for !grabquote.
# Keyed by lowercased nick -> (original_nick, text).
self.last_lines: dict[str, tuple[str, str]] = {}
self.last_speaker: str | None = None
# --- low-level I/O ----------------------------------------------------
@ -233,11 +237,17 @@ class IRCBot:
return
target, message = params[0], params[1]
sender = prefix.split("!", 1)[0] if prefix else ""
is_channel = target.startswith(("#", "&"))
# Reply to the channel for channel messages, or to the user for PMs.
reply_to = target if target.startswith(("#", "&")) else sender
reply_to = target if is_channel else sender
if not message.startswith(CMD_PREFIX):
# Remember the last real line each user said, so !grabquote can
# recall it. Bot commands are deliberately not recorded.
if is_channel and sender:
self.last_lines[sender.lower()] = (sender, message)
self.last_speaker = sender
return
parts = message[len(CMD_PREFIX):].strip().split(" ", 1)
cmd = parts[0].lower()
@ -293,6 +303,21 @@ def cmd_delquote(bot: IRCBot, reply_to: str, sender: str, arg: str):
bot.send_privmsg(reply_to, f"Deleted quote #{arg}." if ok else f"No quote #{arg}.")
def cmd_grabquote(bot: IRCBot, reply_to: str, sender: str, arg: str):
"""!grabquote [user] — store the last line a user said (default: last speaker)."""
nick = arg.split()[0] if arg.split() else bot.last_speaker
if not nick:
bot.send_privmsg(reply_to, "Nothing to grab yet.")
return
rec = bot.last_lines.get(nick.lower())
if not rec:
bot.send_privmsg(reply_to, f"I haven't seen {nick} say anything.")
return
orig_nick, text = rec
qid = bot.store.add(f"<{orig_nick}> {text}", sender)
bot.send_privmsg(reply_to, f"Grabbed quote #{qid}: <{orig_nick}> {text}")
def cmd_search(bot: IRCBot, reply_to: str, sender: str, arg: str):
"""!search <term> — first quote matching a substring."""
if not arg:
@ -310,14 +335,16 @@ def cmd_count(bot: IRCBot, reply_to: str, sender: str, arg: str):
def cmd_help(bot: IRCBot, reply_to: str, sender: str, arg: str):
bot.send_privmsg(
reply_to,
"Commands: !quote [id], !addquote <text>, !delquote <id>, "
"!search <term>, !quotecount, !help",
"Commands: !quote [id], !addquote <text>, !grabquote [user], "
"!delquote <id>, !search <term>, !quotecount, !help",
)
COMMANDS = {
"quote": cmd_quote,
"addquote": cmd_addquote,
"grabquote": cmd_grabquote,
"grab": cmd_grabquote,
"delquote": cmd_delquote,
"search": cmd_search,
"quotecount": cmd_count,