mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
change(cacheHandlers): forEach logic
This commit is contained in:
+64
-26
@@ -14,6 +14,7 @@ import { GuildMemberWithUser } from "./types/members/guild_member.ts";
|
||||
import { Message } from "./types/messages/message.ts";
|
||||
import { Role } from "./types/permissions/role.ts";
|
||||
import { VoiceState } from "./types/voice/voice_state.ts";
|
||||
import { snowflakeToBigint } from "./util/bigint.ts";
|
||||
|
||||
export const cache = {
|
||||
isReady: false,
|
||||
@@ -170,32 +171,69 @@ async function get(table: TableName, key: bigint) {
|
||||
return cache[table].get(key);
|
||||
}
|
||||
|
||||
function forEach(
|
||||
table: "threads",
|
||||
callback: (value: DiscordenoThread, key: bigint, map: Map<bigint, DiscordenoThread>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "guilds",
|
||||
callback: (value: DiscordenoGuild, key: bigint, map: Map<bigint, DiscordenoGuild>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "unavailableGuilds",
|
||||
callback: (value: number, key: bigint, map: Map<bigint, number>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "channels",
|
||||
callback: (value: DiscordenoChannel, key: bigint, map: Map<bigint, DiscordenoChannel>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "messages",
|
||||
callback: (value: DiscordenoMessage, key: bigint, map: Map<bigint, DiscordenoMessage>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "members",
|
||||
callback: (value: DiscordenoMember, key: bigint, map: Map<bigint, DiscordenoMember>) => unknown
|
||||
): void;
|
||||
function forEach(table: TableName, callback: (value: any, key: bigint, map: Map<bigint, any>) => unknown) {
|
||||
return cache[table].forEach(callback);
|
||||
// callback: (value: DiscordenoThread, key: bigint, map: Map<bigint, DiscordenoThread>) => void
|
||||
async function forEach(type: "DELETE_MESSAGES_FROM_CHANNEL", options: { channelId: bigint }): Promise<void>;
|
||||
async function forEach(type: "DELETE_MESSAGES_FROM_GUILD", options: { guildId: bigint }): Promise<void>;
|
||||
async function forEach(type: "DELETE_CHANNELS_FROM_GUILD", options: { guildId: bigint }): Promise<void>;
|
||||
async function forEach(type: "DELETE_GUILD_FROM_MEMBER", options: { guildId: bigint }): Promise<void>;
|
||||
async function forEach(type: "DELETE_ROLE_FROM_MEMBER", options: { guildId: bigint; roleId: bigint }): Promise<void>;
|
||||
async function forEach(
|
||||
type:
|
||||
| "DELETE_MESSAGES_FROM_CHANNEL"
|
||||
| "DELETE_MESSAGES_FROM_GUILD"
|
||||
| "DELETE_CHANNELS_FROM_GUILD"
|
||||
| "DELETE_GUILD_FROM_MEMBER"
|
||||
| "DELETE_ROLE_FROM_MEMBER",
|
||||
options?: Record<string, unknown>
|
||||
) {
|
||||
if (type === "DELETE_MESSAGES_FROM_CHANNEL") {
|
||||
cache.messages.forEach((message) => {
|
||||
if (message.channelId === options?.channelId) cache.messages.delete(message.id);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "DELETE_MESSAGES_FROM_GUILD") {
|
||||
cache.messages.forEach((message) => {
|
||||
if (message.guildId === options?.guildId) cache.messages.delete(message.id);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "DELETE_CHANNELS_FROM_GUILD") {
|
||||
cache.channels.forEach((channel) => {
|
||||
if (channel.guildId === options?.guildId) cache.channels.delete(channel.id);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "DELETE_GUILD_FROM_MEMBER") {
|
||||
cache.members.forEach((member) => {
|
||||
if (!member.guilds.has(options?.guildId as bigint)) return;
|
||||
|
||||
member.guilds.delete(options?.guildId as bigint);
|
||||
|
||||
if (!member.guilds.size) {
|
||||
return cache.members.delete(member.id);
|
||||
}
|
||||
|
||||
cache.members.set(member.id, member);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "DELETE_ROLE_FROM_MEMBER") {
|
||||
cache.members.forEach((member) => {
|
||||
// Not in the relevant guild so just skip
|
||||
if (!member.guilds.has(options?.guildId as bigint)) return;
|
||||
|
||||
const guildMember = member.guilds.get(options?.guildId as bigint)!;
|
||||
|
||||
guildMember.roles = guildMember.roles.filter((id) => id !== (options?.roleId as bigint));
|
||||
cache.members.set(member.id, member);
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function filter(
|
||||
|
||||
@@ -40,12 +40,7 @@ export async function handleChannelDelete(data: DiscordGatewayPayload) {
|
||||
].includes(payload.type)
|
||||
) {
|
||||
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
|
||||
cacheHandlers.forEach("messages", (message) => {
|
||||
eventHandlers.debug?.("loop", `Running forEach messages loop in CHANNEL_DELTE file.`);
|
||||
if (message.channelId === snowflakeToBigint(payload.id)) {
|
||||
cacheHandlers.delete("messages", message.id);
|
||||
}
|
||||
});
|
||||
await cacheHandlers.forEach("DELETE_MESSAGES_FROM_CHANNEL", { channelId: snowflakeToBigint(payload.id) });
|
||||
}
|
||||
|
||||
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
|
||||
|
||||
@@ -11,12 +11,7 @@ export async function handleThreadDelete(data: DiscordGatewayPayload) {
|
||||
if (!cachedChannel) return;
|
||||
|
||||
await cacheHandlers.delete("threads", snowflakeToBigint(payload.id));
|
||||
cacheHandlers.forEach("messages", (message) => {
|
||||
eventHandlers.debug?.("loop", `Running forEach messages loop in THREAD_DELETE file.`);
|
||||
if (message.channelId === snowflakeToBigint(payload.id)) {
|
||||
cacheHandlers.delete("messages", message.id);
|
||||
}
|
||||
});
|
||||
await cacheHandlers.forEach("DELETE_MESSAGES_FROM_CHANNEL", { channelId: snowflakeToBigint(payload.id) });
|
||||
|
||||
eventHandlers.threadDelete?.(cachedChannel);
|
||||
}
|
||||
|
||||
@@ -23,30 +23,9 @@ export async function handleGuildDelete(data: DiscordGatewayPayload, shardId: nu
|
||||
eventHandlers.guildDelete?.(guild);
|
||||
}
|
||||
|
||||
cacheHandlers.forEach("messages", (message) => {
|
||||
eventHandlers.debug?.("loop", `1. Running forEach messages loop in CHANNEL_DELTE file.`);
|
||||
if (message.guildId === guild.id) {
|
||||
cacheHandlers.delete("messages", message.id);
|
||||
}
|
||||
});
|
||||
|
||||
cacheHandlers.forEach("channels", (channel) => {
|
||||
eventHandlers.debug?.("loop", `2. Running forEach channels loop in CHANNEL_DELTE file.`);
|
||||
if (channel.guildId === guild.id) {
|
||||
cacheHandlers.delete("channels", channel.id);
|
||||
}
|
||||
});
|
||||
|
||||
cacheHandlers.forEach("members", (member) => {
|
||||
eventHandlers.debug?.("loop", `3. Running forEach members loop in CHANNEL_DELTE file.`);
|
||||
if (!member.guilds.has(guild.id)) return;
|
||||
|
||||
member.guilds.delete(guild.id);
|
||||
|
||||
if (!member.guilds.size) {
|
||||
return cacheHandlers.delete("members", member.id);
|
||||
}
|
||||
|
||||
cacheHandlers.set("members", member.id, member);
|
||||
});
|
||||
await Promise.all([
|
||||
cacheHandlers.forEach("DELETE_MESSAGES_FROM_GUILD", { guildId: guild.id }),
|
||||
cacheHandlers.forEach("DELETE_CHANNELS_FROM_GUILD", { guildId: guild.id }),
|
||||
cacheHandlers.forEach("DELETE_GUILD_FROM_MEMBER", { guildId: guild.id }),
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user