woomy/events/voiceStateUpdate.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-04-22 07:39:46 +00:00
// Copyright 2020 Emily J. / mudkipscience and contributors. Subject to the AGPLv3 license.
2020-04-22 07:58:25 +00:00
const music = require('../utils/music')
2020-04-22 07:39:46 +00:00
module.exports = (client, oldState, newState) => {
2020-04-22 07:58:25 +00:00
if (newState.channelID !== oldState.channelID) {
const guild = music.getGuild(newState.guild.id)
2020-04-22 07:39:46 +00:00
// Reset queue, dispatcher, etc if Woomy is forcibly disconnected from the queue
if (!guild.voiceChannel.members.get(client.user.id) && guild.queue.length > 0) {
guild.queue = []
guild.playing = false
guild.paused = false
guild.skippers = []
}
// Auto-disconnect feature
2020-04-22 07:58:25 +00:00
if (guild.playing && guild.voiceChannel.id === oldState.channelID) {
2020-04-22 09:00:57 +00:00
if (guild.voiceChannel.members.filter(member => !member.user.bot).size < 1) {
2020-04-22 07:58:25 +00:00
guild.autoDisconnect = true
2020-04-22 09:00:57 +00:00
guild.message.channel.send(`The music will end in 2 minutes if nobody rejoins **${guild.voiceChannel.name}**`)
.then(msg => {
msg.delete({ timeout: 120000 })
})
2020-04-22 07:58:25 +00:00
setTimeout(() => {
2020-04-22 09:00:57 +00:00
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 = []
2020-04-22 07:53:55 +00:00
2020-04-22 09:00:57 +00:00
guild.message.channel.send('The music has ended because no one was listening to me ;~;')
2020-04-22 07:58:25 +00:00
} else {
guild.autoDisconnect = false
}
2020-04-22 09:00:57 +00:00
}, 120000)
} else {
guild.autoDisconnect = false
2020-04-22 07:58:25 +00:00
}
2020-04-22 07:39:46 +00:00
}
2020-04-22 07:58:25 +00:00
}
}