diff --git a/plugins/cache/src/setupCacheEdits.ts b/plugins/cache/src/setupCacheEdits.ts index 1e4e34a00..d0c370a64 100644 --- a/plugins/cache/src/setupCacheEdits.ts +++ b/plugins/cache/src/setupCacheEdits.ts @@ -5,6 +5,7 @@ import type { DiscordMessageReactionAdd, DiscordMessageReactionRemove, DiscordMessageReactionRemoveAll, + DiscordVoiceState, } from "../deps.ts"; import type { BotWithCache } from "./addCacheCollections.ts"; @@ -15,6 +16,7 @@ export function setupCacheEdits(bot: BotWithCache) { MESSAGE_REACTION_ADD, MESSAGE_REACTION_REMOVE, MESSAGE_REACTION_REMOVE_ALL, + VOICE_STATE_UPDATE, } = bot.handlers; bot.handlers.GUILD_MEMBER_ADD = function (_, data, shardId) { @@ -116,4 +118,18 @@ export function setupCacheEdits(bot: BotWithCache) { MESSAGE_REACTION_REMOVE_ALL(bot, data, shardId); }; + + bot.handlers.VOICE_STATE_UPDATE = (_, data, shardId) => { + const payload = data.d as DiscordVoiceState; + if (!payload.guild_id) return; + + const vs = bot.transformers.voiceState(bot, { + voiceState: payload, + guildId: bot.transformers.snowflake(payload.guild_id), + }); + + bot.guilds.get(vs.guildId)?.voiceStates.set(vs.userId, vs); + + VOICE_STATE_UPDATE(bot, data, shardId); + }; } diff --git a/plugins/cache/src/setupCacheRemovals.ts b/plugins/cache/src/setupCacheRemovals.ts index 9ce971749..d8d826722 100644 --- a/plugins/cache/src/setupCacheRemovals.ts +++ b/plugins/cache/src/setupCacheRemovals.ts @@ -1,5 +1,4 @@ -import type { Emoji, Guild } from "../deps.ts"; -import type { BotWithCache } from "./addCacheCollections.ts"; +import type { Emoji } from "../deps.ts"; import { Bot, Collection, @@ -11,7 +10,9 @@ import { DiscordMessageDelete, DiscordMessageDeleteBulk, DiscordUnavailableGuild, + DiscordVoiceState, } from "../deps.ts"; +import type { BotWithCache } from "./addCacheCollections.ts"; export function setupCacheRemovals(bot: BotWithCache) { const { @@ -22,6 +23,7 @@ export function setupCacheRemovals(bot: BotWithCache) { GUILD_MEMBER_REMOVE, GUILD_ROLE_DELETE, MESSAGE_DELETE_BULK, + VOICE_STATE_UPDATE, } = bot.handlers; bot.handlers.GUILD_DELETE = function (_, data, shardId) { @@ -118,4 +120,18 @@ export function setupCacheRemovals(bot: BotWithCache) { GUILD_ROLE_DELETE(bot, data, shardId); }; + + bot.handlers.VOICE_STATE_UPDATE = (_, data, shardId) => { + const payload = data.d as DiscordVoiceState; + if (!payload.guild_id) return; + + const vs = bot.transformers.voiceState(bot, { + voiceState: payload, + guildId: bot.transformers.snowflake(payload.guild_id), + }); + + if (!vs.channelId) bot.guilds.get(vs.guildId)?.voiceStates.delete(vs.userId); + + VOICE_STATE_UPDATE(bot, data, shardId); + }; }