Skip to content

Commit 2c4d866

Browse files
authored
lc leaderboard command now general use (#239)
* add query for leetcode leaderboard * lint fix * lc leaderboard embed * lint fix * remove useless util function * lint is quite annoying vro * return error stack trace on reject * lc leaderboard command now general use
1 parent 759d745 commit 2c4d866

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const Command = require('../Command');
2+
const {
3+
queryLeetcodeLeaderboard
4+
} = require('../../APIFunctions/LeetcodeLeaderboard');
5+
const { EmbedBuilder } = require('discord.js');
6+
7+
const MONTHS = [
8+
'January', 'February', 'March', 'April', 'May', 'June',
9+
'July', 'August', 'September', 'October', 'November', 'December'
10+
];
11+
12+
module.exports = new Command({
13+
name: 'leetcode',
14+
description: 'Query the LeetCode Leaderboard',
15+
aliases: [],
16+
example: 's!leetcode',
17+
permissions: 'general',
18+
category: 'information',
19+
execute: async (message) => {
20+
const data = await queryLeetcodeLeaderboard();
21+
22+
if (!data || !data.leaderboard) {
23+
return message.channel.send('Error querying LeetCode Leaderboard.');
24+
}
25+
26+
const { leaderboard, month } = data;
27+
const top10 = leaderboard.slice(0, 10);
28+
29+
let maxNameLen = 'Username'.length;
30+
let maxPointsLen = 'Points'.length;
31+
32+
top10.forEach((user, index) => {
33+
let name = user.username;
34+
if (name.length > 10) {
35+
name = name.substring(0, 10);
36+
}
37+
user.displayName = name;
38+
39+
const rank = index + 1;
40+
const rankStr = rank < 10 ? ` ${rank}. ` : `${rank}. `;
41+
42+
if (rankStr.length + name.length > maxNameLen) {
43+
maxNameLen = rankStr.length + name.length;
44+
}
45+
46+
const pointsStr = user.points.toString();
47+
if (pointsStr.length > maxPointsLen) {
48+
maxPointsLen = pointsStr.length;
49+
}
50+
});
51+
52+
const gap = 4;
53+
54+
let description = '```ansi\n';
55+
56+
const title = 'LeetCode Leaderboard';
57+
const totalWidth = maxNameLen + gap + maxPointsLen;
58+
const titlePadding = Math.floor((totalWidth - title.length) / 2);
59+
description += `\u001b[0;33m${' '.repeat(titlePadding)}${title}\u001b[0m\n`;
60+
61+
const headerName = 'Username';
62+
const headerPoints = 'Points';
63+
const headerNamePadding = ' '.repeat(maxNameLen - headerName.length + gap);
64+
const headerPointsPadding = ' '.repeat(maxPointsLen - headerPoints.length);
65+
66+
description += `\u001b[0;37m${headerName}${headerNamePadding}` +
67+
`${headerPointsPadding}${headerPoints}\u001b[0m\n`;
68+
69+
top10.forEach((user, index) => {
70+
const rank = index + 1;
71+
const name = user.displayName;
72+
const points = user.points.toString();
73+
74+
const rankStr = rank < 10 ? ` ${rank}. ` : `${rank}. `;
75+
const nameCol = `${rankStr}${name}`;
76+
const namePadding = ' '.repeat(maxNameLen - nameCol.length + gap);
77+
const pointsPadding = ' '.repeat(maxPointsLen - points.length);
78+
79+
let colorCode = '\u001b[0;37m';
80+
if (rank === 1) colorCode = '\u001b[0;33m';
81+
else if (rank === 2) colorCode = '\u001b[0;31m';
82+
else if (rank === 3) colorCode = '\u001b[0;34m';
83+
84+
description += `${colorCode}${nameCol}${namePadding}` +
85+
`${pointsPadding}${points}\u001b[0m\n`;
86+
});
87+
88+
const monthText = `Month: ${MONTHS[month]}`;
89+
const monthPadding = Math.floor((totalWidth - monthText.length) / 2);
90+
description += `\u001b[0;33m${' '.repeat(monthPadding)}` +
91+
`${monthText}\u001b[0m\n`;
92+
93+
description += '```';
94+
95+
const embed = new EmbedBuilder()
96+
.setColor('#000000')
97+
.setDescription(description);
98+
99+
return message.channel.send({ embeds: [embed] });
100+
},
101+
});

0 commit comments

Comments
 (0)