Let users @ the bot on a message to make it respond

Reply to any message + @bot to have the bot read and respond to it.
Also picks up image attachments from referenced messages so users
can reply to a photo with "@bot roast this".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-23 12:24:26 -05:00
parent 8a06ddbd6e
commit 86b23c2b7f

View File

@@ -55,7 +55,7 @@ class ChatCog(commands.Cog):
if self.bot.user in message.mentions:
should_reply = True
# Check if replying to one of the bot's messages
# Check if replying to a message
if message.reference and message.reference.message_id:
try:
ref_msg = message.reference.cached_message
@@ -64,9 +64,14 @@ class ChatCog(commands.Cog):
message.reference.message_id
)
if ref_msg.author.id == self.bot.user.id:
# Replying to the bot's own message — continue conversation
should_reply = True
if ref_msg.content:
reply_context = f"[Replying to bot's message: {ref_msg.content[:300]}]\n"
elif should_reply:
# @mentioned the bot while replying to someone else — include that message
ref_text = ref_msg.content[:500] if ref_msg.content else "(no text)"
reply_context = f"[{ref_msg.author.display_name} said: {ref_text}]\n"
except discord.HTTPException:
pass
@@ -99,13 +104,25 @@ class ChatCog(commands.Cog):
# Clean the mention out of the message content
content = message.content.replace(f"<@{self.bot.user.id}>", "").strip()
# Check for image attachments
# Check for image attachments (on this message or the referenced message)
image_attachment = None
for att in message.attachments:
ext = att.filename.rsplit(".", 1)[-1].lower() if "." in att.filename else ""
if ext in _IMAGE_TYPES:
image_attachment = att
break
if not image_attachment and message.reference:
try:
ref = message.reference.cached_message or await message.channel.fetch_message(
message.reference.message_id
)
for att in ref.attachments:
ext = att.filename.rsplit(".", 1)[-1].lower() if "." in att.filename else ""
if ext in _IMAGE_TYPES:
image_attachment = att
break
except discord.HTTPException:
pass
typing_ctx = None
@@ -165,6 +182,7 @@ class ChatCog(commands.Cog):
if response:
response = re.sub(r"\[Server context:[^\]]*\]\n?", "", response)
response = re.sub(r"\[Replying to bot's message:[^\]]*\]\n?", "", response)
response = re.sub(r"\[\w[\w ]* said:[^\]]*\]\n?", "", response)
# Catch reformatted metadata (LLM drops prefix but keeps content)
response = re.sub(r"\[[^\]]*#[a-z-]+[^\]]*(?:drama score|offense)[^\]]*\]\n?", "", response, flags=re.IGNORECASE)
response = response.strip()