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:
+20
-2
@@ -55,7 +55,7 @@ class ChatCog(commands.Cog):
|
|||||||
if self.bot.user in message.mentions:
|
if self.bot.user in message.mentions:
|
||||||
should_reply = True
|
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:
|
if message.reference and message.reference.message_id:
|
||||||
try:
|
try:
|
||||||
ref_msg = message.reference.cached_message
|
ref_msg = message.reference.cached_message
|
||||||
@@ -64,9 +64,14 @@ class ChatCog(commands.Cog):
|
|||||||
message.reference.message_id
|
message.reference.message_id
|
||||||
)
|
)
|
||||||
if ref_msg.author.id == self.bot.user.id:
|
if ref_msg.author.id == self.bot.user.id:
|
||||||
|
# Replying to the bot's own message — continue conversation
|
||||||
should_reply = True
|
should_reply = True
|
||||||
if ref_msg.content:
|
if ref_msg.content:
|
||||||
reply_context = f"[Replying to bot's message: {ref_msg.content[:300]}]\n"
|
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:
|
except discord.HTTPException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -99,13 +104,25 @@ class ChatCog(commands.Cog):
|
|||||||
# Clean the mention out of the message content
|
# Clean the mention out of the message content
|
||||||
content = message.content.replace(f"<@{self.bot.user.id}>", "").strip()
|
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
|
image_attachment = None
|
||||||
for att in message.attachments:
|
for att in message.attachments:
|
||||||
ext = att.filename.rsplit(".", 1)[-1].lower() if "." in att.filename else ""
|
ext = att.filename.rsplit(".", 1)[-1].lower() if "." in att.filename else ""
|
||||||
if ext in _IMAGE_TYPES:
|
if ext in _IMAGE_TYPES:
|
||||||
image_attachment = att
|
image_attachment = att
|
||||||
break
|
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
|
typing_ctx = None
|
||||||
|
|
||||||
@@ -165,6 +182,7 @@ class ChatCog(commands.Cog):
|
|||||||
if response:
|
if response:
|
||||||
response = re.sub(r"\[Server context:[^\]]*\]\n?", "", response)
|
response = re.sub(r"\[Server context:[^\]]*\]\n?", "", response)
|
||||||
response = re.sub(r"\[Replying to bot's message:[^\]]*\]\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)
|
# Catch reformatted metadata (LLM drops prefix but keeps content)
|
||||||
response = re.sub(r"\[[^\]]*#[a-z-]+[^\]]*(?:drama score|offense)[^\]]*\]\n?", "", response, flags=re.IGNORECASE)
|
response = re.sub(r"\[[^\]]*#[a-z-]+[^\]]*(?:drama score|offense)[^\]]*\]\n?", "", response, flags=re.IGNORECASE)
|
||||||
response = response.strip()
|
response = response.strip()
|
||||||
|
|||||||
Reference in New Issue
Block a user