Browse Source

Attempt to add basic server-specific leaderboard

pull/41/head
Abheek Dhawan 3 years ago
parent
commit
302275a2d8
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 33
      src/commands/top.ts

33
src/commands/top.ts

@ -11,7 +11,6 @@ export const data = new SlashCommandBuilder()
export async function execute(interaction) {
await interaction.deferReply();
let messageContent = '';
userScore
.find({})
.sort({ score: -1 }) // Sort by descending order
@ -20,20 +19,46 @@ export async function execute(interaction) {
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));
console.log(serverLeaderBoardArray[0]);
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 });
});
}
Loading…
Cancel
Save