Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
fix(Examples): destroy discord.js v12 adapter less readily (#160)
Browse files Browse the repository at this point in the history
* fix(Examples): destroy discord.js v12 adapter less readily

* refactor(Examples): delete adapters instead of setting empty set

* refactor(Examples): consistent Id/ID casing
  • Loading branch information
amishshah authored Jul 25, 2021
1 parent 4023ef9 commit 60307f8
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions examples/basic/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,24 @@ function trackClient(client: Client) {
adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload);
}
});
}

const trackedGuilds = new Map<WebSocketShard, Set<Snowflake>>();

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<number, Set<Snowflake>>();

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);
}
Expand Down

0 comments on commit 60307f8

Please sign in to comment.