From 60307f87e8a5b0fd47f293641bcb3471c5267000 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sun, 25 Jul 2021 12:04:02 +0100 Subject: [PATCH] fix(Examples): destroy discord.js v12 adapter less readily (#160) * fix(Examples): destroy discord.js v12 adapter less readily * refactor(Examples): delete adapters instead of setting empty set * refactor(Examples): consistent Id/ID casing --- examples/basic/adapter.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/basic/adapter.ts b/examples/basic/adapter.ts index 87eff80f..14767dc9 100644 --- a/examples/basic/adapter.ts +++ b/examples/basic/adapter.ts @@ -20,27 +20,24 @@ function trackClient(client: Client) { adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload); } }); -} - -const trackedGuilds = new Map>(); - -function cleanupGuilds(shard: WebSocketShard) { - const guilds = trackedGuilds.get(shard); - if (guilds) { - for (const guildId of guilds.values()) { - adapters.get(guildId)?.destroy(); + client.on(Constants.Events.SHARD_DISCONNECT, (_, shardID) => { + const guilds = trackedShards.get(shardID); + if (guilds) { + for (const guildID of guilds.values()) { + adapters.get(guildID)?.destroy(); + } } - } + trackedShards.delete(shardID); + }); } +const trackedShards = new Map>(); + function trackGuild(guild: Guild) { - let guilds = trackedGuilds.get(guild.shard); + let guilds = trackedShards.get(guild.shardID); if (!guilds) { - const cleanup = () => cleanupGuilds(guild.shard); - guild.shard.on('close', cleanup); - guild.shard.on('destroyed', cleanup); guilds = new Set(); - trackedGuilds.set(guild.shard, guilds); + trackedShards.set(guild.shardID, guilds); } guilds.add(guild.id); }