Random messages

This commit is contained in:
James Shiffer 2023-10-08 18:59:04 -07:00
parent ba0b93ba4f
commit 81b75d3546
2 changed files with 43 additions and 1 deletions

View File

@ -1,3 +1,8 @@
TOKEN="sadkfl;jasdkl;fj"
REACTIONS="💀,💯,😭"
GUILD="123456789012345678"
ENABLE_MOTD=1
MOTD_CHANNEL="123456789012345678"
MOTD_HREF="https://fembooru.jp/post/list"
MOTD_QUERY="#tips"

View File

@ -3,7 +3,16 @@
* Scans the chat for reactions and updates the leaderboard database.
*/
import { Client, Events, GatewayIntentBits, MessageReaction, PartialMessageReaction, Partials, User } from 'discord.js';
import {
Client,
Events,
GatewayIntentBits,
MessageReaction,
PartialMessageReaction,
Partials,
TextChannel,
User
} from 'discord.js';
import { db, openDb, reactionEmojis, recordReaction, sync } from './util';
const client = new Client({
@ -46,6 +55,31 @@ async function onMessageReactionChanged(reaction: MessageReaction | PartialMessa
await recordReaction(<MessageReaction> reaction);
}
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);
}
client.on(Events.MessageReactionAdd, onMessageReactionChanged);
client.on(Events.MessageReactionRemove, onMessageReactionChanged);
@ -58,6 +92,9 @@ async function startup() {
console.log("[bot] Logging in...");
await client.login(process.env.TOKEN);
await sync(client.guilds);
if (process.env.ENABLE_MOTD) {
await scheduleRandomMessage(true);
}
}
startup();