mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 03:18:17 +00:00
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import { eventHandlers } from "../../bot.ts";
|
|
import { cacheHandlers } from "../../cache.ts";
|
|
import { structures } from "../../structures/mod.ts";
|
|
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
|
|
import { VoiceState } from "../../types/voice/voice_state.ts";
|
|
|
|
export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
|
|
const payload = data.d as VoiceState;
|
|
|
|
if (!payload.guildId) return;
|
|
|
|
const guild = await cacheHandlers.get("guilds", payload.guildId);
|
|
if (!guild) return;
|
|
|
|
const member = payload.member
|
|
? await structures.createDiscordenoMember(
|
|
payload.member,
|
|
guild.id,
|
|
)
|
|
: await cacheHandlers.get("members", payload.userId);
|
|
if (!member) return;
|
|
|
|
// No cached state before so lets make one for em
|
|
const cachedState = guild.voiceStates.get(payload.userId);
|
|
|
|
guild.voiceStates.set(
|
|
payload.userId,
|
|
payload,
|
|
);
|
|
|
|
await cacheHandlers.set("guilds", payload.guildId, guild);
|
|
|
|
if (cachedState?.channelId !== payload.channelId) {
|
|
// Either joined or moved channels
|
|
if (payload.channelId) {
|
|
if (cachedState?.channelId) { // Was in a channel before
|
|
eventHandlers.voiceChannelSwitch?.(
|
|
member,
|
|
payload.channelId,
|
|
cachedState.channelId,
|
|
);
|
|
} else { // Was not in a channel before so user just joined
|
|
eventHandlers.voiceChannelJoin?.(member, payload.channelId);
|
|
}
|
|
} // Left the channel
|
|
else if (cachedState?.channelId) {
|
|
guild.voiceStates.delete(payload.userId);
|
|
eventHandlers.voiceChannelLeave?.(member, cachedState.channelId);
|
|
}
|
|
}
|
|
|
|
eventHandlers.voiceStateUpdate?.(member, payload);
|
|
}
|