Update GUILD_UPDATE.ts

This commit is contained in:
ITOH
2021-05-07 16:27:12 +02:00
parent 7cab5f34e7
commit 63d633bf48
+25 -19
View File
@@ -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) {
// Guild create sends undefined and update sends false.
if (!cachedValue && !value) return;
if (Array.isArray(cachedValue) && Array.isArray(value)) { if (cachedValue === value) return;
const different = (cachedValue.length !== value.length) || // Guild create sends undefined and update sends false.
cachedValue.find((val) => !value.includes(val)) || if (!cachedValue && !value) return;
value.find((val) => !cachedValue.includes(val));
if (!different) return;
}
// @ts-ignore index signature if (Array.isArray(cachedValue) && Array.isArray(value)) {
newGuild[key] = value; const different =
return { key, oldValue: cachedValue, value }; cachedValue.length !== value.length ||
cachedValue.find((val) => !value.includes(val)) ||
value.find((val) => !cachedValue.includes(val));
if (!different) return;
} }
}).filter((change) => change) as GuildUpdateChange[];
return { key, oldValue: cachedValue, value };
})
.filter((change) => change) as GuildUpdateChange[];
await cacheHandlers.set("guilds", newGuild.id, newGuild); await cacheHandlers.set("guilds", newGuild.id, newGuild);