mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
Update GUILD_UPDATE.ts
This commit is contained in:
@@ -1,17 +1,21 @@
|
|||||||
import { eventHandlers } from "../../bot.ts";
|
import { eventHandlers } from "../../bot.ts";
|
||||||
import { cacheHandlers } from "../../cache.ts";
|
import { cacheHandlers } from "../../cache.ts";
|
||||||
|
import { structures } from "../../structures/mod.ts";
|
||||||
import type { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts";
|
import type { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts";
|
||||||
import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
|
import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
|
||||||
import type { Guild } from "../../types/guilds/guild.ts";
|
import type { Guild } from "../../types/guilds/guild.ts";
|
||||||
import { snowflakeToBigint } from "../../util/bigint.ts";
|
import { snowflakeToBigint } from "../../util/bigint.ts";
|
||||||
|
|
||||||
export async function handleGuildUpdate(data: DiscordGatewayPayload) {
|
export async function handleGuildUpdate(
|
||||||
|
data: DiscordGatewayPayload,
|
||||||
|
shardId: number
|
||||||
|
) {
|
||||||
const payload = data.d as Guild;
|
const payload = data.d as Guild;
|
||||||
const newGuild = await cacheHandlers.get(
|
const oldGuild = await cacheHandlers.get(
|
||||||
"guilds",
|
"guilds",
|
||||||
snowflakeToBigint(payload.id),
|
snowflakeToBigint(payload.id)
|
||||||
);
|
);
|
||||||
if (!newGuild) return;
|
if (!oldGuild) return;
|
||||||
|
|
||||||
const keysToSkip = [
|
const keysToSkip = [
|
||||||
"id",
|
"id",
|
||||||
@@ -22,28 +26,30 @@ export async function handleGuildUpdate(data: DiscordGatewayPayload) {
|
|||||||
"emojis",
|
"emojis",
|
||||||
];
|
];
|
||||||
|
|
||||||
const changes = Object.entries(payload)
|
const newGuild = await structures.createDiscordenoGuild(payload, shardId);
|
||||||
|
|
||||||
|
const changes = Object.entries(newGuild)
|
||||||
.map(([key, value]) => {
|
.map(([key, value]) => {
|
||||||
if (keysToSkip.includes(key)) return;
|
if (keysToSkip.includes(key)) return;
|
||||||
|
|
||||||
// @ts-ignore index signature
|
// @ts-ignore index signature
|
||||||
const cachedValue = newGuild[key];
|
const cachedValue = oldGuild[key];
|
||||||
if (cachedValue !== value) {
|
|
||||||
|
if (cachedValue === value) return;
|
||||||
// Guild create sends undefined and update sends false.
|
// Guild create sends undefined and update sends false.
|
||||||
if (!cachedValue && !value) return;
|
if (!cachedValue && !value) return;
|
||||||
|
|
||||||
if (Array.isArray(cachedValue) && Array.isArray(value)) {
|
if (Array.isArray(cachedValue) && Array.isArray(value)) {
|
||||||
const different = (cachedValue.length !== value.length) ||
|
const different =
|
||||||
|
cachedValue.length !== value.length ||
|
||||||
cachedValue.find((val) => !value.includes(val)) ||
|
cachedValue.find((val) => !value.includes(val)) ||
|
||||||
value.find((val) => !cachedValue.includes(val));
|
value.find((val) => !cachedValue.includes(val));
|
||||||
if (!different) return;
|
if (!different) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore index signature
|
|
||||||
newGuild[key] = value;
|
|
||||||
return { key, oldValue: cachedValue, value };
|
return { key, oldValue: cachedValue, value };
|
||||||
}
|
})
|
||||||
}).filter((change) => change) as GuildUpdateChange[];
|
.filter((change) => change) as GuildUpdateChange[];
|
||||||
|
|
||||||
await cacheHandlers.set("guilds", newGuild.id, newGuild);
|
await cacheHandlers.set("guilds", newGuild.id, newGuild);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user