mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 09:09:00 +03:00
Split message clear log when cleared messages are too long (#288)
This change makes Octobot split the message clear log into multiple messages when the combined length of cleared messages exceeds the maximum length for an embed description. Closes #180
This commit is contained in:
parent
c2f7aadaea
commit
5e4d0a528c
1 changed files with 28 additions and 6 deletions
|
@ -102,7 +102,9 @@ public class ClearCommandGroup : CommandGroup
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var idList = new List<Snowflake>(messages.Count);
|
var idList = new List<Snowflake>(messages.Count);
|
||||||
var builder = new StringBuilder().AppendLine(Mention.Channel(channelId)).AppendLine();
|
|
||||||
|
var logEntries = new List<ClearedMessageEntry> { new() };
|
||||||
|
var currentLogEntry = 0;
|
||||||
for (var i = messages.Count - 1; i >= 1; i--) // '>= 1' to skip last message ('Octobot is thinking...')
|
for (var i = messages.Count - 1; i >= 1; i--) // '>= 1' to skip last message ('Octobot is thinking...')
|
||||||
{
|
{
|
||||||
var message = messages[i];
|
var message = messages[i];
|
||||||
|
@ -112,8 +114,17 @@ public class ClearCommandGroup : CommandGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
idList.Add(message.ID);
|
idList.Add(message.ID);
|
||||||
builder.AppendLine(string.Format(Messages.MessageFrom, Mention.User(message.Author)));
|
|
||||||
builder.Append(message.Content.InBlockCode());
|
var entry = logEntries[currentLogEntry];
|
||||||
|
var str = $"{string.Format(Messages.MessageFrom, Mention.User(message.Author))}\n{message.Content.InBlockCode()}";
|
||||||
|
if (entry.Builder.Length + str.Length > EmbedConstants.MaxDescriptionLength)
|
||||||
|
{
|
||||||
|
logEntries.Add(entry = new ClearedMessageEntry());
|
||||||
|
currentLogEntry++;
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.Builder.Append(str);
|
||||||
|
entry.DeletedCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (idList.Count == 0)
|
if (idList.Count == 0)
|
||||||
|
@ -127,7 +138,6 @@ public class ClearCommandGroup : CommandGroup
|
||||||
var title = author is not null
|
var title = author is not null
|
||||||
? string.Format(Messages.MessagesClearedFiltered, idList.Count.ToString(), author.GetTag())
|
? string.Format(Messages.MessagesClearedFiltered, idList.Count.ToString(), author.GetTag())
|
||||||
: string.Format(Messages.MessagesCleared, idList.Count.ToString());
|
: string.Format(Messages.MessagesCleared, idList.Count.ToString());
|
||||||
var description = builder.ToString();
|
|
||||||
|
|
||||||
var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
|
var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
|
||||||
channelId, idList, executor.GetTag().EncodeHeader(), ct);
|
channelId, idList, executor.GetTag().EncodeHeader(), ct);
|
||||||
|
@ -136,12 +146,24 @@ public class ClearCommandGroup : CommandGroup
|
||||||
return ResultExtensions.FromError(deleteResult);
|
return ResultExtensions.FromError(deleteResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
_utility.LogAction(
|
foreach (var log in logEntries)
|
||||||
data.Settings, channelId, executor, title, description, bot, ColorsList.Red, false, ct);
|
{
|
||||||
|
_utility.LogAction(
|
||||||
|
data.Settings, channelId, executor, author is not null
|
||||||
|
? string.Format(Messages.MessagesClearedFiltered, log.DeletedCount.ToString(), author.GetTag())
|
||||||
|
: string.Format(Messages.MessagesCleared, log.DeletedCount.ToString()),
|
||||||
|
log.Builder.ToString(), bot, ColorsList.Red, false, ct);
|
||||||
|
}
|
||||||
|
|
||||||
var embed = new EmbedBuilder().WithSmallTitle(title, bot)
|
var embed = new EmbedBuilder().WithSmallTitle(title, bot)
|
||||||
.WithColour(ColorsList.Green).Build();
|
.WithColour(ColorsList.Green).Build();
|
||||||
|
|
||||||
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sealed class ClearedMessageEntry
|
||||||
|
{
|
||||||
|
public StringBuilder Builder { get; } = new();
|
||||||
|
public int DeletedCount { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue