Message Caching
DiSky’s message caching system allows you to track information about deleted and edited messages. This is crucial for moderation, logging, and maintaining accountability in your Discord server.
How It Works
Section titled “How It Works”DiSky caches received messages in memory while your bot is running. This means:
- Only messages sent while your bot is online are cached
- Messages sent before the bot started are not cached
- Cache is cleared when the bot restarts
- Memory usage increases with cached messages
Edited Messages
Section titled “Edited Messages”When a message is edited, DiSky updates its cache with both the old and new content.
How Editing Works
Section titled “How Editing Works”- User sends a message
- DiSky caches the message content
- User edits the message
- DiSky stores the old content and updates with new content
- You can access both versions in the
on message editevent
Using the Edit Event
Section titled “Using the Edit Event”on message edit: # event-string contains the OLD content # event-message contains the NEW/current content
set {_old} to event-string set {_new} to event-message
# Log to moderation channel set {_logChannel} to text channel with id "MOD_LOG_CHANNEL_ID"
make embed: set title of embed to "📝 Message Edited" add field named "Author" with value mention tag of author of {_new} to fields of embed add field named "Channel" with value mention tag of message channel of {_new} to fields of embed add field named "Before" with value {_old} to fields of embed add field named "After" with value {_new} to fields of embed add field named "Jump" with value "[Go to message](%jump url of {_new}%)" to fields of embed set embed color of embed to orange set timestamp of embed to now
post last embed to {_logChannel}Deleted Messages
Section titled “Deleted Messages”When a message is deleted, DiSky recreates a “fake” message object with all cached information.
How Deletion Works
Section titled “How Deletion Works”- User sends a message
- DiSky caches all message data (content, author, attachments, etc.)
- User or moderator deletes the message
- DiSky recreates a message object from the cache
- You can access the deleted message data in the
on message deleteevent
Using the Delete Event
Section titled “Using the Delete Event”on message delete: # event-message is a reconstructed message with cached data
set {_deleted} to event-message set {_content} to content of {_deleted} set {_author} to author of {_deleted} set {_channel} to message channel of {_deleted}
# Skip if no content (e.g., uncached message) if {_content} is not set: stop
# Log deletion set {_logChannel} to text channel with id "DELETE_LOG_CHANNEL_ID"
make embed: set title of embed to "🗑️ Message Deleted" add field named "Author" with value mention tag of {_author} to fields of embed add field named "Channel" with value mention tag of {_channel} to fields of embed add field named "Content" with value {_content} to fields of embed set embed color of embed to red set timestamp of embed to now
post last embed to {_logChannel}What Can You Access?
Section titled “What Can You Access?”From cached deleted messages, you can typically access:
- Message content (text)
- Author information
- Channel information
- Timestamp
- Attachments (URLs, not files)
- Embeds
- Message ID
Practical Examples
Section titled “Practical Examples”Complete Moderation Log
Section titled “Complete Moderation Log”# Message editson message edit: # Ignore bot messages if author of event-message is a bot: stop
set {_old} to event-string set {_new} to event-message set {_author} to author of {_new} set {_channel} to message channel of {_new}
set {_logChannel} to text channel with id "EDIT_LOG_CHANNEL"
make embed: set title of embed to "📝 Message Edited" set author of embed to discord name of {_author} set author icon of embed to avatar of {_author}
if length of {_old} > 1024: set {_old} to "%first 1020 characters of {_old}%..." if length of {_new} > 1024: set {_new} to "%first 1020 characters of {_new}%..."
add field named "Before" with value {_old} to fields of embed add field named "After" with value {_new} to fields of embed add field named "Location" with value "%mention tag of {_channel}% ([Jump](%jump url of {_new}%))" to fields of embed
set embed color of embed to orange set timestamp of embed to now set footer of embed to "User ID: %discord id of {_author}%"
post last embed to {_logChannel}
# Message deletionson message delete: set {_msg} to event-message
# Check if message was cached if {_msg} is not set: stop
set {_author} to author of {_msg}
# Ignore bot messages if {_author} is a bot: stop
set {_content} to content of {_msg} set {_channel} to message channel of {_msg}
# Skip empty messages (like image-only) if {_content} is not set: set {_content} to "*[No text content]*"
set {_logChannel} to text channel with id "DELETE_LOG_CHANNEL"
make embed: set title of embed to "🗑️ Message Deleted" set author of embed to discord name of {_author} set author icon of embed to avatar of {_author}
# Truncate long messages if length of {_content} > 1024: set {_content} to "%first 1020 characters of {_content}%..."
add field named "Content" with value {_content} to fields of embed add field named "Channel" with value mention tag of {_channel} to fields of embed add field named "Message ID" with value discord id of {_msg} to fields of embed
set embed color of embed to red set timestamp of embed to now set footer of embed to "User ID: %discord id of {_author}%"
post last embed to {_logChannel}Track Attachment Deletions
Section titled “Track Attachment Deletions”on message delete: set {_msg} to event-message
if {_msg} is not set: stop
# Get attachments set {_attachments::*} to attachments of {_msg}
if {_attachments::*} is set: set {_author} to author of {_msg} set {_channel} to message channel of {_msg}
set {_logChannel} to text channel with id "ATTACHMENT_LOG"
make embed: set title of embed to "📎 Message with Attachments Deleted" set author of embed to discord name of {_author} set author icon of embed to avatar of {_author}
add field named "Channel" with value mention tag of {_channel} to fields of embed
# List attachments clear {_attachList::*} loop {_attachments::*}: add "[%file name of loop-value%](%url of loop-value%)" to {_attachList::*}
add field named "Attachments" with value join {_attachList::*} with nl to fields of embed
set embed color of embed to purple set timestamp of embed to now
post last embed to {_logChannel}Filter Spam Deletions
Section titled “Filter Spam Deletions”# Track recently deleted messages by useron message delete: set {_msg} to event-message
if {_msg} is not set: stop
set {_author} to author of {_msg} set {_authorId} to discord id of {_author}
# Ignore bots if {_author} is a bot: stop
# Count recent deletions add 1 to {spam-tracker::%{_authorId}%}
# Clear counter after 10 seconds wait 10 seconds remove 1 from {spam-tracker::%{_authorId}%}
# Check for spam deletion (5+ messages in 10 seconds) if {spam-tracker::%{_authorId}%} >= 5: set {_logChannel} to text channel with id "SPAM_LOG"
make embed: set title of embed to "⚠️ Rapid Message Deletion Detected" set description of embed to "%mention tag of {_author}% deleted 5+ messages in 10 seconds" add field named "User" with value "%discord name of {_author}% (%{_authorId}%)" to fields of embed set embed color of embed to dark red set timestamp of embed to now
post last embed to {_logChannel}
# Reset counter set {spam-tracker::%{_authorId}%} to 0Restore Deleted Content
Section titled “Restore Deleted Content”command /restore <text>: trigger: # Only for moderators if event-user doesn't have permission "MANAGE_MESSAGES": reply with hidden "❌ You don't have permission!" stop
set {_messageId} to arg-1
# Check if message exists in cache (this is conceptual) # Note: You'd need to implement your own caching system for this
reply with "ℹ️ DiSky cannot retrieve deleted messages after they're deleted. Use message delete event to log them in real-time."Best Practices
Section titled “Best Practices”-
Log Immediately
Process edit/delete events as they happen. Don’t rely on retrieving data later.
-
Filter Bot Messages
Ignore bot edits/deletions to reduce log spam.
-
Check for Null
Always verify cached data exists before using it.
-
Truncate Long Content
Limit message content to prevent embed errors (1024 character field limit).
-
Store Important Data
If you need long-term message history, save critical data to a database.
-
Respect Privacy
Consider user privacy when logging message content. Make logs accessible only to moderators.
-
Monitor Cache Size
Be aware of memory usage if your bot handles many messages.
Limitations and Caveats
Section titled “Limitations and Caveats”Cache is Temporary
Section titled “Cache is Temporary”# ❌ This won't work - cache is cleared on restarton server start: # No access to messages from before bot startedCannot Modify Fake Messages
Section titled “Cannot Modify Fake Messages”on message delete: set {_msg} to event-message
# ❌ This will fail - message doesn't exist on Discord delete {_msg} # Results in "10008: Unknown Message" errorMemory Usage
Section titled “Memory Usage”# Large servers with many messages can consume significant RAM# Consider implementing:# - Cache size limits# - Periodic cache clearing# - Selective caching (only certain channels)Why This System Exists
Section titled “Why This System Exists”Discord’s API doesn’t provide message content for deleted messages. When a message is deleted, Discord only sends the message ID and channel ID - no content, author, or other data.
DiSky’s caching system solves this by:
- Storing message data when received
- Providing that data when deletion events occur
- Allowing moderation logging without relying on Discord’s API
Alternatives
Section titled “Alternatives”If you need permanent message history:
# Store messages in a databaseon message received: set {_msg} to event-message set {_id} to discord id of {_msg} set {_content} to content of {_msg} set {_author} to discord id of author of {_msg} set {_time} to now
# Store in database (pseudocode) execute "INSERT INTO messages VALUES ('%{_id}%', '%{_content}%', '%{_author}%', '%{_time}%')" in {sql}Troubleshooting
Section titled “Troubleshooting”Deleted Message Has No Content
Section titled “Deleted Message Has No Content”Problem: event-message is null in delete event.
Solutions:
- Message was sent before bot started (not cached)
- Message was sent during bot downtime
- Bot didn’t have permission to see the message
- Cache was cleared or bot restarted
Edit Event Not Showing Old Content
Section titled “Edit Event Not Showing Old Content”Problem: event-string is empty or same as new content.
Solutions:
- Message was edited before bot started
- Edit happened too quickly (race condition)
- Message wasn’t cached properly
”Unknown Message” Error
Section titled “”Unknown Message” Error”Problem: Trying to interact with deleted message.
Explanation: Deleted messages are reconstructed objects, not real Discord messages.
Solution: Only use cached data for reading. Don’t try to delete, edit, or reference fake messages.
Next Steps
Section titled “Next Steps”- Create Polls for gathering feedback
- Build Advanced Messages with embeds
- Learn about Error Handling
- Set up Audit Logs for moderation tracking