2020-03-02 19:38:42 +00:00
|
|
|
module.exports = {
|
|
|
|
name: 'voiceStateUpdate',
|
2020-03-07 20:07:54 +00:00
|
|
|
async execute(client, oldState, newState) {
|
2020-03-11 13:21:15 +00:00
|
|
|
if (oldState.channel === null) return;
|
2020-03-07 20:07:54 +00: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 13:21:15 +00:00
|
|
|
if (newState.channel === null) {
|
2020-04-01 13:58:08 +00:00
|
|
|
statisticsUpdate(client, newState, radio);
|
2020-03-08 14:19:24 +00:00
|
|
|
return client.radio.delete(newState.guild.id);
|
2020-03-02 19:38:42 +00:00
|
|
|
}
|
2020-03-11 13:21:15 +00: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-11 23:22:53 +00:00
|
|
|
return radio.connection = connection;
|
2020-03-11 13:21:15 +00:00
|
|
|
} catch (error) {
|
2020-04-01 13:58:08 +00:00
|
|
|
statisticsUpdate(client, newState, radio);
|
2020-03-11 13:21:15 +00:00
|
|
|
radio.connection.dispatcher.destroy();
|
|
|
|
radio.voiceChannel.leave();
|
2020-04-01 13:58:08 +00:00
|
|
|
client.radio.delete(oldState.guild.id);
|
2020-03-12 22:53:23 +00:00
|
|
|
return;
|
2020-03-11 13:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-11 23:22:53 +00:00
|
|
|
if (newState.channel !== radio.voiceChannel) {
|
|
|
|
change = true;
|
|
|
|
radio.voiceChannel = newState.channel;
|
|
|
|
radio.connection = newState.connection;
|
|
|
|
}
|
2020-03-07 20:07:54 +00:00
|
|
|
}
|
|
|
|
if (oldState.channel.members.size === 1 && oldState.channel === radio.voiceChannel || change) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!radio) return;
|
|
|
|
if (radio.voiceChannel.members.size === 1) {
|
2020-04-01 13:58:08 +00:00
|
|
|
statisticsUpdate(client, newState, radio);
|
2020-03-08 19:20:47 +00:00
|
|
|
radio.connection.dispatcher.destroy();
|
2020-03-11 11:41:25 +00:00
|
|
|
radio.voiceChannel.leave();
|
|
|
|
client.radio.delete(newState.guild.id);
|
2020-03-07 20:07:54 +00:00
|
|
|
}
|
2020-03-11 11:42:01 +00:00
|
|
|
}, 120000);
|
2020-03-02 19:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 13:58:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function statisticsUpdate(client, currentState, radio) {
|
|
|
|
|
|
|
|
client.datastore.checkEntry(currentState.guild.id);
|
|
|
|
|
|
|
|
radio.currentGuild = client.datastore.getEntry(currentState.guild.id);
|
|
|
|
|
|
|
|
if(!radio.currentGuild.statistics[radio.station.name]){
|
|
|
|
radio.currentGuild.statistics[radio.station.name] = {};
|
|
|
|
radio.currentGuild.statistics[radio.station.name].time = 0;
|
|
|
|
radio.currentGuild.statistics[radio.station.name].used = 0;
|
|
|
|
client.datastore.updateEntry(currentState.guild, radio.currentGuild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!radio.connection.dispatcher){
|
|
|
|
let date = new Date();
|
|
|
|
radio.currentTime = date.getTime();
|
|
|
|
radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime);
|
|
|
|
radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.playTime);
|
|
|
|
} else {
|
|
|
|
radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.connection.dispatcher.streamTime.toFixed(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1;
|
|
|
|
client.datastore.updateEntry(currentState.guild, radio.currentGuild);
|
|
|
|
|
2020-03-08 19:20:47 +00:00
|
|
|
}
|