Add user context for mentioned users in messages

- Bot now knows who is talking to it (display name, username, nickname)
- Bot recognizes other users mentioned in messages
- Includes user's display name, username, nickname, and top role
- Context is added to system prompt for AI awareness
This commit is contained in:
2026-01-11 20:53:16 +01:00
parent 1e2e6b0327
commit 8f521b869b

View File

@@ -97,7 +97,7 @@ class AIChatCog(commands.Cog):
# Show typing indicator while generating response
async with message.channel.typing():
try:
response_text = await self._generate_response(message.author.id, content)
response_text = await self._generate_response(message, content)
# Split and send response
chunks = split_message(response_text)
@@ -125,16 +125,56 @@ class AIChatCog(commands.Cog):
return content.strip()
async def _generate_response(self, user_id: int, user_message: str) -> str:
def _get_mentioned_users_context(self, message: discord.Message) -> str | None:
"""Get context about mentioned users (excluding the bot).
Args:
message: The Discord message
Returns:
Formatted string with user info, or None if no other users mentioned
"""
# Filter out the bot from mentions
other_mentions = [
m for m in message.mentions if self.bot.user is None or m.id != self.bot.user.id
]
if not other_mentions:
return None
user_info = []
for user in other_mentions:
# Get member info if available (for nickname, roles, etc.)
member = message.guild.get_member(user.id) if message.guild else None
if member:
info = f"- {member.display_name} (username: {member.name})"
if member.nick and member.nick != member.name:
info += f" [nickname: {member.nick}]"
# Add top role if not @everyone
if len(member.roles) > 1:
top_role = member.roles[-1] # Highest role
if top_role.name != "@everyone":
info += f" [role: {top_role.name}]"
else:
info = f"- {user.display_name} (username: {user.name})"
user_info.append(info)
return "Mentioned users:\n" + "\n".join(user_info)
async def _generate_response(self, message: discord.Message, user_message: str) -> str:
"""Generate an AI response for a user message.
Args:
user_id: Discord user ID
user_message: The user's message
message: The Discord message object
user_message: The user's message content
Returns:
The AI's response text
"""
user_id = message.author.id
# Get conversation history
history = self.conversations.get_history(user_id)
@@ -144,8 +184,23 @@ class AIChatCog(commands.Cog):
# Check if we should search the web
search_context = await self._maybe_search(user_message)
# Build system prompt with search context if available
# Get context about mentioned users
mentioned_users_context = self._get_mentioned_users_context(message)
# Build system prompt with additional context
system_prompt = self.ai_service.get_system_prompt()
# Add info about the user talking to the bot
author_info = f"\n\nYou are talking to: {message.author.display_name} (username: {message.author.name})"
if isinstance(message.author, discord.Member) and message.author.nick:
author_info += f" [nickname: {message.author.nick}]"
system_prompt += author_info
# Add mentioned users context
if mentioned_users_context:
system_prompt += f"\n\n--- {mentioned_users_context} ---"
# Add search results if available
if search_context:
system_prompt += (
"\n\n--- Web Search Results ---\n"