better thread caching

This commit is contained in:
Cynthia Foxwell 2021-08-30 13:01:45 -06:00
parent 2ee2b46bbf
commit 17fab4658b

View file

@ -72,15 +72,6 @@ bot.on("messageCreate", async (msg) => {
) {
const newChannel = await bot.getDMChannel(msg.author.id);
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
} else if (
msg.guildID &&
!bot.guilds.get(msg.guildID).channels.has(msg.channel.id)
) {
const threads = await bot.requestHandler
.request("GET", `/guilds/${msg.guildID}/threads/active`, true)
.then((x) => x.threads);
const thread = threads.filter((x) => x.id == msg.channel.id)[0];
if (thread) msg.channel = new Eris.TextChannel(thread, bot);
}
// if we still have no dm channel (threads cause this too)
@ -108,6 +99,22 @@ bot.on("ready", async () => {
}
});
bot.once("ready", async function () {
const guilds = bot.guilds.values();
async function cacheThreads(guild) {
if (!guild) return;
const threads = await bot.requestHandler
.request("GET", `/guilds/${guild.id}/threads/active`, true)
.then((x) => x.threads);
for (const thread of threads) {
guild.channels.set(thread.id, new Eris.TextChannel(thread, bot));
}
const next = guilds.next().value;
if (next) await cacheThreads(next);
}
await cacheThreads(guilds.next().value);
});
bot.on("error", (err) => {
logger.error("hf:main", "Catching error: " + err);
});