2022-03-19 17:37:45 +07:00
|
|
|
'use strict';
|
|
|
|
|
|
2022-03-24 17:55:32 +07:00
|
|
|
const { Events } = require('../../util/Constants');
|
2022-03-19 17:37:45 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manages voice connections for the client
|
|
|
|
|
*/
|
|
|
|
|
class ClientVoiceManager {
|
|
|
|
|
constructor(client) {
|
|
|
|
|
/**
|
|
|
|
|
* The client that instantiated this voice manager
|
|
|
|
|
* @type {Client}
|
|
|
|
|
* @readonly
|
|
|
|
|
* @name ClientVoiceManager#client
|
|
|
|
|
*/
|
|
|
|
|
Object.defineProperty(this, 'client', { value: client });
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps guild ids to voice adapters created for use with @discordjs/voice.
|
|
|
|
|
* @type {Map<Snowflake, Object>}
|
|
|
|
|
*/
|
|
|
|
|
this.adapters = new Map();
|
|
|
|
|
|
2022-03-24 17:55:32 +07:00
|
|
|
client.on(Events.SHARD_DISCONNECT, (_, shardId) => {
|
2022-03-19 17:37:45 +07:00
|
|
|
for (const [guildId, adapter] of this.adapters.entries()) {
|
|
|
|
|
if (client.guilds.cache.get(guildId)?.shardId === shardId) {
|
2022-05-21 20:58:33 +07:00
|
|
|
// Because it has 1 shard => adapter.destroy();
|
2022-03-19 17:37:45 +07:00
|
|
|
}
|
2022-05-21 20:58:33 +07:00
|
|
|
adapter.destroy();
|
2022-03-19 17:37:45 +07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onVoiceServer(payload) {
|
2022-05-21 20:58:33 +07:00
|
|
|
if (payload.guild_id) {
|
|
|
|
|
this.adapters.get(payload.guild_id)?.onVoiceServerUpdate(payload);
|
|
|
|
|
} else {
|
|
|
|
|
this.adapters.get(payload.channel_id)?.onVoiceServerUpdate(payload);
|
|
|
|
|
}
|
2022-03-19 17:37:45 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onVoiceStateUpdate(payload) {
|
|
|
|
|
if (payload.guild_id && payload.session_id && payload.user_id === this.client.user?.id) {
|
|
|
|
|
this.adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload);
|
2022-05-21 20:58:33 +07:00
|
|
|
} else if (payload.channel_id && payload.session_id && payload.user_id === this.client.user?.id) {
|
|
|
|
|
this.adapters.get(payload.channel_id)?.onVoiceStateUpdate(payload);
|
2022-03-19 17:37:45 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = ClientVoiceManager;
|