From 4f9702dc3ac65a82dbcb6dc260aa325150aff1c9 Mon Sep 17 00:00:00 2001 From: eashwar mohan Date: Sun, 9 Jun 2024 22:10:10 -0700 Subject: [PATCH] Truncate leaderboard message if it exceeds 2000 characters (#29) --- src/commands/subcommands/leaderboard.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/commands/subcommands/leaderboard.ts b/src/commands/subcommands/leaderboard.ts index 6d58c92..cecc087 100644 --- a/src/commands/subcommands/leaderboard.ts +++ b/src/commands/subcommands/leaderboard.ts @@ -36,8 +36,18 @@ const calculateLeaderboardForSeason = async ( response.push(`${idx}. <@${row.userId}> : ${row.points}`); } + let finalMessage = response.join('\n'); + + if (finalMessage.length > 2_000) { + const truncatedResponse = response.slice(0, 11); + + truncatedResponse.splice(1, 0, '*Only displaying top 10 due to message length limit.*'); + + finalMessage = truncatedResponse.join('\n'); + } + await interaction.reply({ - content: response.join('\n'), + content: finalMessage, ephemeral: true, }); }; @@ -85,6 +95,16 @@ const calculateLeaderboardForWeek = async ( response.push(`${idx}. <@${entry.userId}> : [${entry.reacts}](${entry.messageUrl})`); } + let finalMessage = response.join('\n'); + + if (finalMessage.length > 2_000) { + const truncatedResponse = response.slice(0, 11); + + truncatedResponse.splice(1, 0, '*Only displaying top 10 due to message length limit.*'); + + finalMessage = truncatedResponse.join('\n'); + } + await interaction.reply({ content: response.join('\n'), ephemeral: true,