-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
408 lines (333 loc) · 12.3 KB
/
index.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import DiscordJS, { Intents, MessageEmbed, TextChannel } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()
// i probably shouldn't keep everything in a single index.ts file lol. i'll fix this later
const client = new DiscordJS.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
})
let scoreboard: any = {};
let config: any = {};
client.on('ready', () => {
loadConfigFromFile();
loadScoreBoardFromFile();
})
const loadConfigFromFile = () => {
const fs = require('fs');
const path = 'config.json';
if (fs.existsSync(path)) {
//file exists
fs.readFile(path, (err: any, data: any) => {
console.log('Config found - loading into memory');
if (err) {
throw err;
}
config = JSON.parse(data.toString());
console.log(config);
// if no timezone, set default to LA
if (!config.timezone) {
config.timezone = 'America/Los_Angeles'
writeConfigToFile();
}
// if a channel id is present in config, start scheduler for midnight notification in channel
if (config.channelid) {
client.channels.fetch(config.channelid);
setNotificationSchedule();
}
});
} else {
console.log('No config found - creating blank config');
writeConfigToFile();
}
}
const writeConfigToFile = () => {
const fs = require('fs');
const path = 'config.json';
fs.writeFile(path, JSON.stringify(config), (err: any) => {
if (err) {
throw err;
}
console.log('Config written to file')
})
}
const loadScoreBoardFromFile = () => {
const fs = require('fs');
const path = 'score.json';
if (fs.existsSync(path)) {
//file exists
fs.readFile(path, (err: any, data: any) => {
console.log('Scoreboard found - loading into memory');
if (err) {
throw err;
}
scoreboard = JSON.parse(data.toString());
console.log(scoreboard);
for (const id in scoreboard) {
client.users.fetch(id)
}
console.log('Bot Ready!')
});
} else {
console.log('No scoreboard found - creating blank scoreboard');
writeScoreBoardToFile();
}
}
const writeScoreBoardToFile = () => {
const fs = require('fs');
const path = 'score.json';
fs.writeFile(path, JSON.stringify(scoreboard), (err: any) => {
if (err) {
throw err;
}
console.log('Scoreboard written to file')
})
}
const updateScoreBoardScore = (userid: any, points: number, increment: boolean) => {
// create a new scoreboard entry
if (!scoreboard[userid]) {
scoreboard[userid] = []
scoreboard[userid][0] = points;
scoreboard[userid][1] = 1;
} else {
scoreboard[userid][0] += points;
if (increment)
scoreboard[userid][1]++;
}
writeScoreBoardToFile();
return scoreboard[userid];
}
const setScoreBoardAttempts = (userid: any, attempts: number) => {
scoreboard[userid][1] = attempts;
writeScoreBoardToFile();
return scoreboard[userid];
}
const setNotificationChannel = (channelid: any) => {
if (channelid) {
config.channelid = channelid;
} else {
delete config.channelid;
}
writeConfigToFile();
}
const setNotificationTimezone = (timezone: any) => {
if (timezone) {
config.timezone = timezone;
} else {
config.timezone = 'America/Los_Angeles';
}
writeConfigToFile();
}
const setNotificationSchedule = () => {
// midnight post temp
const schedule = require('node-schedule');
const rule = new schedule.RecurrenceRule();
rule.hour = 0;
rule.minute = 0;
rule.tz = config.timezone;
const job = schedule.scheduleJob(rule, function () {
if (config.channelid) {
const dailyEmbed = generateScoreBoardEmbed('NEW WORDLE CHALLENGE');
(client.channels.cache.get(config.channelid) as TextChannel).send({ content: dailyEmbed.userListEmbed.replace(/\n/g, ' ') });
(client.channels.cache.get(config.channelid) as TextChannel).send({ embeds: [dailyEmbed.embed] });
}
});
console.log(`Bot schedule to send message at ${rule.hour}:${rule.minute} ${rule.tz}`);
}
const generateScoreBoardEmbed = (title: string) => {
// sort results
let scoreArray = []
for (const key in scoreboard) {
scoreArray.push([key, scoreboard[key][0]]);
}
scoreArray = scoreArray.sort(sort2d);
let userListEmbed = '';
let scoreListEmbed = '';
let averageListEmbed = '';
let attemptsListEmbed = '';
let allPlayersList = [];
let topPlayersList = [];
let topScoreCount = 0;
let topScore = -1;
let maxTopPlayers = 1;
for (const entry of scoreArray) {
userListEmbed += `${client.users.cache.get(entry[0])}\n`;
// get top 3 places
if (topScoreCount < maxTopPlayers) {
if (topScore == -1) {
topScore = entry[1];
topPlayersList.push(client.users.cache.get(entry[0]));
} else if (parseInt(entry[1]) == topScore) {
topPlayersList.push(client.users.cache.get(entry[0]));
}
else if (parseInt(entry[1]) != topScore) {
topScore = entry[1];
topScoreCount++;
if (topScoreCount < maxTopPlayers) {
topPlayersList.push(client.users.cache.get(entry[0]));
}
}
}
allPlayersList.push(entry[0]);
scoreListEmbed += `${entry[1]}\n`;
attemptsListEmbed += `${scoreboard[entry[0]][1]}\n`;
let avg = Math.abs((parseFloat(entry[1]) / parseFloat(scoreboard[entry[0]][1])) - 7).toFixed(1);
// averageListEmbed += `${(parseFloat(entry[1]) / parseFloat(scoreboard[entry[0]][1])).toFixed(2)}\n`;
averageListEmbed += `${avg.replace('.0', '')}/6\n`;
}
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`:green_square: :green_square: :green_square: ${title} :green_square: :green_square: :green_square:`)
.setURL('https://www.powerlanguage.co.uk/wordle/')
.setDescription('https://www.powerlanguage.co.uk/wordle/')
.addFields(
{ name: 'User', value: userListEmbed, inline: true },
{ name: 'Score', value: scoreListEmbed, inline: true },
{ name: 'Avg', value: averageListEmbed, inline: true }
);
return {
embed: embed,
userListEmbed: userListEmbed,
topPlayersList: topPlayersList,
allPlayersList: allPlayersList
};
}
const setChampRoles = () => {
// const scoreBoardEmbed = generateScoreBoardEmbed('');
// const topPlayersList = scoreBoardEmbed.topPlayersList;
// const allPlayersList = scoreBoardEmbed.allPlayersList;
// let role = client.guilds.cache.get()
// for (const player of allPlayersList) {
// console.log(player);
// client.guilds
// }
// console.log(topPlayersList);
}
client.on('messageCreate', (message) => {
// Check if admin
const isAdmin = message.member?.permissions.has("ADMINISTRATOR");
// Check for Wordle Score
const wordleRegex = /Wordle \d{3} ([\dX])\/6\*?\n{0,2}[⬛🟩🟨⬜🟧🟦]{5}/;
const wordleMessage = message.content.match(wordleRegex);
if (wordleMessage) {
console.log(wordleMessage);
console.log(message.author);
// Check if X/6
let wordleScore;
if (wordleMessage[1] == 'X') {
wordleScore = 0;
} else {
wordleScore = 7 - parseInt(wordleMessage[1]);
}
const updatedScore = updateScoreBoardScore(message.author.id, wordleScore, true);
// reply with score
message.reply({
content: `${wordleScore} points for ${message.author} (${updatedScore[0]} total pts, ${updatedScore[1]} attempts)`
})
}
// leaderboard command
if (message.content === '!w scores') {
const leaderboardEmbed = generateScoreBoardEmbed('LEADERBOARD');
message.channel.send({ embeds: [leaderboardEmbed.embed] });
}
// fun LOTR stories
if (message.content === '!w story') {
const randomQuote = require('random-lotr-movie-quote');
// console.log(randomQuote());
message.channel.send({ content: `${randomQuote().char}: ${randomQuote().dialog}` });
message.delete();
}
// admin commands
if (isAdmin) {
// just debug stuff
if (message.content === '!w debug') {
const exampleEmbed = generateScoreBoardEmbed('NEW WORDLE CHALLENGE');
message.channel.send({ content: exampleEmbed.userListEmbed.replace(/\n/g, ' ') });
message.channel.send({ embeds: [exampleEmbed.embed] });
setChampRoles();
}
// manual update score
if (message.content.includes('!w x')) {
const updateRegex = /!(\d+)> *([+-]\d+)/
const updateParse = message.content.match(updateRegex);
if (updateParse) {
console.log(updateParse);
const userid = updateParse[1];
const score = parseInt(updateParse[2]);
const updatedScore = updateScoreBoardScore(userid, score, false);
// reply with score
message.channel.send({
content: `[Admin] ${score} points for ${client.users.cache.get(userid)} (${updatedScore[0]} total pts, ${updatedScore[1]} attempts)`
})
}
}
// manual attempt set
if (message.content.includes('!w attempts')) {
console.log(message.content);
const updateRegex = /!(\d+)> *(\d+)/
const updateParse = message.content.match(updateRegex);
if (updateParse) {
console.log(updateParse);
const userid = updateParse[1];
const attempts = parseInt(updateParse[2]);
const updatedAttempts = setScoreBoardAttempts(userid, attempts);
// reply with score
message.channel.send({
content: `[Admin] Set ${attempts} attempts for ${client.users.cache.get(userid)} (${updatedAttempts[0]} total pts, ${updatedAttempts[1]} attempts)`
})
}
}
// configure wordle channel
if (message.content.includes('!w channel')) {
console.log(message.content);
const channelRegex = /#(\d+)/;
const channelParse = message.content.match(channelRegex);
if (channelParse) {
const channelid = channelParse[1];
setNotificationChannel(channelid);
message.reply({
content: `[Config] Wordle channel set to ${client.channels.cache.get(channelid)}`
})
} else if (message.content.includes('remove')) {
setNotificationChannel(null);
message.reply({
content: `[Config] Wordle channel removed`
})
}
// todo print error if no channel provided
}
// configure timezone
if (message.content.includes('!w tz')) {
console.log(message.content);
const tzRegex = /tz (.+)/;
const tzParse = message.content.match(tzRegex);
console.log(tzParse);
if (tzParse) {
const tz = tzParse[1];
if (tz == 'remove') {
setNotificationTimezone(null);
message.reply({
content: `[Config] Wordle timezone reset to ${config.timezone}`
})
} else {
setNotificationTimezone(tz);
message.reply({
content: `[Config] Wordle timezone set to ${tz}`
})
}
}
// todo print error if no valid tz provided
}
// TODO create command to designate a wordle champ role
}
})
const sort2d = (a: any, b: any) => {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? 1 : -1;
}
}
client.login(process.env.TOKEN);