diff --git a/main.js b/main.js index 6d63e324..f558f9ed 100644 --- a/main.js +++ b/main.js @@ -190,4 +190,34 @@ bot.on('guildDelete', guild => { console.log(`[G ${guild.id}] Left ${guild.name}`); }); -bot.login(token); \ No newline at end of file +bot.login(token); + +let inprg = false; +async function shuttingDown() { + if (inprg) return; + inprg = true; + console.log('[Warden] Shutting down...'); + if (startup) { + console.log('[Warden] Disconnecting from all guilds...'); + for (const pair of bot.music.players) { + const player = pair[1]; + console.log(`[G ${player.guildId}] Disconnecting (restarting)`); + await player.queue.channel.send({ + embeds: [ + new MessageEmbed() + .setDescription('Warden is restarting and will disconnect.') + .setFooter('Sorry for the inconvenience caused.') + .setColor(defaultColor), + ], + }); + player.disconnect(); + bot.music.destroyPlayer(player.guildId); + } + } + bot.destroy(); + process.exit(); +} + +['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach(eventType => { + process.on(eventType, shuttingDown); +}); \ No newline at end of file