Improved sync logic, show more statistics, bug fixes

This commit is contained in:
James Shiffer
2023-10-08 13:48:41 -07:00
parent 68b94e0642
commit 657afe4755
7 changed files with 164 additions and 62 deletions

View File

@@ -3,7 +3,7 @@
* Common helper functions
*/
import { MessageReaction, User } from 'discord.js';
import { Collection, GuildManager, GuildTextBasedChannel, Message, MessageReaction, User } from 'discord.js';
import { createWriteStream, existsSync, unlinkSync } from 'fs';
import { get as httpGet } from 'https';
import { Database, open } from 'sqlite';
@@ -41,7 +41,7 @@ async function downloadUserAvatar(user: User)
console.log(`[bot] Downloading ${user.id}'s avatar...`);
const file = createWriteStream(userAvatarPath(user));
return new Promise<void>(resolve => {
httpGet(user.avatarURL(), res => {
httpGet(user.displayAvatarURL(), res => {
res.pipe(file);
file.on('finish', () => {
file.close();
@@ -83,7 +83,7 @@ async function recordReaction(reaction: MessageReaction)
}
try {
await db.run(
`INSERT INTO messages(id, guild, channel, author, content, reaction_${emojiIdx}_count) VALUES(?, ?, ?, ?, ?, 1)
`INSERT INTO messages(id, guild, channel, author, content, reaction_${emojiIdx}_count) VALUES(?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET reaction_${emojiIdx}_count = ? WHERE id = ?`,
reaction.message.id,
reaction.message.guildId,
@@ -91,6 +91,7 @@ async function recordReaction(reaction: MessageReaction)
reaction.message.author.id,
reaction.message.content,
reaction.count,
reaction.count,
reaction.message.id
);
await refreshUserReactionTotalCount(reaction.message.author, emojiIdx);
@@ -101,4 +102,62 @@ async function recordReaction(reaction: MessageReaction)
}
}
export { db, clearDb, openDb, reactionEmojis, recordReaction };
async function sync(guilds: GuildManager) {
const guild = await 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}`);
const oldestMsg = await db.get<ScoreboardMessageRow>(
'SELECT * FROM messages WHERE guild = ? AND channel = ? ORDER BY id ASC LIMIT 1',
guild.id,
id
);
const newestMsg = await db.get<ScoreboardMessageRow>(
'SELECT * FROM messages WHERE guild = ? AND channel = ? ORDER BY id DESC LIMIT 1',
guild.id,
id
);
let before: string = oldestMsg && String(oldestMsg.id);
let after: string = newestMsg && String(newestMsg.id);
let messagesCount = 0;
let reactionsCount = 0;
let newMessagesBefore: Collection<string, Message<true>>;
let newMessagesAfter: Collection<string, Message<true>>;
try {
do {
newMessagesBefore = await textChannel.messages.fetch({ before, limit: 100 });
messagesCount += newMessagesBefore.size;
newMessagesAfter = await textChannel.messages.fetch({ after, limit: 100 });
messagesCount += newMessagesAfter.size;
console.log(`[bot] [${id}] Fetched ${messagesCount} messages (+${newMessagesBefore.size} older, ${newMessagesAfter.size} newer)`);
const reactions = newMessagesBefore.flatMap<MessageReaction>(m => m.reactions.cache)
.concat(newMessagesAfter.flatMap<MessageReaction>(m => m.reactions.cache));
for (const [_, reaction] of reactions) {
await recordReaction(reaction);
}
reactionsCount += reactions.size;
console.log(`[bot] [${id}] Recorded ${reactionsCount} reactions (+${reactions.size}).`);
if (newMessagesBefore.size > 0) {
before = newMessagesBefore.last().id;
}
if (newMessagesAfter.size > 0) {
after = newMessagesAfter.first().id;
}
} while (newMessagesBefore.size === 100 || newMessagesAfter.size === 100);
console.log(`[bot] [${id}] Done.`);
} catch (err) {
console.warn(`[bot] [${id}] Failed to fetch messages and reactions: ${err}`);
}
}
}
export { db, clearDb, openDb, reactionEmojis, recordReaction, sync };