-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
165 lines (135 loc) Β· 4.67 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require('dotenv').config();
const Discord = require('discord.js');
const client = new Discord.Client();
const leetCodeDatabase = {};
const ryanUserId = '169572455218806784';
function printDatabase(msg) {
if (Object.entries(leetCodeDatabase).length === 0) {
msg.channel.send(`No one has submitted any LeetCode submissions. Sad! π€`);
return;
}
for (const entry in leetCodeDatabase) {
printDatabaseForUser(msg, entry);
}
}
function printDatabaseForUser(msg, userId) {
let user = client.users.find(usr => usr.id === userId);
if (!user) {
console.warn(`β οΈ ${userId} does not exist in this server! β οΈ`);
return;
}
if (leetCodeDatabase[userId] == null) {
msg.channel.send(`${user.username} has not logged any LeetCode problems. Shame! π`);
return;
}
let finalString = `${user.username} has completed a total of:\n`;
for (const submissions in leetCodeDatabase[userId]) {
let submissionCount = leetCodeDatabase[userId][submissions];
let emoji;
switch (submissions) {
case 'easy':
emoji = 'π’';
break;
case 'medium':
emoji = 'π‘';
break;
case 'hard':
emoji = 'π΄';
break;
}
finalString += `${emoji} ${submissionCount} ${submissions} problem${submissionCount != 1 ? 's' : ''}\n`;
}
msg.channel.send(finalString);
}
function clearDataForUser(msg, userId) {
let user = client.users.find(usr => usr.id === userId);
if (!user) {
msg.channel.send(`β οΈ That person does not exist in this server! β οΈ`);
return;
}
if (leetCodeDatabase[userId] == null) {
msg.channel.send(`${user.username} has no data to clear! π€`);
return;
}
leetCodeDatabase[userId] = null;
msg.channel.send(`Cleared data for ${user.username}! π`);
}
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
client.on('message', msg => {
// Return so that the bot doesn't respond to its own messages.
if (client.user.id === msg.author.id) return;
const message = msg.content;
if (message.startsWith('!lc')) {
const command = message.substring(message.indexOf(' ') + 1);
if (command.startsWith('record')) {
handleLeetCodeArgs(msg, command.substring(command.indexOf(' ') + 1));
} else if (command.startsWith('check')) {
let userId = command.substring(command.indexOf(' ') + 1);
if (userId !== 'check') {
userId = userId.replace(/\D/g, '');
printDatabaseForUser(msg, userId);
} else {
printDatabase(msg);
}
} else if (command.startsWith('clear')) {
if (msg.author.id !== ryanUserId) {
msg.channel.send(`π¨π¨π¨ Unauthorized user ${msg.author} has tried to clear database. Alerting <@${ryanUserId}>. π¨π¨π¨`);
return;
}
let userId = command.substring(command.indexOf(' ') + 1);
if (userId !== 'clear') {
userId = userId.replace(/\D/g, '');
clearDataForUser(msg, userId);
} else {
msg.channel.send(`Must provide user when attempting to clear data! π§`);
}
} else {
msg.channel.send(`Command \`${command}\` is undefined. π€`);
}
}
});
function handleLeetCodeArgs(msg, args) {
const command = args.split(' ');
let finalString = '';
command.forEach(arg => finalString += handleSingleLeetCodeArg(msg, arg));
msg.channel.send(finalString);
}
function handleSingleLeetCodeArg(msg, arg) {
let resultString;
if (arg.startsWith('easy') || arg.startsWith('medium') || arg.startsWith('hard')) {
resultString = `${handleLeetCodeSubmission(msg, arg)}\n`;
} else {
resultString = `Arg ${arg} is not defined. π€\n`;
}
return resultString;
}
function handleLeetCodeSubmission(msg, arg) {
let resultString = `${msg.author.username} finished `;
let submissionCount = 0;
if (arg.startsWith('easy')) {
submissionCount = arg.substring('easy'.length);
resultString += `${submissionCount} easy problem${submissionCount != 1 ? 's' : ''} π’`;
setDatabaseResults(msg.author.id, 'easy', submissionCount);
} else if (arg.startsWith('medium')) {
submissionCount = arg.substring('medium'.length);
resultString += `${submissionCount} medium problem${submissionCount != 1 ? 's' : ''} π‘`;
setDatabaseResults(msg.author.id, 'medium', submissionCount);
} else {
submissionCount = arg.substring('hard'.length);
resultString += `${submissionCount} hard problem${submissionCount != 1 ? 's' : ''} π΄`;
setDatabaseResults(msg.author.id, 'hard', submissionCount);
}
return resultString;
}
function setDatabaseResults(authorId, difficulty, submissionCount) {
if (leetCodeDatabase[authorId] == null) {
leetCodeDatabase[authorId] = {};
leetCodeDatabase[authorId].easy = 0;
leetCodeDatabase[authorId].medium = 0;
leetCodeDatabase[authorId].hard = 0;
}
leetCodeDatabase[authorId][difficulty] += Number(submissionCount);
}
client.login(process.env.BOT_TOKEN);