forked from GeorgeCiesinski/poke-guesser-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.js
59 lines (54 loc) · 1.8 KB
/
leaderboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { ApplicationCommandType, ApplicationCommandOptionType } = require('discord.js');
const util = require("./util");
// Sanitizes leaderboard by removing non-existent users which might break other functions
function sanitizeLeaderboard(client, leaderboard) {
// Returns a promise
return new Promise(function(resolve, reject) {
let promises = [];
// Iterate through leaderboard object
for (const [userId, score] of Object.entries(leaderboard)) {
// Retrieve user from guild, or null if doesn't exist
objLength = Object.keys(leaderboard).length;
promises.push(findUser(client, userId).then(user => {
// If user object is null, delete the key from leaderboard
if (!user) {
console.log(`Removing User ID ${userId} from leaderboard.`);
delete leaderboard[userId];
}
}));
}
Promise.all(promises).then(values => {
// Resolves promise and returns leaderboard once fake users removed
resolve(leaderboard);
});
});
}
// DEBUGGING - Adds dummy users to leaderboard - for testing leaderboard embed with different numbers of users
function generateLeaderboard(leaderboard) {
for (i=0; i<20; i++) {
let userName = `user${i+1}`;
let score = Math.floor(Math.random() * 10);
leaderboard[userName] = score;
}
return leaderboard;
}
function leaderboard(interaction) {
//const type = interaction.options.getString('type');
let title = '';
let description = '';
// returnEmbed(title, message, image=null)
interaction.reply({
embeds: [util.returnEmbed(title, description)],
ephemeral: true
});
}
function getRegisterObject() {
return {
name: 'leaderboard',
description: 'Shows the Leaderboard',
type: ApplicationCommandType.ChatInput
};
}
// Exports each function separately
module.exports.leaderboard = leaderboard;
module.exports.getRegisterObject = getRegisterObject;