From 26427d04707672f00d37144217badb326e4774e8 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 4 Dec 2021 11:12:00 +0800 Subject: [PATCH] feat: inform users of disconnection --- main.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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