forked from talrnu/tgd-streakbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
executable file
·492 lines (439 loc) · 15 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
const { Client, GatewayIntentBits, ChannelType } = require("discord.js");
const schedule = require("node-schedule");
const lowdb = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const adapter = new FileSync("db.json");
const db = lowdb(adapter);
// sanity check our required configuration
require("dotenv").config();
if (!process.env.GUILD_ID) throw new Error("missing GUILD_ID env var");
if (!process.env.BOT_TOKEN) throw new Error("missing BOT_TOKEN env var");
// connect to discord
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
// setup our lowdb database
db.defaults({
users: [],
}).write();
const ONE_DAY = new Date(1000 * 60 * 60 * 24);
const dayStartHour = process.env.DAY_START_HOUR
? parseInt(process.env.DAY_START_HOUR)
: 0;
const dayStartMinute = process.env.DAY_START_MINUTE
? parseInt(process.env.DAY_START_MINUTE)
: 0;
const morningAnnouncementHour = process.env.MORNING_ANNOUNCEMENT_HOUR
? parseInt(process.env.MORNING_ANNOUNCEMENT_HOUR)
: 7;
const morningAnnouncementMinute = process.env.MORNING_ANNOUNCEMENT_MINUTE
? parseInt(process.env.MORNING_ANNOUNCEMENT_MINUTE)
: 0;
const midDayReminderHour = process.env.MID_DAY_REMINDER_HOUR
? parseInt(process.env.MID_DAY_REMINDER_HOUR)
: 12;
const midDayReminderMinute = process.env.MID_DAY_REMINDER_MINUTE
? parseInt(process.env.MID_DAY_REMINDER_MINUTE)
: 0;
const midWeekSummaryHour = process.env.MID_WEEK_SUMMARY_HOUR
? parseInt(process.env.MID_WEEK_SUMMARY_HOUR)
: 13;
const midWeekSummaryMinute = process.env.MID_WEEK_SUMMARY_MINUTE
? parseInt(process.env.MID_WEEK_SUMMARY_MINUTE)
: 0;
const midWeekDayOfWeek = process.env.MID_WEEK_DAY_OF_WEEK
? parseInt(process.env.MID_WEEK_DAY_OF_WEEK)
: 3;
const channelName = process.env.CHANNEL_NAME || "standups";
let morningAnnouncementJob;
let midDayReminderJob;
let reconnectJob;
let midweekJob;
const scheduleJobs = () => {
if (!morningAnnouncementJob) {
console.log("Scheduling day start job...");
morningAnnouncementJob = schedule.scheduleJob(
`00 ${morningAnnouncementMinute
.toString()
.padStart(2, "0")} ${morningAnnouncementHour
.toString()
.padStart(2, "0")} * * *`,
() => {
console.log("Broadcasting morning announcement...");
broadcastMorningAnnouncement();
}
);
}
/*
if (!midDayReminderJob) {
console.log("Scheduling reminder job...");
midDayReminderJob = schedule.scheduleJob(
`00 ${midDayReminderMinute
.toString()
.padStart(2, "0")} ${midDayReminderHour
.toString()
.padStart(2, "0")} * * *`,
() => {
console.log("Broadcasing reminder...");
broadcastReminder();
}
);
}
*/
/*if (!reconnectJob) {
console.log("Scheduling reconnect job...");
reconnectJob = schedule.scheduleJob("00 05 * * * *", () => {
console.log("Performing scheduled reconnect...");
disconnect();
});
}*/
if (!midweekJob) {
console.log("Scheduling midweek job...");
midweekJob = schedule.scheduleJob(
`00 ${midWeekSummaryMinute
.toString()
.padStart(2, "0")} ${midWeekSummaryHour
.toString()
.padStart(2, "0")} * * ${midWeekDayOfWeek}`,
() => {
console.log("Broadcasing midweek summary...");
broadcastSummary();
}
);
}
};
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag} - ${new Date().toUTCString()}`);
debugGuilds();
// debugChannels();
scheduleJobs();
console.log("Client startup complete.");
});
client.on("error", (error) => {
console.error(error);
});
client.on("disconnect", (msg, code) => {
if (code === 0) return console.error(msg);
console.log("Graceful disconnect occurred.");
disconnectCleanup();
connect();
});
client.on("messageCreate", (message) => {
const currentChannelName = message.channel.name;
const currentChannelType = message.channel.type;
const channelDebug =
currentChannelType == "dm" ? "DM" : `#${currentChannelName}`;
if (message.author.bot) {
// console.debug("ignoring message from bot", message.author.name);
return;
} else if (message.guildId !== process.env.GUILD_ID) {
/*
console.debug("ignoring message from different server than configured", {
ourGuildId: process.env.GUILD_ID,
messageGuildId: message.guildId,
});
*/
return;
} else if (message.channel.name !== channelName) {
/*
console.debug("ignoring message not in our standup channel", {
ourChannelName: channelName,
messageChannelName: message.channel.name,
});
*/
return;
}
// test out various announcements
if (message.content.startsWith("!")) {
if (message.content == "!summary") {
broadcastSummary();
} else if (message.content == "!gm") {
broadcastMorningAnnouncement();
} else if (message.content == "!reminder") {
broadcastReminder();
} else if (message.content == "!help" || message.content == "!debug") {
standupChannel().send(`current debug commands:
!summary
!gm
!reminder
!help
active guild: ${process.env.GUILD_ID}
active channel: #${channelName}
`);
} else {
standupChannel().send(
`sorry, I don't understand the command \`${message.content}\`. please try again.`
);
}
return;
}
// lastly,
console.log(`Processing message received from ${message.author.username}`);
processMessageForStreak(message);
});
const disconnectCleanup = () => {
console.log("Cleaning up after disconnect...");
dayStartJob.cancel();
dayStartJob = null;
midDayReminderJob.cancel();
midDayReminderJob = null;
reconnectJob.cancel();
reconnectJob = null;
};
const disconnect = async () => {
console.log("Disconnecting...");
await client.destroy();
};
const connect = async () => {
console.log("Connecting...");
await client.login(process.env.BOT_TOKEN);
};
const debugGuilds = () => {
console.log(
"debugGuilds",
client.guilds.cache.map((guild) => [guild.id, guild.name])
);
};
const debugChannels = () => {
console.log(
"debugChannels",
client.channels.cache.map((channel) => [
channel.guildId,
channel.id,
channel.name,
channel.type,
])
);
};
const standupChannel = () => {
return client.channels.cache.find(
(c) =>
c.guildId == process.env.GUILD_ID &&
c.name === channelName &&
c.type === ChannelType.GuildText
);
};
const broadcastMorningAnnouncement = () => {
console.log("Sending morning announcement...");
const announcement = `Let the new day begin! Post your standup to start or continue your daily streak. Check the pinned messages for a full introduction. ${getUsersWhoPostedYesterday()}`;
standupChannel().send(announcement);
};
const broadcastReminder = () => {
console.log("Sending reminder announcement...");
const announcement = `The day is half done! Don't forget to post an update for the day. A quick note about what you plan to do tomorrow is great too. ${getUsersWhoCouldLoseTheirStreak()}`;
standupChannel().send(announcement);
};
const broadcastSummary = () => {
console.log("Sending summary...");
// const announcement = `We're halfway through the week! Time for a weekly summary.${getUsersWhoPostedInThePastWeek()}`;
const announcement = `Summary for this week:\n${getUsersWhoPostedInThePastWeek()}`;
standupChannel().send(announcement);
};
const getUsersWhoPostedYesterday = () => {
console.log("getUsersWhoPostedYesterday()");
let listText = "";
const users = db.get("users").value();
//console.log(`users: ${JSON.stringify(users)}`);
const activeStreakUsers = users.filter(userStreakLastUpdatedYesterday);
if (activeStreakUsers.length > 0) {
listText += "\nCurrent running streaks:";
activeStreakUsers.forEach((user) => {
// client.users.find can return null if the user hasn't been cached by the client yet.
// const username = user.mentionsEnabled
// ? client.users.find((u) => u.id === user.userID) || user.username
// : user.username;
const username = user.username;
listText += `\n\t${username}: ${user.streak} (best: ${user.bestStreak})`;
});
}
return listText;
};
const getUsersWhoCouldLoseTheirStreak = () => {
console.log("getUsersWhoCouldLoseTheirStreak()");
let listText = "";
const users = db.get("users").value();
//console.log(`users: ${JSON.stringify(users)}`);
const atRiskUsers = users.filter(userStreakStillNeedsUpdatingToday);
if (atRiskUsers.length > 0) {
listText +=
"\nThese users still need to post today if they want to keep their current streak alive:";
atRiskUsers.forEach((user) => {
// client.users.find can return null if the user hasn't been cached by the client yet.
// const username = user.mentionsEnabled
// ? client.users.find((u) => u.id === user.userID) || user.username
// : user.username;
const username = user.username;
listText += `\n\t${username}: ${user.streak} (best: ${user.bestStreak})`;
});
}
return listText;
};
const getUsersWhoPostedInThePastWeek = () => {
console.log("getUsersWhoPostedInThePastWeek()");
let listText = "";
const users = db.get("users").value();
//console.log(`users: ${JSON.stringify(users)}`);
const pastWeekUsers = users.filter(userStreakUpdatedInPastWeek);
if (pastWeekUsers.length > 0) {
listText += "\nUsers who have posted in the past week:";
pastWeekUsers.forEach((user) => {
listText += `\n\t${user.username} (best streak: ${user.bestStreak})`;
});
listText += "\nKeep up the good work!";
} else {
listText =
"\nNo users have posted in the past week! I'll have an existential meltdown now.";
}
return listText;
};
const processMessageForStreak = (msg) => {
const dbUser = getOrCreateDBUser(msg);
if (userStreakNotAlreadyUpdatedToday(dbUser.value())) {
console.log("\tUpdating streak...");
addToStreak(msg, dbUser);
} else {
console.log(
`Extraneous message posted to ${msg.channel.name} by ${
msg.author.username
} (${msg.author.tag}) at ${new Date().toISOString()}`
);
const reply = `howdy partner, it looks like you posted multiple times to the server's #${msg.channel.name} channel today. We'd like to avoid overshadowing daily status updates with other conversations, so we'd appreciate it if you would move this conversation to a thread or another channel. If you want to update your standup, just edit the existing post`;
// not everybody can accept DMs, so post as a public reply
// msg.author.send(reply)
msg.reply(reply).catch((e) => console.error("error replying", e));
}
};
const getOrCreateDBUser = (msg) => {
let dbUser = db.get("users").find({ userID: msg.author.id });
if (dbUser.value()) {
dbUser.assign({ username: msg.author.username }).write();
console.log(`Existing user updated: ${JSON.stringify(dbUser.value())}`);
} else {
const newUser = {
userID: msg.author.id,
username: msg.author.username,
messagesEnabled: true,
mentionsEnabled: true,
};
const dbUsers = db.get("users");
dbUsers.push(newUser).write();
dbUser = dbUsers.find({ userID: newUser.userID });
console.log(`New user created: ${JSON.stringify(dbUser.value())}`);
}
return dbUser;
};
const userStreakNotAlreadyUpdatedToday = (user) => {
console.log(`userStreakNotAlreadyUpdatedToday(${user.username})`);
if (!user.streak) {
console.log("\tThis user is starting their first streak");
return true;
}
const mostRecentDayStart = getMostRecentDayStart();
const userLastUpdate = new Date(user.lastUpdate);
// console.log(
// `\tMost recent day start: ${mostRecentDayStart.toUTCString()}, user last update: ${userLastUpdate.toUTCString()}`
// );
return userLastUpdate < mostRecentDayStart;
};
const userStreakLastUpdatedYesterday = (user) => {
console.log(`userStreakLastUpdatedYesterday(${user.username})`);
if (!user.lastUpdate) {
console.log("\tUser's first streak! No last update date.");
return false;
}
const mostRecentDayStart = getMostRecentDayStart();
const userLastUpdate = new Date(user.lastUpdate);
const timeBeforeDayStart =
mostRecentDayStart.getTime() - userLastUpdate.getTime();
console.log(`\tMost recent day start: ${mostRecentDayStart.toUTCString()};`);
console.log(`\tuser last update: ${userLastUpdate.toUTCString()};`);
console.log(
`\ttime between last update and most recent day start: ${timeBeforeDayStart};`
);
console.log(`\tOne day is ${ONE_DAY};`);
const didLastUpdateYesterday =
timeBeforeDayStart > 0 && timeBeforeDayStart < ONE_DAY;
console.log(`\tResult: ${didLastUpdateYesterday}`);
return didLastUpdateYesterday;
};
const userStreakStillNeedsUpdatingToday = (user) => {
return (
userStreakLastUpdatedYesterday(user) &&
userStreakNotAlreadyUpdatedToday(user)
);
};
const userStreakUpdatedInPastWeek = (user) => {
const userLastUpdate = new Date(user.lastUpdate);
const timeSinceLastUpdate = new Date() - userLastUpdate;
return timeSinceLastUpdate < 7 * ONE_DAY;
};
const getMostRecentDayStart = () => {
console.log("getMostRecentDayStart()");
const now = new Date(Date.now());
// console.log(
// `current time is ${now.toUTCString()}; day starts at ${dayStartHour}:${dayStartMinute}`
// );
const mostRecentDayStart = now;
if (
mostRecentDayStart.getHours() < dayStartHour ||
(mostRecentDayStart.getHours() == dayStartHour &&
mostRecentDayStart.getMinutes() < dayStartMinute)
) {
// It's the next calendar day, but the streak day hasn't started yet, so subtract 24h to get the correct date
console.log("\tAdjusting for the wee hours...");
mostRecentDayStart.setDate(mostRecentDayStart.getDate() - 1);
}
mostRecentDayStart.setHours(dayStartHour, dayStartMinute, 0, 0);
return mostRecentDayStart;
};
const addToStreak = (msg, dbUser) => {
const streakData = {
streak: 1,
bestStreak: 1,
lastUpdate: new Date(),
};
let isNewBest = true;
let isNewStreak = false;
const user = dbUser.value();
if (!user.bestStreak) {
console.log(`${msg.author.username} started their first streak`);
isNewStreak = true;
} else if (!userStreakLastUpdatedYesterday(user)) {
console.log(`${msg.author.username} started a new streak`);
isNewStreak = true;
streakData.bestStreak = user.bestStreak;
isNewBest = false;
} else {
const newLevel = user.streak + 1;
const currentBest = user.bestStreak;
streakData.streak = newLevel;
streakData.bestStreak = Math.max(newLevel, currentBest);
console.log(`${msg.author.username} continued a streak to ${newLevel}`);
if (newLevel > currentBest) {
console.log(`\t...and it's a new best! (${newLevel})`);
} else {
isNewBest = false;
}
}
if (isNewStreak) {
msg.react("☄");
}
if (isNewBest) {
msg.react("🌟");
} else {
msg.react("⭐");
}
dbUser.assign(streakData).write();
};
async function main() {
await connect();
}
main().catch((e) => {
console.error(e);
process.exit(1);
});