4.6 KiB
DM Reaction Logging Feature
Overview
This feature adds comprehensive reaction logging to the Miku bot's DM system. Both user reactions and Miku's reactions to any message in DMs are now tracked and displayed in the web UI.
What Was Added
1. Data Structure Enhancement (bot/utils/dm_logger.py)
- Modified Message Entry: Added
reactionsfield to each message entry that stores:emoji: The reaction emojireactor_id: Discord ID of who reactedreactor_name: Display name of the reactoris_bot: Boolean indicating if Miku reactedadded_at: Timestamp when reaction was added
2. Reaction Logging Methods (bot/utils/dm_logger.py)
Added two new async methods to the DMLogger class:
-
log_reaction_add(): Logs when a reaction is added- Parameters: user_id, message_id, emoji, reactor_id, reactor_name, is_bot_reactor
- Finds the message in logs and appends reaction data
- Prevents duplicate reactions
-
log_reaction_remove(): Logs when a reaction is removed- Parameters: user_id, message_id, emoji, reactor_id
- Finds and removes the specific reaction from message logs
3. Discord Event Handlers (bot/bot.py)
Added four event handlers to capture all reaction events:
on_reaction_add(): Handles cached message reactionson_raw_reaction_add(): Handles uncached messages (catches bot's own reactions)on_reaction_remove(): Handles cached message reaction removalson_raw_reaction_remove(): Handles uncached message reaction removals
All handlers:
- Filter for DM reactions only (ignore server reactions)
- Properly identify the DM user (not the bot)
- Log both user and bot reactions
- Handle emoji conversion to strings
4. Web UI Styling (bot/static/index.html)
Added CSS styles for reaction display:
.message-reactions: Container for reactions with flexbox layout.reaction-item: Individual reaction bubble with hover effects.reaction-emoji: Styled emoji display.reaction-by: Shows who reacted with color coding:- Bot reactions: cyan (#61dafb)
- User reactions: orange (#ffa726)
5. Web UI JavaScript (bot/static/index.html)
Enhanced displayUserConversations() function to:
- Check for reactions array in each message
- Generate HTML for each reaction showing:
- Emoji
- Who reacted (🤖 Miku or 👤 User)
- Tooltip with full details and timestamp
How It Works
Flow:
- User or Miku reacts to a message in DMs
- Discord event fires (
on_reaction_addoron_raw_reaction_add) - Event handler captures the reaction details
- DMLogger.log_reaction_add() stores the reaction in the user's JSON log
- Web UI displays reactions when viewing conversations
Data Storage:
Reactions are stored in memory/dms/{user_id}.json:
{
"user_id": 123456789,
"username": "User",
"conversations": [
{
"timestamp": "2025-11-03T12:00:00",
"message_id": 987654321,
"is_bot_message": false,
"content": "Hello Miku!",
"attachments": [],
"reactions": [
{
"emoji": "❤️",
"reactor_id": 111222333,
"reactor_name": "Miku",
"is_bot": true,
"added_at": "2025-11-03T12:01:00"
}
]
}
]
}
Features
✅ Tracks both user and bot reactions ✅ Logs reaction additions and removals ✅ Displays reactions in web UI with visual distinction ✅ Shows who reacted and when (via tooltip) ✅ Works with both cached and uncached messages ✅ Only tracks DM reactions (ignores server reactions) ✅ Color-coded by reactor type (bot vs user)
Benefits
- Complete conversation history: See not just messages but emotional responses via reactions
- Miku's reactions tracked: Know when Miku reacted to user messages
- User reactions tracked: See how users respond to Miku's messages
- Timestamped: Know when reactions were added
- Clean UI: Reactions displayed in attractive bubbles below messages
Testing
To test the feature:
- Send a DM to Miku
- React to one of Miku's messages with an emoji
- Have Miku react to one of your messages
- View the conversation in the web UI at
http://localhost:3939 - Click on "DM Users" → Select your user → View conversations
- You should see reactions displayed below the messages
Notes
- Reactions are only logged for DM conversations, not server messages
- The bot uses both regular and "raw" event handlers to catch all reactions, including its own
- Removing a reaction will remove it from the logs
- Reactions persist across bot restarts (stored in JSON files)