eximiabots-radiox/client/events/voiceStateUpdate.js

46 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-03-02 21:38:42 +02:00
module.exports = {
name: 'voiceStateUpdate',
2020-03-07 22:07:54 +02:00
async execute(client, oldState, newState) {
2020-03-11 15:21:15 +02:00
if (oldState.channel === null) return;
2020-03-07 22:07:54 +02:00
let change = false;
const radio = client.radio.get(newState.guild.id);
if (!radio) return;
if (newState.member.id === client.user.id && oldState.member.id === client.user.id) {
2020-03-11 15:21:15 +02:00
if (newState.channel === null) {
2020-04-02 10:34:04 +03:00
client.funcs.statisticsUpdate(client, newState.guild, radio);
2020-03-08 16:19:24 +02:00
return client.radio.delete(newState.guild.id);
2020-03-02 21:38:42 +02:00
}
2020-03-11 15:21:15 +02:00
const newPermissions = newState.channel.permissionsFor(newState.client.user);
if (!newPermissions.has('CONNECT') || !newPermissions.has('SPEAK') || !newPermissions.has('VIEW_CHANNEL')) {
try {
const connection = await oldState.channel.join();
2020-03-12 01:22:53 +02:00
return radio.connection = connection;
2020-03-11 15:21:15 +02:00
} catch (error) {
2020-04-02 10:34:04 +03:00
client.funcs.statisticsUpdate(client, newState.guild, radio);
2020-03-11 15:21:15 +02:00
radio.connection.dispatcher.destroy();
radio.voiceChannel.leave();
client.radio.delete(oldState.guild.id);
2020-03-13 00:53:23 +02:00
return;
2020-03-11 15:21:15 +02:00
}
}
2020-03-12 01:22:53 +02:00
if (newState.channel !== radio.voiceChannel) {
change = true;
radio.voiceChannel = newState.channel;
radio.connection = newState.connection;
}
2020-03-07 22:07:54 +02:00
}
if (oldState.channel.members.size === 1 && oldState.channel === radio.voiceChannel || change) {
setTimeout(() => {
2020-08-03 13:02:52 +03:00
if (!radio || !radio.connection.dispatcher || !radio.connection.dispatcher === null) return;
2020-03-07 22:07:54 +02:00
if (radio.voiceChannel.members.size === 1) {
2020-04-02 10:34:04 +03:00
client.funcs.statisticsUpdate(client, newState.guild, radio);
2020-03-08 21:20:47 +02:00
radio.connection.dispatcher.destroy();
2020-03-11 13:41:25 +02:00
radio.voiceChannel.leave();
client.radio.delete(newState.guild.id);
2020-03-07 22:07:54 +02:00
}
2020-03-11 13:42:01 +02:00
}, 120000);
2020-03-02 21:38:42 +02:00
}
}
2020-08-03 13:02:52 +03:00
};