woomy/events/voiceStateUpdate.js
2020-04-22 19:00:57 +10:00

39 lines
1.4 KiB
JavaScript

// Copyright 2020 Emily J. / mudkipscience and contributors. Subject to the AGPLv3 license.
const music = require('../utils/music')
module.exports = (client, oldState, newState) => {
if (newState.channelID !== oldState.channelID) {
const guild = music.getGuild(newState.guild.id)
if (guild.playing && guild.voiceChannel.id === oldState.channelID) {
if (guild.voiceChannel.members.filter(member => !member.user.bot).size < 1) {
guild.autoDisconnect = true
guild.message.channel.send(`The music will end in 2 minutes if nobody rejoins **${guild.voiceChannel.name}**`)
.then(msg => {
msg.delete({ timeout: 120000 })
})
setTimeout(() => {
if (guild.dispatcher !== null && guild.voiceChannel.members.filter(member => !member.user.bot).size < 1 && guild.autoDisconnect) {
// Probably should be async? But no need here I think
guild.dispatcher.end('silent')
guild.queue = []
guild.playing = false
guild.paused = false
guild.dispatcher = null
guild.skippers = []
guild.message.channel.send('The music has ended because no one was listening to me ;~;')
} else {
guild.autoDisconnect = false
}
}, 120000)
} else {
guild.autoDisconnect = false
}
}
}
}