-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
142 lines (117 loc) · 5.2 KB
/
bot.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { Client, Events, EmbedBuilder, GatewayIntentBits, ShardEvents, WebSocketShardEvents } = require('discord.js');
const axios = require('axios');
const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const prefix = ['!p', '!problem'];
const levels = ['easy', 'medium', 'hard'];
const problems = [];
const leetcodeProblemUrl = 'https://leetcode.com/problems/';
const leetcodeApiUrl = 'https://leetcode.com/api/problems/all/';
const completedProblems = [];
const owner = process.env.DISCORD_BOT_OWNERID;
let totalProblems = 0;
function Problem(problemObject) {
this.id = problemObject.stat.question_id;
this.title = problemObject.stat.question__title;
this.titleSlug = problemObject.stat.question__title_slug;
this.difficulty = problemObject.difficulty.level === 3 ? 'Hard' : problemObject.difficulty.level === 2 ? 'Medium' : 'Easy';
this.paidOnly = problemObject.paid_only;
this.description = `Problem ID: ${this.id}\nTitle: ${this.title}\nSlug Title: ${this.titleSlug}\nDifficulty: ${this.difficulty}\nIs Paid? ${this.paidOnly}`;
}
function getProblem(data) {
const randProblem = Math.floor(Math.random() * data.length);
const problem = data[randProblem];
if (completedProblems.includes(problem.id)) {
getProblem(data);
}
completedProblems.push(problem.id);
return problem;
}
function sendProblems(msg, difficulty, amount = 1) {
let data = [...problems];
if (difficulty != -1) {
data = data.filter(x => x.difficulty.toLowerCase() === difficulty.toLowerCase());
}
for (let i = 0; i < amount; i++) {
const problem = getProblem(data);
const problemUrl = `${leetcodeProblemUrl}${problem.titleSlug}/`;
const embed = new EmbedBuilder()
.setTitle(problem.title)
.setColor('#f89f1b')
.setThumbnail('https://leetcode.com/static/images/LeetCode_logo_rvs.png')
.setDescription(`${problem.difficulty} - ${problem.title}`)
.setURL(problemUrl);
msg.channel.send({ embeds: [embed]});
}
}
const sanitizeAndConvertToArray = (value) => value.replace(/[^0-9,]/g, '').split(',').map(Number);
function removeDuplicates(value) {
const results = value.filter(x => !completedProblems.includes(x));
return [...results];
}
function sendOwnerCachedValues() {
console.log('sending owner cached values');
client.users.fetch(owner, false).then((user) => {
user.send(`Completed problem ids:\n \`\`\`[${completedProblems}]\`\`\``);
});
}
axios.get(leetcodeApiUrl)
.then((resp) => {
totalProblems = resp.data.num_total;
resp.data.stat_status_pairs.filter(x => !x.paid_only).forEach((problem) => { problems.push(new Problem(problem)); });
}).catch((err) => { console.log(err); });
bot.on(Events.ClientReady, () => { console.log(`Logged in as ${bot.user.username}`); });
bot.on(Events.Error, (err) => { sendOwnerCachedValues(); console.error(err); });
bot.on(ShardEvents.Disconnect, (err) => { sendOwnerCachedValues(); console.log(err); });
bot.on(WebSocketShardEvents.Destroyed, (err) => { sendOwnerCachedValues(); console.log(err); });
bot.on(Events.MessageCreate, (msg) => {
if (msg.author.bot) return;
if (msg.content === '!ping') {
bot.createMessage(msg.channel.id, 'Pong!');
}
if (msg.content.startsWith(prefix[0]) || msg.content.startsWith(prefix[1])) {
const args = msg.content.slice(prefix.length).trim().split(' ');
let difficulty = -1;
let amount = 1;
if (typeof args[0] != undefined) {
if (args[0].toLowerCase() === 'help') {
msg.channel.send(
'```Usage:\n\n\t!problem | !p (without args) - gives you a random problem of any difficulty either paid/free.' +
'\n\nAdding difficulty modifiers:\n\n\t!problem | !p <easy | medium | hard> - lets you pick a random free problem of the chosen difficulty.' +
'\n\nAdding amount modifier:\n\n\t!problem | !p <easy | medium | hard> <1-4> - lets you pick a specified number of random free problem (max 4) of the chosen difficulty.```',
);
return;
}
if (args[0].toLowerCase() === 'cache') {
msg.channel.send(`Completed problem ids:\n \`\`\`[${completedProblems}]\`\`\``);
return;
}
if (args[0].toLowerCase() === 'set') {
const argument = sanitizeAndConvertToArray(args[1]);
if (argument != null && argument.length > 0) {
const newProblems = removeDuplicates(argument);
if (newProblems.length > 0) {
completedProblems.push(...newProblems);
}
msg.channel.send(`Set new completed problem ids:\n \`\`\`[${completedProblems}]\`\`\``);
} else {
msg.channel.send('Improper arugment provided.');
}
return;
}
if (levels.indexOf(args[0].toLowerCase()) > -1 && Number(args[0]) !== 'NaN') {
difficulty = args[0].toLowerCase();
}
if (args[1] != null && levels.indexOf(args[1].toLowerCase()) > -1 && Number(args[1]) !== 'NaN') {
difficulty = args[1].toLowerCase();
}
if (Number(args[0])) {
amount = Math.min(Number(args[0]), 4);
}
if (Number(args[1])) {
amount = Math.min(Number(args[1]), 4);
}
}
sendProblems(msg, difficulty, amount);
}
});
bot.login(process.env.DISCORD_BOT_TOKEN);