Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions Modules/EmojisModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.IO.Compression;
using System.Text;

namespace Morpheus.Modules;

Expand Down Expand Up @@ -161,8 +162,71 @@ public async Task ListEmojis()
return;
}

string emojiList = string.Join("\n", emotes.Select(e => e.Name + " - " + e.ToString()));
await ReplyAsync($"**Custom Emojis:**\n{emojiList}");
IReadOnlyList<string> messages = BuildEmojiListMessages(
emotes.Select(e => e.Name + " - " + e.ToString()));

foreach (string message in messages)
await ReplyAsync(message);
}

internal static IReadOnlyList<string> BuildEmojiListMessages(IEnumerable<string> emojiEntries)
{
const string header = "**Custom Emojis:**";
const int maxMessageLength = 2000;
var messages = new List<string>();
var chunk = new StringBuilder(header);

void Flush()
{
if (chunk.Length == header.Length)
return;

messages.Add(chunk.ToString());
chunk.Clear().Append(header);
}

foreach (string entry in emojiEntries)
{
if (chunk.Length > header.Length
&& chunk.Length + 1 + entry.Length > maxMessageLength)
{
Flush();
}

int offset = 0;
while (offset < entry.Length)
{
int available = maxMessageLength - chunk.Length - 1;
if (available <= 0)
{
Flush();
continue;
}

int take = Math.Min(available, entry.Length - offset);
if (offset + take < entry.Length
&& char.IsHighSurrogate(entry[offset + take - 1])
&& char.IsLowSurrogate(entry[offset + take]))
{
take--;
}

if (take == 0)
{
Flush();
continue;
}

chunk.Append('\n').Append(entry.AsSpan(offset, take));
offset += take;

if (offset < entry.Length)
Flush();
}
}

Flush();
return messages;
}

[Name("Download Emojis")]
Expand Down
34 changes: 34 additions & 0 deletions Morpheus.Tests/EmojisModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ public void TryGetEmojiPage_HandlesEmptyAndOutOfRangePages(
Assert.Equal(expectedTotalPages, totalPages);
}

[Fact]
public void BuildEmojiListMessages_SplitsEntriesWithinDiscordLimit()
{
string[] entries = [.. Enumerable.Range(1, 100)
.Select(index => $"emoji-{index:D3} - <:emoji_{index:D3}:123456789012345678>")];

IReadOnlyList<string> messages = EmojisModule.BuildEmojiListMessages(entries);

Assert.True(messages.Count > 1);
Assert.All(messages, message => Assert.InRange(message.Length, 1, 2000));
Assert.All(messages, message => Assert.StartsWith("**Custom Emojis:**\n", message));
Assert.Equal(
entries,
messages.SelectMany(message => message.Split('\n').Skip(1)));
}

[Fact]
public void BuildEmojiListMessages_SplitsOversizedEntriesWithoutBreakingUnicode()
{
const string header = "**Custom Emojis:**\n";
string entry = new string('x', 1980) + "😀" + new string('y', 100);

IReadOnlyList<string> messages = EmojisModule.BuildEmojiListMessages([entry]);
string[] payloads = [.. messages.Select(message => message[header.Length..])];

Assert.All(messages, message => Assert.InRange(message.Length, 1, 2000));
Assert.Equal(entry, string.Concat(payloads));
Assert.All(payloads, payload =>
{
Assert.False(char.IsHighSurrogate(payload[^1]));
Assert.False(char.IsLowSurrogate(payload[0]));
});
}

[Fact]
public async Task ImportSessionStore_AppliesConcurrentPageUpdatesWithoutLosingChanges()
{
Expand Down