From e7191def02da8ca7835c1fea885491f51e9c191b Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 7 Jun 2020 19:52:05 +0200 Subject: [PATCH] Split long short-list results up into embed fields --- PluralKit.Bot/Lists/ShortRenderer.cs | 32 ++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/PluralKit.Bot/Lists/ShortRenderer.cs b/PluralKit.Bot/Lists/ShortRenderer.cs index fa056927..7fd48947 100644 --- a/PluralKit.Bot/Lists/ShortRenderer.cs +++ b/PluralKit.Bot/Lists/ShortRenderer.cs @@ -1,6 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; +using System.Text; using DSharpPlus.Entities; @@ -28,7 +27,32 @@ namespace PluralKit.Bot return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}**"; } - eb.Description = string.Join("\n", members.Select(RenderLine)); + var buf = new StringBuilder(); + var chunks = new List(); + + // Split the list into properly-sized chunks + foreach (var m in members) + { + var line = RenderLine(m); + + // First chunk goes in description (2048 chars), rest go in embed values (1000 chars) + var lengthLimit = chunks.Count == 0 ? 2048 : 1000; + if (buf.Length + line.Length + 1 > lengthLimit) + { + chunks.Add(buf.ToString()); + buf.Clear(); + } + + buf.Append(RenderLine(m)); + buf.Append("\n"); + } + chunks.Add(buf.ToString()); + + // Put the first chunk in the description, rest in blank-name embed fields + eb.Description = chunks[0]; + for (var i = 1; i < chunks.Count; i++) + // Field name is Unicode zero-width space + eb.AddField("\u200B", chunks[i]); } } } \ No newline at end of file