From 7595273822a70d48182cef7883ec7fe3fc42753d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 19 Aug 2023 20:38:19 +0200 Subject: [PATCH] Resolve crash when data.name exceed the length limit (#104) Reviewed-on: https://git.mylloon.fr/ConfrerieDuKassoulait/Botanique/pulls/104 Co-authored-by: Mylloon Co-committed-by: Mylloon --- src/commands/music/play.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/commands/music/play.ts b/src/commands/music/play.ts index 26da0e2..cb754d5 100644 --- a/src/commands/music/play.ts +++ b/src/commands/music/play.ts @@ -196,10 +196,29 @@ export default { // Returns a list of songs with their title and author return interaction.respond( - tracks.map((t) => ({ - name: `${t.title} • ${t.author}`, - value: t.url, - })) + tracks.map((t) => { + let title = t.title; + let author = t.author; + let name = `${title} • ${author}`; + + // Slice returned data if needed to not exceed the length limit (100) + if (name.length > 100) { + const newTitle = title.substring(0, 40); + if (title.length != newTitle.length) { + title = `${newTitle}...`; + } + const newAuthor = author.substring(0, 40); + if (author.length != newAuthor.length) { + author = `${newAuthor}...`; + } + name = `${newTitle} • ${newAuthor}`; + } + + return { + name, + value: t.url, + }; + }) ); } }