diff --git a/config.js b/config.js index 33db48fed..a7024ef7d 100644 --- a/config.js +++ b/config.js @@ -64,6 +64,14 @@ module.exports = { password: "youshallnotpass", id: "Local Node", secure: false, + resuming: { // resumeConfiguration + key: "Your resuming key", + timeout: 60000 // after 60 seconds the bot will not resume. + }, + reconnect: { // reconnectConfiguration + tries: 10, + delay: 10000 + }, }, ], }, diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index af1364d71..780887eaf 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -27,10 +27,90 @@ module.exports = (client) => { lavaclient.on("nodeConnect", (node, event) => { client.logger.log(`Node "${node.id}" connected`); + + // Because sometimes the player is disconnected and cannot resume or play again (under investigation). + node.players.forEach(async (player) => { + try { + if (player.queue.tracks.length > 0) { + // Only player have tracks in queue + if (!player.connected) player.connect(); // Not connected but have tracks in queue because node is disconnected for a long time + if (player.paused) player.resume(); // Or user paused the player + if (!player.playing) player.play(); // If connected but not playing for some reasons + + const rePlayInterval = setInterval(async () => { + // Update player to re-play current song when player is connected but stuck at current song for some reasons (under investigation). + if (player.connected && player.playing) { + if (player.playingSince + player.queue.current.length < new Date.now()) { + player.queue.tracks.unshift(player.queue.current); + await player.queue.skip(); + } + } else { + if (!player.connected) { + client.logger.error("Player is not connected to any voice channel."); + player.stop(); + clearInterval(rePlayInterval); + } else if (!player.playing) { + client.logger.error( + "Player is paused or not playing. Try playing if there is at least 1 song in queue." + ); + if (player.queue.current.length > 0) { + if (player.paused) { + client.logger.debug( + `Player is paused and there is ${player.queue.current.length} ${ + player.queue.current.length > 1 ? "songs" : "song" + } in queue. Trying to resume...` + ); + player.resume(); + } else { + client.logger.debug( + `Player is not playing and there is ${player.queue.current.length} ${ + player.queue.current.length > 1 ? "songs" : "song" + } in queue. Trying to play...` + ); + player.play(); + } + } + } + } + }, 1000); + } + } catch (e) { + client.logger.log(player.queue.tracks.length); + } + }); }); - lavaclient.on("nodeDisconnect", (node, event) => { + lavaclient.on("nodeDisconnect", async (node, event) => { client.logger.log(`Node "${node.id}" disconnected`); + + // Log code and reason why node is disconnected. And inform that node is trying reconnecting + client.logger.log(`Code "${event.code}"`); + client.logger.log(`Reason: ${event.reason}`); + client.logger.log(`Node "${node.id}" reconnecting...`); + + // Try reconnecting node + if (node.conn.canReconnect) { + // If node can reconnect + while (node.conn.reconnectTry <= 10) { + // Try reconnecting again and again until connection is established or max connection attempts exceeded + if (node.conn.active) break; // if connection is established so exit loop + if (!node.conn.canReconnect) { + // If cannot reconnect + client.logger.log(`Node "${node.id}" reconnect failed!`); + node.conn.connect(); // We need to connect by hand because node cannot reconnect + break; + } + await node.conn.reconnect(); // Try reconnect and wait for response + } + if (node.conn.reconnectTry > 10) { + // Max connection attempts exceeded + client.logger.log(`Node "${node.id}" reconnect try times exceed!`); + node.conn.connect(); // We need to connect by hand because node cannot reconnect + } + } else { + // Else, we need to connect by hand + node.conn.connect(); + } }); lavaclient.on("nodeError", (node, error) => { @@ -81,3 +161,4 @@ module.exports = (client) => { return lavaclient; }; + diff --git a/src/helpers/Logger.js b/src/helpers/Logger.js index 6b0875a89..32dc50466 100644 --- a/src/helpers/Logger.js +++ b/src/helpers/Logger.js @@ -4,7 +4,6 @@ const pino = require("pino"); const webhookLogger = process.env.ERROR_LOGS ? new WebhookClient({ url: process.env.ERROR_LOGS }) : undefined; -const today = new Date(); const pinoLogger = pino.default( { level: "debug", @@ -19,7 +18,7 @@ const pinoLogger = pino.default( translateTime: "yyyy-mm-dd HH:mm:ss", ignore: "pid,hostname", singleLine: false, - hideObject: true, + hideObject: false, customColors: "info:blue,warn:yellow,error:red", }, }), @@ -27,7 +26,7 @@ const pinoLogger = pino.default( { level: "debug", stream: pino.destination({ - dest: `${process.cwd()}/logs/combined-${today.getFullYear()}.${today.getMonth() + 1}.${today.getDate()}.log`, + dest: `${process.cwd()}/logs/combined-${new Date().getFullYear()}.${new Date().getMonth() + 1}.${new Date().getDate()}.log`, sync: true, mkdir: true, }),