mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 09:20:08 +00:00
* fix: check new types idea * fix: type errors * fix: new style * fix: more cleanup * fix: more cleanup * fix: cleanup audit logs * fix: cleanup stickers * fix: cleanup integrations * fix: more cleanup * fix: organize into 1 place * fix: few errors * fix: some broken import fixes * fix: quite a lot of fixes across the board * fix: more fixes for broken imports * fix: more fixes for broken imports * fix: handler imports * fix: all remaining import errors * fix: more errors needing fixes * fix: clearing up transformers * fix: few moer types * fix: more cleanup of extra types * fix: fmt * fix: cleanup discordeno file * Nuke Base Types (#2102) * fix: cleanup snake stuff * convert camelCase to snake_case (#2103) * fix: add camelize * fix: finalize remaining errors * fix: imports in test Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
100 lines
2.6 KiB
TypeScript
100 lines
2.6 KiB
TypeScript
import { Bot, DiscordGatewayPayload } from "../deps.ts";
|
|
import { BotWithCache } from "./addCacheCollections.ts";
|
|
|
|
const processing = new Set<bigint>();
|
|
|
|
export async function dispatchRequirements<B extends Bot>(
|
|
bot: BotWithCache<B>,
|
|
data: DiscordGatewayPayload,
|
|
) {
|
|
// DELETE MEANS WE DONT NEED TO FETCH. CREATE SHOULD HAVE DATA TO CACHE
|
|
if (data.t && ["GUILD_CREATE", "GUILD_DELETE"].includes(data.t)) return;
|
|
|
|
const id = bot.utils.snowflakeToBigint(
|
|
(data.t && ["GUILD_UPDATE"].includes(data.t)
|
|
? // deno-lint-ignore no-explicit-any
|
|
(data.d as any)?.id
|
|
: // deno-lint-ignore no-explicit-any
|
|
(data.d as any)?.guild_id) ?? "",
|
|
);
|
|
|
|
if (!id || bot.activeGuildIds.has(id)) return;
|
|
|
|
// If this guild is in cache, it has not been swept and we can cancel
|
|
if (bot.guilds.has(id)) {
|
|
bot.activeGuildIds.add(id);
|
|
return;
|
|
}
|
|
|
|
if (processing.has(id)) {
|
|
bot.events.debug(
|
|
`[DISPATCH] New Guild ID already being processed: ${id} in ${data.t} event`,
|
|
);
|
|
|
|
let runs = 0;
|
|
do {
|
|
await bot.utils.delay(500);
|
|
runs++;
|
|
} while (processing.has(id) && runs < 40);
|
|
|
|
if (!processing.has(id)) return;
|
|
|
|
return bot.events.debug(
|
|
`[DISPATCH] Already processed guild was not successfully fetched: ${id} in ${data.t} event`,
|
|
);
|
|
}
|
|
|
|
processing.add(id);
|
|
|
|
// New guild id has appeared, fetch all relevant data
|
|
bot.events.debug(
|
|
`[DISPATCH] New Guild ID has appeared: ${id} in ${data.t} event`,
|
|
);
|
|
|
|
const guild = (await bot.helpers
|
|
.getGuild(id, {
|
|
counts: true,
|
|
})
|
|
.catch(console.log));
|
|
|
|
if (!guild) {
|
|
processing.delete(id);
|
|
return bot.events.debug(`[DISPATCH] Guild ID ${id} failed to fetch.`);
|
|
}
|
|
|
|
bot.events.debug(`[DISPATCH] Guild ID ${id} has been found. ${guild.name}`);
|
|
|
|
const [channels, botMember] = await Promise.all([
|
|
bot.helpers.getChannels(id),
|
|
bot.helpers.getMember(id, bot.id),
|
|
]).catch((error) => {
|
|
bot.events.debug(error);
|
|
return [];
|
|
});
|
|
|
|
if (!botMember || !channels) {
|
|
processing.delete(id);
|
|
return bot.events.debug(
|
|
`[DISPATCH] Guild ID ${id} Name: ${guild.name} failed. Unable to get botMember or channels`,
|
|
);
|
|
}
|
|
|
|
// Add to cache
|
|
bot.guilds.set(id, guild);
|
|
bot.dispatchedGuildIds.delete(id);
|
|
channels.forEach((channel) => {
|
|
bot.dispatchedChannelIds.delete(channel.id);
|
|
bot.channels.set(channel.id, channel);
|
|
});
|
|
bot.members.set(
|
|
bot.transformers.snowflake(`${botMember.id}${guild.id}`),
|
|
botMember,
|
|
);
|
|
|
|
processing.delete(id);
|
|
|
|
bot.events.debug(
|
|
`[DISPATCH] Guild ID ${id} Name: ${guild.name} completely loaded.`,
|
|
);
|
|
}
|