TheOchulus/commands/100-games/leaderboard.js

69 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-12-19 23:19:08 +01:00
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
2025-01-10 19:16:32 +01:00
const { getLeaderboardEntries, getLeaderboardEntriesBetweenDates } = require('../../databaseHelperFunctions.js');
2023-12-19 23:19:08 +01:00
module.exports = {
data: new SlashCommandBuilder()
.setName('leaderboard')
2025-01-10 19:16:32 +01:00
.setDescription('Show the leaderboard!')
.addIntegerOption(option => option.setName('year').setDescription('The year to check').addChoices({ name: '2024', value: 2024 }, { name: '2025', value: 2025 })),
2023-12-19 23:19:08 +01:00
async execute(interaction) {
await interaction.deferReply();
2023-12-19 23:19:08 +01:00
2025-01-10 19:16:32 +01:00
const yearOption = interaction.options.getInteger('year');
let leaderboard;
if (yearOption) {
leaderboard = await getLeaderboardEntriesBetweenDates(`${yearOption}-01-01`, `${yearOption}-12-31`);
}
else {
leaderboard = await getLeaderboardEntries();
}
if (!leaderboard) return interaction.editReply({ content: 'There was a problem!', ephemeral: true });
2023-12-19 23:19:08 +01:00
let desc = '';
2024-04-20 17:24:37 +02:00
let count = 0;
for (let i = 0; i < leaderboard.length; i++) {
if (leaderboard[i].count > 0) {
count += leaderboard[i].count;
}
}
desc = desc.concat('__Total: ', count, '__');
2023-12-19 23:19:08 +01:00
for (let i = 0; i < leaderboard.length; i++) {
if (leaderboard[i].count > 0) {
2024-02-28 19:12:04 +01:00
let newLine = String.prototype.concat(leaderboard[i].username, ' - ', leaderboard[i].count, ' games');
const number = String.prototype.concat('**#', (i + 1), '** ');
if (i == 0) newLine = String.prototype.concat('**', newLine, '**');
if (leaderboard[i].username == interaction.user.username) newLine = String.prototype.concat('*', newLine, '*');
2024-02-28 19:12:04 +01:00
newLine = String.prototype.concat(number, newLine);
newLine = String.prototype.concat('\n', newLine);
desc = String.prototype.concat(desc, newLine);
}
2023-12-19 23:19:08 +01:00
}
const embed = new EmbedBuilder()
.setColor(0x6441a5)
.setTitle('The 100 Games Challenge Leaderboard!')
.setDescription(desc)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp();
return interaction.editReply({ embeds: [embed] });
2023-12-19 23:19:08 +01:00
},
};