2023-10-07 22:46:02 -07:00
|
|
|
/**
|
|
|
|
* bot.ts
|
|
|
|
* Scans the chat for reactions and updates the leaderboard database.
|
|
|
|
*/
|
|
|
|
|
2023-10-08 18:59:04 -07:00
|
|
|
import {
|
|
|
|
Client,
|
|
|
|
Events,
|
|
|
|
GatewayIntentBits,
|
|
|
|
MessageReaction,
|
|
|
|
PartialMessageReaction,
|
|
|
|
Partials,
|
|
|
|
TextChannel,
|
|
|
|
User
|
|
|
|
} from 'discord.js';
|
2023-10-08 13:48:41 -07:00
|
|
|
import { db, openDb, reactionEmojis, recordReaction, sync } from './util';
|
2023-10-07 22:46:02 -07:00
|
|
|
|
|
|
|
const client = new Client({
|
|
|
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
|
|
|
|
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
|
|
|
|
});
|
|
|
|
|
|
|
|
client.once(Events.ClientReady, async () => {
|
|
|
|
console.log('[bot] Ready.');
|
|
|
|
for (let i = 0; i < reactionEmojis.length; ++i)
|
|
|
|
console.log(`[bot] config: reaction_${i + 1} = ${reactionEmojis[i]}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
async function onMessageReactionChanged(reaction: MessageReaction | PartialMessageReaction, user: User)
|
|
|
|
{
|
|
|
|
// When a reaction is received, check if the structure is partial
|
|
|
|
if (reaction.partial) {
|
|
|
|
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
|
|
|
|
try {
|
|
|
|
await reaction.fetch();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('[bot] Something went wrong when fetching the reaction:', error);
|
|
|
|
// Return as `reaction.message.author` may be undefined/null
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reaction.message.partial) {
|
|
|
|
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
|
|
|
|
try {
|
|
|
|
await reaction.message.fetch();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('[bot] Something went wrong when fetching the message:', error);
|
|
|
|
// Return as `reaction.message.author` may be undefined/null
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the message has been cached and is fully available
|
|
|
|
console.log(`[bot] ${reaction.message.author.id}'s message reaction count changed: ${reaction.emoji.name}x${reaction.count}`);
|
|
|
|
await recordReaction(<MessageReaction> reaction);
|
|
|
|
}
|
|
|
|
|
2023-10-08 18:59:04 -07:00
|
|
|
async function fetchMotd()
|
|
|
|
{
|
|
|
|
const res = await fetch(process.env.MOTD_HREF);
|
|
|
|
const xml = await res.text();
|
|
|
|
const parser = new DOMParser();
|
|
|
|
const doc = parser.parseFromString(xml, 'text/html');
|
|
|
|
return doc.querySelector(process.env.MOTD_QUERY).textContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function scheduleRandomMessage(firstTime = false)
|
|
|
|
{
|
|
|
|
if (!firstTime) {
|
|
|
|
const channel = <TextChannel> await client.channels.fetch(process.env.MOTD_CHANNEL);
|
|
|
|
const randomMessage = await fetchMotd();
|
|
|
|
await channel.send(randomMessage);
|
|
|
|
console.log(`[bot] Sent MOTD: ${randomMessage}`);
|
|
|
|
}
|
|
|
|
// wait between 2-8 hours
|
|
|
|
const timeoutMins = Math.random() * 360 + 120;
|
|
|
|
const scheduledTime = new Date();
|
|
|
|
scheduledTime.setMinutes(scheduledTime.getMinutes() + timeoutMins);
|
|
|
|
console.log(`[bot] Next MOTD: ${scheduledTime}`);
|
|
|
|
setTimeout(scheduleRandomMessage, timeoutMins * 60 * 1000);
|
|
|
|
}
|
|
|
|
|
2023-10-07 22:46:02 -07:00
|
|
|
client.on(Events.MessageReactionAdd, onMessageReactionChanged);
|
|
|
|
client.on(Events.MessageReactionRemove, onMessageReactionChanged);
|
|
|
|
|
|
|
|
async function startup() {
|
|
|
|
console.log("[db] Opening...");
|
|
|
|
await openDb();
|
|
|
|
console.log("[db] Migrating...");
|
|
|
|
await db.migrate();
|
|
|
|
console.log("[db] Ready.");
|
|
|
|
console.log("[bot] Logging in...");
|
|
|
|
await client.login(process.env.TOKEN);
|
2023-10-08 13:48:41 -07:00
|
|
|
await sync(client.guilds);
|
2023-10-08 18:59:04 -07:00
|
|
|
if (process.env.ENABLE_MOTD) {
|
|
|
|
await scheduleRandomMessage(true);
|
|
|
|
}
|
2023-10-07 22:46:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
startup();
|