Improved sync logic, show more statistics, bug fixes
This commit is contained in:
@@ -1,20 +1,11 @@
|
||||
/**
|
||||
* sync.ts
|
||||
* Syncs the message reactions in chat with the database, for when the bot is not running.
|
||||
* Syncs message reactions in chat with the database.
|
||||
* The bot will do the same thing automatically upon startup, but this is just in the form of a standalone script.
|
||||
*/
|
||||
|
||||
import {
|
||||
Client,
|
||||
Collection,
|
||||
Events,
|
||||
GatewayIntentBits,
|
||||
GuildTextBasedChannel,
|
||||
IntentsBitField,
|
||||
Message,
|
||||
MessageReaction,
|
||||
Partials
|
||||
} from 'discord.js';
|
||||
import { db, clearDb, openDb, reactionEmojis, recordReaction } from './util';
|
||||
import { Client, Events, GatewayIntentBits, IntentsBitField, Partials } from 'discord.js';
|
||||
import { db, openDb, reactionEmojis, sync } from './util';
|
||||
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages],
|
||||
@@ -28,8 +19,6 @@ client.once(Events.ClientReady, async () => {
|
||||
});
|
||||
|
||||
async function startup() {
|
||||
console.log("[db] Clearing database...");
|
||||
clearDb();
|
||||
console.log("[db] Opening...");
|
||||
await openDb();
|
||||
console.log("[db] Migrating...");
|
||||
@@ -37,39 +26,7 @@ async function startup() {
|
||||
console.log("[db] Ready.");
|
||||
console.log("[bot] Logging in...");
|
||||
await client.login(process.env.TOKEN);
|
||||
|
||||
const guild = await client.guilds.fetch(process.env.GUILD);
|
||||
if (!guild) {
|
||||
console.error(`[bot] FATAL: guild ${guild.id} not found!`);
|
||||
return 1;
|
||||
}
|
||||
console.log(`[bot] Entered guild ${guild.id}`);
|
||||
const channels = await guild.channels.fetch();
|
||||
const textChannels = <Collection<string, GuildTextBasedChannel>> channels.filter(c => c && 'messages' in c && c.isTextBased);
|
||||
for (const [id, textChannel] of textChannels) {
|
||||
console.log(`[bot] Found text channel ${id}`);
|
||||
let before: string = undefined;
|
||||
let messages = new Collection<string, Message<true>>();
|
||||
let newMessages: Collection<string, Message<true>>;
|
||||
try {
|
||||
do {
|
||||
newMessages = await textChannel.messages.fetch({before, limit: 100});
|
||||
messages = messages.concat(newMessages);
|
||||
console.log(`[bot] [${id}] Fetched ${messages.size} messages (+${newMessages.size})`);
|
||||
if (messages.size > 0)
|
||||
before = messages.last().id;
|
||||
} while (newMessages.size > 0);
|
||||
console.log(`[bot] [${id}] Fetched all messages.`);
|
||||
const reactions = messages.flatMap<MessageReaction>(m => m.reactions.cache);
|
||||
console.log(`[bot] Found ${reactions.size} reactions`);
|
||||
for (const [_, reaction] of reactions) {
|
||||
await recordReaction(reaction);
|
||||
}
|
||||
console.log(`[bot] [${id}] Finished recording reactions.`);
|
||||
} catch (err) {
|
||||
console.warn(`[bot] [${id}] Failed to fetch messages and reactions: ${err}`);
|
||||
}
|
||||
}
|
||||
await sync(client.guilds);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user