Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect node and resume player if lavalink node is disconnected. #349

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
],
},
Expand Down
83 changes: 82 additions & 1 deletion src/handlers/lavaclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -81,3 +161,4 @@ module.exports = (client) => {

return lavaclient;
};

5 changes: 2 additions & 3 deletions src/helpers/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -19,15 +18,15 @@ 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",
},
}),
},
{
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,
}),
Expand Down