2021-08-04 11:47:27 +00:00
|
|
|
const {
|
|
|
|
getVoiceConnection,
|
|
|
|
joinVoiceChannel
|
|
|
|
} = require("@discordjs/voice");
|
2021-08-04 11:57:02 +00:00
|
|
|
const { createDiscordJSAdapter } = require("../utils/adapter");
|
2021-08-04 11:47:27 +00:00
|
|
|
|
2020-03-02 19:38:42 +00:00
|
|
|
module.exports = {
|
2021-01-21 12:14:58 +00:00
|
|
|
name: "voiceStateUpdate",
|
|
|
|
async execute(client, oldState, newState) {
|
|
|
|
if (oldState.channel === null) return;
|
|
|
|
let change = false;
|
|
|
|
const radio = client.radio.get(newState.guild.id);
|
|
|
|
if (!radio) return;
|
2020-03-11 13:21:15 +00:00
|
|
|
|
2021-01-21 12:14:58 +00:00
|
|
|
if (newState.member.id === client.user.id && oldState.member.id === client.user.id) {
|
2021-08-04 12:14:12 +00:00
|
|
|
|
2021-01-21 12:14:58 +00:00
|
|
|
if (newState.channel === null) {
|
|
|
|
client.funcs.statisticsUpdate(client, newState.guild, radio);
|
2021-08-22 15:48:40 +00:00
|
|
|
radio.connection?.destroy();
|
2021-08-04 14:46:06 +00:00
|
|
|
radio.audioPlayer?.stop();
|
2021-08-22 17:51:02 +00:00
|
|
|
client.funcs.logger('Radio', 'Stream stopped' + " / " + newState.guild.id);
|
2021-01-21 12:14:58 +00:00
|
|
|
return client.radio.delete(newState.guild.id);
|
|
|
|
}
|
2021-01-21 12:05:22 +00:00
|
|
|
|
2021-01-21 12:14:58 +00:00
|
|
|
const newPermissions = newState.channel.permissionsFor(newState.client.user);
|
|
|
|
if (!newPermissions.has("CONNECT") || !newPermissions.has("SPEAK") || !newPermissions.has("VIEW_CHANNEL")) {
|
|
|
|
try {
|
|
|
|
setTimeout(
|
2021-08-04 11:34:41 +00:00
|
|
|
async () => (
|
2021-08-04 11:57:02 +00:00
|
|
|
radio.connection = joinVoiceChannel({
|
2021-08-04 11:34:41 +00:00
|
|
|
channelId: oldState.channel.id,
|
|
|
|
guildId: oldState.channel.guild.id,
|
|
|
|
adapterCreator: createDiscordJSAdapter(oldState.channel)
|
|
|
|
})
|
|
|
|
//radio.connection = await oldState.channel.join()
|
|
|
|
),
|
2021-01-21 12:14:58 +00:00
|
|
|
1000
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
client.funcs.statisticsUpdate(client, newState.guild, radio);
|
2021-08-04 14:46:06 +00:00
|
|
|
radio.connection?.destroy();
|
|
|
|
radio.audioPlayer?.stop();
|
2021-08-22 17:51:02 +00:00
|
|
|
client.funcs.logger('Radio', 'Stream stopped' + " / " + newState.guild.id);
|
2021-01-21 12:14:58 +00:00
|
|
|
client.radio.delete(oldState.guild.id);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newState.channel !== radio.voiceChannel) {
|
|
|
|
change = true;
|
|
|
|
radio.voiceChannel = newState.channel;
|
2021-08-04 11:42:37 +00:00
|
|
|
radio.connection = getVoiceConnection(newState.channel.guild.id);
|
2021-08-04 11:34:41 +00:00
|
|
|
//radio.connection = await newState.channel.join();
|
2021-01-21 12:14:58 +00:00
|
|
|
}
|
2021-01-21 12:05:22 +00:00
|
|
|
}
|
2021-01-21 12:14:58 +00:00
|
|
|
if ((oldState.channel.members.size === 1 && oldState.channel === radio.voiceChannel) || change) {
|
|
|
|
setTimeout(() => {
|
2021-08-04 14:46:06 +00:00
|
|
|
if (!radio || !radio.connection || !radio.connection === null) return;
|
2021-01-21 12:14:58 +00:00
|
|
|
if (radio.voiceChannel.members.size === 1) {
|
|
|
|
client.funcs.statisticsUpdate(client, newState.guild, radio);
|
2021-08-04 14:46:06 +00:00
|
|
|
radio.connection?.destroy();
|
|
|
|
radio.audioPlayer?.stop();
|
2021-08-22 17:51:02 +00:00
|
|
|
client.funcs.logger('Radio', 'Stream stopped' + " / " + newState.guild.id);
|
2021-01-21 12:14:58 +00:00
|
|
|
client.radio.delete(newState.guild.id);
|
|
|
|
}
|
2021-08-22 15:48:40 +00:00
|
|
|
}, 60000);
|
2020-03-02 19:38:42 +00:00
|
|
|
}
|
2021-01-21 12:14:58 +00:00
|
|
|
},
|
2020-08-03 10:02:52 +00:00
|
|
|
};
|