Skip to content

Commit

Permalink
Added economy leaderboard command
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteNikki committed Nov 7, 2024
1 parent c2f0561 commit 668f621
Show file tree
Hide file tree
Showing 5 changed files with 516 additions and 1 deletion.
70 changes: 70 additions & 0 deletions src/interactions/commands/economy/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ApplicationIntegrationType, AttachmentBuilder, EmbedBuilder, InteractionContextType, SlashCommandBuilder } from 'discord.js';
import { t } from 'i18next';

import { LeaderboardBuilder } from 'classes/balance-leaderboard';
import { Command } from 'classes/command';

import { computeLeaderboard, getLeaderboard } from 'db/user';

import { chunk } from 'utils/common';
import { pagination } from 'utils/pagination';

import { ModuleType } from 'types/interactions';

export default new Command({
module: ModuleType.Economy,
botPermissions: ['SendMessages'],
data: new SlashCommandBuilder()
.setName('leaderboard')
.setDescription('Shows the richest users')
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall)
.setContexts(InteractionContextType.BotDM, InteractionContextType.Guild, InteractionContextType.PrivateChannel)
.addBooleanOption((option) => option.setName('ephemeral').setDescription('When set to false will show the message to everyone').setRequired(false)),
async execute({ interaction, client, lng }) {
if (!interaction.inCachedGuild()) {
return;
}

const { options } = interaction;

const ephemeral = options.getBoolean('ephemeral', false) ?? true;
await interaction.deferReply({ ephemeral });

const leaderboard = await getLeaderboard();
const computedLeaderboard = await computeLeaderboard(leaderboard, client);

if (!computedLeaderboard.length) {
await interaction.editReply({ embeds: [new EmbedBuilder().setColor(client.colors.error).setDescription(t('level.none', { lng }))] });
return;
}

const chunkedLeaderboard = chunk(computedLeaderboard, 5);

const embeds: EmbedBuilder[] = [];

const attachments: AttachmentBuilder[] = [];

for (let i = 0; i < chunkedLeaderboard.length; i++) {
const leaderboardBuilder = new LeaderboardBuilder({
players: chunkedLeaderboard[i].map((user) => ({
avatar: user.avatar,
username: user.username ?? user.userId,
displayName: user.displayName ?? 'Unknown User',
rank: user.position,
bank: user.bank,
wallet: user.wallet
}))
});
if (i === 0) {
leaderboardBuilder.setVariant('default');
} else {
leaderboardBuilder.setVariant('horizontal');
}
const image = await leaderboardBuilder.build({ format: 'png' });
attachments.push(new AttachmentBuilder(image, { name: `leaderboard_${i}.png` }));
embeds.push(new EmbedBuilder().setColor(client.colors.level).setImage(`attachment://leaderboard_${i}.png`));
}

await pagination({ interaction, embeds, attachments });
}
});
Loading

0 comments on commit 668f621

Please sign in to comment.