Skip to content

Commit

Permalink
fixes #60
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Sawatzky <[email protected]>
  • Loading branch information
msawatzky75 committed Dec 12, 2023
1 parent b04f16d commit 150aa7e
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions src/commands/quote.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import debug from "debug";
import {ChatInputCommandInteraction, Collection, Message, SlashCommandBuilder, TextChannel} from "discord.js";
import {
ChatInputCommandInteraction,
Collection,
Message,
SlashCommandBuilder,
Snowflake,
TextChannel,
} from "discord.js";
import type {Command} from "./index";

const d = debug("bot.commands.quote");
const messages = new Collection<Snowflake, Collection<string, Message<true>>>();
const lastUpdate = new Collection<Snowflake, Date>();
const cacheLife = 1000 * 60 * 15; // 15 minutes

const command: Command = {
data: new SlashCommandBuilder()
Expand All @@ -18,23 +28,19 @@ const command: Command = {

if (!(quoteChannel instanceof TextChannel)) throw new Error("Could not find quote channel");

const limit = 100;
let serverMessages = messages.get(quoteChannel.guildId);
let timeSinceUpdate = new Date().getTime() - (lastUpdate.get(quoteChannel.guildId)?.getTime() ?? 0);

let messages: Collection<string, Message<true>> = new Collection();
let fetchedMessageCount = 0;
let pages = 0;
if (!serverMessages) {
d(`No cache for ${quoteChannel.guild.name}, fetching quotes...`);
await UpdateMessages(quoteChannel);
}
if (timeSinceUpdate >= cacheLife) {
d(`Cache expired for ${quoteChannel.guild.name}, fetching quotes...`);
await UpdateMessages(quoteChannel);
}

do {
d(`fetching ${limit} new messages...`);
const newMessages = await quoteChannel.messages.fetch({limit: limit, before: messages.last()?.id});
fetchedMessageCount = newMessages.size;
messages = messages.concat(newMessages);
pages++;
} while (fetchedMessageCount === limit);

d(`fetched ${messages.size} messages over ${pages} pages`);

const randomMessage = messages.random();
const randomMessage = messages.get(quoteChannel.guildId).random();

// replace mentions with their names
const content = randomMessage.content.replace(/<@!?(\d+)>/g, (match) => {
Expand All @@ -47,4 +53,24 @@ const command: Command = {
},
};

async function UpdateMessages(quoteChannel: TextChannel) {
const limit = 100;

let quoteMessages: Collection<string, Message<true>> = new Collection();
let fetchedMessageCount = 0;
let pages = 0;

do {
d(`fetching ${limit} new messages...`);
const newMessages = await quoteChannel.messages.fetch({limit: limit, before: quoteMessages.last()?.id});
fetchedMessageCount = newMessages.size;
quoteMessages = quoteMessages.concat(newMessages);
pages++;
} while (fetchedMessageCount === limit);

d(`fetched ${quoteMessages.size} messages over ${pages} pages`);
messages.set(quoteChannel.guildId, quoteMessages);
lastUpdate.set(quoteChannel.guildId, new Date());
}

export default command;

0 comments on commit 150aa7e

Please sign in to comment.