Skip to content

Commit

Permalink
Merge pull request #457 from Ixrec/truncate-edit-delete-logs
Browse files Browse the repository at this point in the history
#418 Auto-truncate long message edit/delete logs, so we no longer fail to post logs at all
  • Loading branch information
Ixrec authored Aug 6, 2023
2 parents a244fff + ae10f73 commit 2436118
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
65 changes: 55 additions & 10 deletions Izzy-Moonbot/EventListeners/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,34 @@ private async Task ProcessMessageUpdate(
await _modLogger.CreateModLog(defaultGuild).SetContent(oldEditWarning).SetFileLogContent(oldEditWarning).Send();
}

var logMessage =
$"Message {newMessage.Id} by {DiscordHelper.DisplayName(author, defaultGuild)} ({author.Username}/{author.Id}) **edited** in {channel.Name}:\n" +
var logMessageTemplate =
$"Message {newMessage.Id} by {DiscordHelper.DisplayName(author, defaultGuild)} ({author.Username}/{author.Id}) **edited** in {channel.Name}:" +
"\n{warn}" +
(oldContent != null ?
$"__Before__:\n{oldContent}\n" :
"__Before__:\n{old}\n" :
"Content before edit unknown (this usually means the original message was too old to be in Izzy's cache).\n") +
$"__After__:\n{newMessage.Content}";
"__After__:\n{new}";

var oldLength = oldContent?.Length ?? 0;
var newContent = newMessage.Content;
var truncationWarning = "";
if (logMessageTemplate.Length + oldLength + newContent.Length > DiscordHelper.MessageLengthLimit) {
truncationWarning = "⚠️ The message needed to be truncated\n";
var spaceForMessages = DiscordHelper.MessageLengthLimit - logMessageTemplate.Length - truncationWarning.Length;
var truncationMarker = "\n[...]\n";
var spaceForHalfMessage = ((spaceForMessages / 2) - truncationMarker.Length) / 2;

if (oldContent != null)
oldContent = oldContent.Substring(0, spaceForHalfMessage) +
truncationMarker +
oldContent.Substring(oldLength - spaceForHalfMessage);

newContent = newContent.Substring(0, spaceForHalfMessage) +
truncationMarker +
newContent.Substring(newContent.Length - spaceForHalfMessage);
}

var logMessage = logMessageTemplate.Replace("{warn}", truncationWarning).Replace("{old}", oldContent).Replace("{new}", newContent);
await logChannel.SendMessageAsync(logMessage, allowedMentions: AllowedMentions.None);
}

Expand Down Expand Up @@ -96,19 +117,43 @@ private async Task ProcessMessageDelete(
if (author.Id == client.CurrentUser.Id) return; // Don't process self.
if (author.IsBot) return; // Don't listen to bots

var logMessage = $"Message id {messageId} by {DiscordHelper.DisplayName(author, defaultGuild)} ({author.Username}/{author.Id}) **deleted**";
var logMessageTemplate = $"Message id {messageId} by {DiscordHelper.DisplayName(author, defaultGuild)} ({author.Username}/{author.Id}) **deleted**";

if (channel is null)
logMessage += $" in unknown channel {channelId}:\n";
logMessageTemplate += $" in unknown channel {channelId}:\n";
else
logMessage += $" in {channel.Name}:\n";
logMessageTemplate += $" in {channel.Name}:\n";

logMessageTemplate += "{warn}";

var attachmentUrls = "";
if (message.Attachments?.Any() ?? false)
logMessage += $"__Content__:\n{message.Content}\n" +
$"__Attachments__:\n{string.Join('\n', message.Attachments.Select(a => a.ProxyUrl))}";
{
logMessageTemplate += "__Content__:\n{content}\n__Attachments__:\n{attachments}";
attachmentUrls = string.Join('\n', message.Attachments.Select(a => a.ProxyUrl));
}
else
logMessage += $"{message.Content}";
logMessageTemplate += "{content}";

var truncationWarning = "";
var content = message.Content;
if (logMessageTemplate.Length + content.Length + attachmentUrls.Length > DiscordHelper.MessageLengthLimit)
{
truncationWarning = "⚠️ The message needed to be truncated\n";
var spaceForMessages = DiscordHelper.MessageLengthLimit - logMessageTemplate.Length - truncationWarning.Length;
var truncationMarker = "\n[...]\n";

var spaceForHalfContent = ((int)Math.Floor(spaceForMessages * 0.9) - truncationMarker.Length) / 2;
content = content.Substring(0, spaceForHalfContent) +
truncationMarker +
content.Substring(content.Length - spaceForHalfContent);

var spaceForAttachments = (int)Math.Floor(spaceForMessages * 0.1);
if (attachmentUrls.Length > spaceForAttachments)
attachmentUrls = attachmentUrls.Substring(0, spaceForAttachments) + truncationMarker;
}

var logMessage = logMessageTemplate.Replace("{warn}", truncationWarning).Replace("{content}", content).Replace("{attachments}", attachmentUrls);
await logChannel.SendMessageAsync(logMessage, allowedMentions: AllowedMentions.None);
}

Expand Down
2 changes: 2 additions & 0 deletions Izzy-Moonbot/Helpers/DiscordHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Izzy_Moonbot.Helpers;

public static class DiscordHelper
{
public readonly static int MessageLengthLimit = 2000;

// These setters should only be used by tests
public static ulong? DefaultGuildId { get; set; } = null;
public static List<ulong>? DevUserIds { get; set; } = null;
Expand Down
7 changes: 3 additions & 4 deletions Izzy-Moonbot/Helpers/PaginationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,16 @@ await _message.ModifyAsync(msg =>
var truncationWarning = "";
var paginationBoilerplate = 200; // intentionally high so we have space for future changes
var discordMessageLimit = 2000;
var header = _staticParts[0];
var page = Pages[_pageNumber];
var footer = _staticParts[1];
if (header.Length + footer.Length + page.Length + paginationBoilerplate > discordMessageLimit)
if (header.Length + footer.Length + page.Length + paginationBoilerplate > DiscordHelper.MessageLengthLimit)
{
truncationWarning = "⚠️ Some items needed to be truncated";
var items = page.Split('\n');
var newlinesInPage = items.Count();
var spaceForPage = discordMessageLimit - header.Length - footer.Length - paginationBoilerplate - newlinesInPage;
var spaceForPage = DiscordHelper.MessageLengthLimit - header.Length - footer.Length - paginationBoilerplate - newlinesInPage;
var maxItemLength = spaceForPage / items.Count();
var truncationMarker = "[...]";
Expand Down Expand Up @@ -261,4 +260,4 @@ private string GetRandomEmoji()
"<:izzyscrunch:907312716979511317>",
"<:izzysneaksy:1044262492932673607>",
};
}
}

0 comments on commit 2436118

Please sign in to comment.