mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-25 10:15:55 +00:00
Leaderboard: update leaderboard to display users server name rather than discord username, and add their avatar
This commit is contained in:
parent
acd25f6a9c
commit
d482b8673b
1 changed files with 49 additions and 10 deletions
|
@ -10,25 +10,64 @@ module.exports = {
|
|||
const guild = interaction.guild;
|
||||
|
||||
try {
|
||||
// Fetch top 10 leaderboard entries sorted by correct answers
|
||||
const scores = await Leaderboard.find()
|
||||
.sort({ correctAnswers: -1 })
|
||||
.limit(10);
|
||||
|
||||
// Fetch display names and avatars for each leaderboard entry
|
||||
const leaderboardEntries = await Promise.all(
|
||||
scores.map(async (entry, index) => {
|
||||
try {
|
||||
const member = await guild.members.fetch(entry.userId);
|
||||
const displayName = member ? member.displayName : entry.username; // Fallback if member not found
|
||||
const avatarUrl = member.user.displayAvatarURL({
|
||||
dynamic: true,
|
||||
size: 32,
|
||||
}); // Get avatar URL
|
||||
|
||||
return {
|
||||
position: index + 1,
|
||||
name: displayName,
|
||||
correctAnswers: entry.correctAnswers,
|
||||
gamesPlayed: entry.gamesPlayed,
|
||||
avatarUrl: avatarUrl,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error fetching member for userId: ${entry.userId}`,
|
||||
error
|
||||
);
|
||||
return {
|
||||
position: index + 1,
|
||||
name: entry.username,
|
||||
correctAnswers: entry.correctAnswers,
|
||||
gamesPlayed: entry.gamesPlayed,
|
||||
avatarUrl: null, // No avatar in case of error
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const leaderboardEmbed = new EmbedBuilder()
|
||||
.setColor("#0099ff")
|
||||
.setTitle("Trivia Leaderboard")
|
||||
.setDescription(
|
||||
scores
|
||||
.map(
|
||||
(entry, index) =>
|
||||
`${index + 1}. ${entry.username}: ${
|
||||
entry.correctAnswers
|
||||
} correct answers in ${entry.gamesPlayed} games`
|
||||
)
|
||||
.join("\n")
|
||||
)
|
||||
.setTimestamp();
|
||||
|
||||
// Add each leaderboard entry with avatar as a thumbnail
|
||||
leaderboardEntries.forEach((entry) => {
|
||||
const fieldValue = `${entry.correctAnswers} correct answers in ${entry.gamesPlayed} games`;
|
||||
|
||||
leaderboardEmbed.addFields({
|
||||
name: `${entry.position}. ${entry.name}`,
|
||||
value: fieldValue,
|
||||
inline: false,
|
||||
});
|
||||
|
||||
// Use the avatar URL as a thumbnail for each user entry
|
||||
leaderboardEmbed.setThumbnail(entry.avatarUrl || null);
|
||||
});
|
||||
|
||||
if (guild.iconURL()) {
|
||||
leaderboardEmbed.setFooter({
|
||||
text: guild.name,
|
||||
|
|
Loading…
Reference in a new issue