diff --git a/config.js b/config.js index ffa177b..6e9a480 100644 --- a/config.js +++ b/config.js @@ -59,9 +59,12 @@ module.exports = { // Lavalink Websocket configuration LAVALINK_WS: { clientName: "Kiera-Bot", // The name of the lavalink client. - resuming: true, // Whether Lavalink should attempt to resume existing sessions when reconnecting. + resuming: { + key: "prod-kiera", // ganti key nya jangan pakai default ini + timeout: 60000 // after 60 seconds the bot will not resume. + }, reconnecting: { - tries: 1000, // Number of times to attempt reconnecting. + tries: 10, // Number of times to attempt reconnecting. delay: 20000 // Delay } }, diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index 3004e63..77ddb8b 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -20,14 +20,90 @@ module.exports = (client) => { lavaclient.on("nodeConnected", (node, event) => { client.logger.log(`Node "${node.identifier}" 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("nodeDisconnected", (node, event) => { + lavaclient.on("nodeDisconnected", async (node, event) => { client.logger.log(`Node "${node.identifier}" disconnected`); - const reconnectInterval = 20000; // Time in MS, change as needed. - setTimeout(() => { - node.connect(); - }, reconnectInterval); + + // 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.identifier}" 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.identifier}" 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.identifier}" 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) => { diff --git a/src/helpers/logger.js b/src/helpers/logger.js index 6b0875a..32dc504 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, }),