Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaderboard command issues #535

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 4 additions & 37 deletions src/commands/information/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,7 @@ module.exports = {
},
};

// Create a Map object to store cache entries
const cache = new Map();

async function getXpLeaderboard({ guild }, author, settings) {
// Create a cache key using the guild ID and the type of leaderboard
const cacheKey = `${guild.id}:xp`;

// Check if there is a cached result for this request
if (cache.has(cacheKey)) {
// Return the cached result if it exists
return cache.get(cacheKey);
}

if (!settings.stats.enabled) return "The leaderboard is disabled on this server";

Expand All @@ -112,20 +101,10 @@ async function getXpLeaderboard({ guild }, author, settings) {
.setDescription(collector)
.setFooter({ text: `Requested by ${author.tag}` });

// Store the result in the cache for future requests
cache.set(cacheKey, { embeds: [embed] });
return { embeds: [embed] };
}

async function getInviteLeaderboard({ guild }, author, settings) {
// Create a cache key using the guild ID and the type of leaderboard
const cacheKey = `${guild.id}:invite`;

// Check if there is a cached result for this request
if (cache.has(cacheKey)) {
// Return the cached result if it exists
return cache.get(cacheKey);
}

if (!settings.invite.tracking) return "Invite tracking is disabled on this server";

Expand All @@ -151,32 +130,22 @@ async function getInviteLeaderboard({ guild }, author, settings) {
.setColor(EMBED_COLORS.BOT_EMBED)
.setDescription(collector)
.setFooter({ text: `Requested by ${author.tag}` });

// Store the result in the cache for future requests
cache.set(cacheKey, { embeds: [embed] });

return { embeds: [embed] };
}

async function getRepLeaderboard(author) {
// Create a cache key using the user ID and the type of leaderboard
const cacheKey = `${author.id}:rep`;

// Check if there is a cached result for this request
if (cache.has(cacheKey)) {
// Return the cached result if it exists
return cache.get(cacheKey);
}

const lb = await getReputationLb(10);
if (lb.length === 0) return "There are no users in the leaderboard";

let collector = "";
for (let i = 0; i < lb.length; i++) {
try {
const user = await author.client.users.fetch(lb[i].member_id);
collector += `**#${(i + 1).toString()}** - ${escapeInlineCode(user.tag)} [${lb[i].rep}]\n`;
const user = await author.client.users.fetch(lb[i]._id);
collector += `**#${(i + 1).toString()}** - ${escapeInlineCode(user.tag)} [${lb[i].reputation.received}]\n`;
} catch (ex) {
collector += `**#${(i + 1).toString()}** - DeletedUser#0000 [${lb[i].rep}]\n`;
collector += `**#${(i + 1).toString()}** - DeletedUser#0000 [${lb[i].reputation.received}]\n`;
}
}

Expand All @@ -186,7 +155,5 @@ async function getRepLeaderboard(author) {
.setDescription(collector)
.setFooter({ text: `Requested by ${author.tag}` });

// Store the result in the cache for future requests
cache.set(cacheKey, { embeds: [embed] });
return { embeds: [embed] };
}