mirror of
https://github.com/MinazukiAmane/Tinasha-Bot.git
synced 2025-03-15 19:55:58 +08:00
better way to auto reconnect?
still not tested yet, need to be tested more deep
This commit is contained in:
parent
0640fa6d1a
commit
febb665b71
@ -59,9 +59,12 @@ module.exports = {
|
|||||||
// Lavalink Websocket configuration
|
// Lavalink Websocket configuration
|
||||||
LAVALINK_WS: {
|
LAVALINK_WS: {
|
||||||
clientName: "Kiera-Bot", // The name of the lavalink client.
|
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: {
|
reconnecting: {
|
||||||
tries: 1000, // Number of times to attempt reconnecting.
|
tries: 10, // Number of times to attempt reconnecting.
|
||||||
delay: 20000 // Delay
|
delay: 20000 // Delay
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -20,14 +20,90 @@ module.exports = (client) => {
|
|||||||
|
|
||||||
lavaclient.on("nodeConnected", (node, event) => {
|
lavaclient.on("nodeConnected", (node, event) => {
|
||||||
client.logger.log(`Node "${node.identifier}" connected`);
|
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`);
|
client.logger.log(`Node "${node.identifier}" disconnected`);
|
||||||
const reconnectInterval = 20000; // Time in MS, change as needed.
|
|
||||||
setTimeout(() => {
|
// Log code and reason why node is disconnected. And inform that node is trying reconnecting
|
||||||
node.connect();
|
client.logger.log(`Code "${event.code}"`);
|
||||||
}, reconnectInterval);
|
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) => {
|
lavaclient.on("nodeError", (node, error) => {
|
||||||
|
@ -4,7 +4,6 @@ const pino = require("pino");
|
|||||||
|
|
||||||
const webhookLogger = process.env.ERROR_LOGS ? new WebhookClient({ url: process.env.ERROR_LOGS }) : undefined;
|
const webhookLogger = process.env.ERROR_LOGS ? new WebhookClient({ url: process.env.ERROR_LOGS }) : undefined;
|
||||||
|
|
||||||
const today = new Date();
|
|
||||||
const pinoLogger = pino.default(
|
const pinoLogger = pino.default(
|
||||||
{
|
{
|
||||||
level: "debug",
|
level: "debug",
|
||||||
@ -19,7 +18,7 @@ const pinoLogger = pino.default(
|
|||||||
translateTime: "yyyy-mm-dd HH:mm:ss",
|
translateTime: "yyyy-mm-dd HH:mm:ss",
|
||||||
ignore: "pid,hostname",
|
ignore: "pid,hostname",
|
||||||
singleLine: false,
|
singleLine: false,
|
||||||
hideObject: true,
|
hideObject: false,
|
||||||
customColors: "info:blue,warn:yellow,error:red",
|
customColors: "info:blue,warn:yellow,error:red",
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -27,7 +26,7 @@ const pinoLogger = pino.default(
|
|||||||
{
|
{
|
||||||
level: "debug",
|
level: "debug",
|
||||||
stream: pino.destination({
|
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,
|
sync: true,
|
||||||
mkdir: true,
|
mkdir: true,
|
||||||
}),
|
}),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user