From 2a134918a133e1961d98dc4de267483e06fc052a Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Wed, 30 Mar 2022 22:31:27 -0500 Subject: [PATCH] Attempt to add support for server-specific leaderboard --- src/commands/top.ts | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/commands/top.ts b/src/commands/top.ts index c1c12dc..1a27955 100644 --- a/src/commands/top.ts +++ b/src/commands/top.ts @@ -11,29 +11,52 @@ export const data = new SlashCommandBuilder() export async function execute(interaction) { await interaction.deferReply(); - let messageContent = ''; userScore .find({}) .sort({ score: -1 }) // Sort by descending order .exec((err, obj) => { if (err) { log({ logger: 'top', content: `Getting top players failed: ${err}`, level: 'error' }); - console.log(err); } + if (obj.length < 10) { // Need at least 10 scores for top 10 return interaction.followUp( `There are only ${obj.length} users, we need at least 10!`, ); } + + const embeds : unknown[] = []; + let lbMessageContent = ''; + for (let i = 0; i < 10; i++) { - messageContent += `${i + 1}: <@${obj[i].authorID}>: ${obj[i].score}\n`; // Loop through each user and add their name and score to leaderboard content + lbMessageContent += `${i + 1}: <@${obj[i].authorID}>: ${obj[i].score}\n`; // Loop through each user and add their name and score to leaderboard content } + const leaderboardEmbed = new MessageEmbed() .setTitle('Top Ten!') - .setDescription(messageContent) + .setDescription(lbMessageContent) .setColor('#ffffff'); - interaction.followUp({ embeds: [leaderboardEmbed] }); + embeds.push(leaderboardEmbed); + + let sMessageContent = ''; + const members = interaction.guild.members.cache; + + const serverLeaderBoardArray = obj.filter(o => members.some(m => m.user.id === o.authorID)); + if (serverLeaderBoardArray.length < 10) { + for (let i = 0; i < 10; i++) { + sMessageContent += `${i + 1}: <@${serverLeaderBoardArray[i].authorID}>: ${serverLeaderBoardArray[i].score}\n`; + } + + const sLeaderboardEmbed = new MessageEmbed() + .setTitle(`Top Ten in ${interaction.guild.name}!`) + .setDescription(sMessageContent) + .setColor('#ffffff'); + + embeds.push(sLeaderboardEmbed); + } + + interaction.followUp({ embeds: embeds }); }); -} \ No newline at end of file +}