Skip to content

Commit

Permalink
add Select menu in ticket system
Browse files Browse the repository at this point in the history
  • Loading branch information
GrishMahat committed Aug 8, 2024
1 parent 8e667aa commit 57bb514
Show file tree
Hide file tree
Showing 16 changed files with 1,075 additions and 438 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/commands/developer/dm.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export default {
category: 'Devloper',
prefix: true,


run: async (client, interaction) => {
const subcommand = interaction.options.getSubcommand();
let message = interaction.options.getString('message').trim();
Expand Down
161 changes: 84 additions & 77 deletions src/commands/economy/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,114 @@ const ITEMS_PER_PAGE = 12;
const LEADERBOARD_LIMIT = 50;

export default {
data: new SlashCommandBuilder()
.setName('leaderboard')
.setDescription('Displays the leaderboard based on user balances and bank.'),
userPermissions: [],
botPermissions: [],
cooldown: 5,
nsfwMode: false,
testMode: false,
devOnly: false,
category: 'economy',
data: new SlashCommandBuilder()
.setName('leaderboard')
.setDescription(
'Displays the leaderboard based on user balances and bank.'
),
userPermissions: [],
botPermissions: [],
cooldown: 5,
nsfwMode: false,
testMode: false,
devOnly: false,
category: 'economy',

run: async (client, interaction) => {
await interaction.deferReply();
run: async (client, interaction) => {
await interaction.deferReply();

try {
const balances = await fetchBalances();
if (balances.length === 0) {
return interaction.editReply('No users found in the leaderboard.');
}
try {
const balances = await fetchBalances();
if (balances.length === 0) {
return interaction.editReply('No users found in the leaderboard.');
}

const leaderboardEntries = await createLeaderboardEntries(client, balances);
const pages = createLeaderboardPages(leaderboardEntries);
const leaderboardEntries = await createLeaderboardEntries(
client,
balances
);
const pages = createLeaderboardPages(leaderboardEntries);

await pagination(interaction, pages);
} catch (error) {
console.error('Error in leaderboard command:', error);
await interaction.editReply('An error occurred while fetching the leaderboard.');
}
},
await pagination(interaction, pages);
} catch (error) {
console.error('Error in leaderboard command:', error);
await interaction.editReply(
'An error occurred while fetching the leaderboard.'
);
}
},
};

async function fetchBalances() {
return Balance.aggregate([
{
$project: {
userId: 1,
balance: 1,
bank: 1,
totalBalance: { $add: ['$balance', '$bank'] },
return Balance.aggregate([
{
$project: {
userId: 1,
balance: 1,
bank: 1,
totalBalance: { $add: ['$balance', '$bank'] },
},
},
},
{ $sort: { totalBalance: -1 } },
{ $limit: LEADERBOARD_LIMIT },
]).exec();
{ $sort: { totalBalance: -1 } },
{ $limit: LEADERBOARD_LIMIT },
]).exec();
}

async function createLeaderboardEntries(client, balances) {
return Promise.all(
balances.map(async (balance, index) => {
const userTag = await fetchUserTag(client, balance.userId);
return {
index: index + 1,
userTag,
totalBalance: balance.totalBalance,
wallet: balance.balance,
bank: balance.bank,
};
})
);
return Promise.all(
balances.map(async (balance, index) => {
const userTag = await fetchUserTag(client, balance.userId);
return {
index: index + 1,
userTag,
totalBalance: balance.totalBalance,
wallet: balance.balance,
bank: balance.bank,
};
})
);
}

async function fetchUserTag(client, userId) {
try {
const user = await client.users.fetch(userId);
return user.tag;
} catch {
return 'Unknown User';
}
try {
const user = await client.users.fetch(userId);
return user.tag;
} catch {
return 'Unknown User';
}
}

function createLeaderboardPages(entries) {
const pages = [];
for (let i = 0; i < entries.length; i += ITEMS_PER_PAGE) {
const pageEntries = entries.slice(i, i + ITEMS_PER_PAGE);
const embed = createPageEmbed(pageEntries, i, entries.length);
pages.push(embed);
}
return pages;
const pages = [];
for (let i = 0; i < entries.length; i += ITEMS_PER_PAGE) {
const pageEntries = entries.slice(i, i + ITEMS_PER_PAGE);
const embed = createPageEmbed(pageEntries, i, entries.length);
pages.push(embed);
}
return pages;
}

function createPageEmbed(entries, startIndex, totalEntries) {
const fields = entries.map((entry) => ({
name: `${getRankEmoji(entry.index)} **${entry.index}. ${entry.userTag}**`,
value: formatEntryValue(entry),
inline: true,
}));
const fields = entries.map((entry) => ({
name: `${getRankEmoji(entry.index)} **${entry.index}. ${entry.userTag}**`,
value: formatEntryValue(entry),
inline: true,
}));

return new EmbedBuilder()
.setTitle('πŸ† Leaderboard')
.addFields(fields)
.setColor(0xffd700)
.setFooter({
text: `Page ${Math.floor(startIndex / ITEMS_PER_PAGE) + 1} of ${Math.ceil(totalEntries / ITEMS_PER_PAGE)}`,
});
return new EmbedBuilder()
.setTitle('πŸ† Leaderboard')
.addFields(fields)
.setColor(0xffd700)
.setFooter({
text: `Page ${Math.floor(startIndex / ITEMS_PER_PAGE) + 1} of ${Math.ceil(totalEntries / ITEMS_PER_PAGE)}`,
});
}

function getRankEmoji(rank) {
const emojis = ['πŸ₯‡', 'πŸ₯ˆ', 'πŸ₯‰'];
return emojis[rank - 1] || 'πŸ…';
const emojis = ['πŸ₯‡', 'πŸ₯ˆ', 'πŸ₯‰'];
return emojis[rank - 1] || 'πŸ…';
}

function formatEntryValue(entry) {
return `Total: ${entry.totalBalance.toLocaleString()} coins\nWallet: ${entry.wallet.toLocaleString()} | Bank: ${entry.bank.toLocaleString()}`;
}
return `Total: ${entry.totalBalance.toLocaleString()} coins\nWallet: ${entry.wallet.toLocaleString()} | Bank: ${entry.bank.toLocaleString()}`;
}
Loading

0 comments on commit 57bb514

Please sign in to comment.