From 537a8c670933d3905b24eda79e364de65d51e3b2 Mon Sep 17 00:00:00 2001 From: aki Date: Thu, 24 Apr 2025 00:05:57 +0800 Subject: [PATCH] fix(docker): Update environment variables and improve connection handling for Lavalink --- application.yml | 10 ++++++++-- docker-compose.yml | 12 +++++++++++- src/events/ready.js | 29 +++++++++++++++++++++-------- src/index.js | 15 ++++++++++----- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/application.yml b/application.yml index 06bd016..a17658e 100644 --- a/application.yml +++ b/application.yml @@ -1,7 +1,7 @@ server: port: 2333 address: 0.0.0.0 - password: ${LAVALINK_PASSWORD:changeme} # Uses env var or defaults to 'changeme' + password: ${LAVALINK_SERVER_PASSWORD:testing-lavalink-dont-touch} # Changed to use LAVALINK_SERVER_PASSWORD env var logging: level: @@ -22,4 +22,10 @@ lavalink: bufferDurationMs: 400 frameBufferDurationMs: 5000 gc-warnings: true - plugins: [] \ No newline at end of file + plugins: [] + +# Add specific IPs allowed to connect (your bot container) +# Set as 0.0.0.0/0 to allow connections from anywhere (not recommended for production) +# This is important for Docker environments + server.ip-rotation: false + server.ip-blocks: ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.1", "0.0.0.0/0"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1c174f9..609dfd5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,16 +2,22 @@ services: lavalink: image: fredboat/lavalink:latest container_name: lavalink + restart: unless-stopped + networks: + - bot-network ports: - "2333:2333" environment: - - LAVALINK_PASSWORD=${LAVALINK_PASSWORD} + - LAVALINK_SERVER_PASSWORD=${LAVALINK_PASSWORD} volumes: - ./application.yml:/opt/Lavalink/application.yml:ro,Z bot: build: . container_name: discord-music-bot + restart: unless-stopped + networks: + - bot-network env_file: - .env environment: @@ -20,3 +26,7 @@ services: - LAVALINK_PASSWORD=${LAVALINK_PASSWORD} depends_on: - lavalink + +networks: + bot-network: + driver: bridge diff --git a/src/events/ready.js b/src/events/ready.js index 18e9acf..e88cdde 100644 --- a/src/events/ready.js +++ b/src/events/ready.js @@ -5,11 +5,19 @@ const loadErelaEvents = require('../structures/ErelaEvents'); // Import the Erel module.exports = { name: Events.ClientReady, once: true, // This event should only run once - execute(client) { + async execute(client) { logger.info(`Ready! Logged in as ${client.user.tag}`); + // Wait a moment before initializing Erela.js to give Lavalink time to start up + // This is especially important in Docker environments + await new Promise(resolve => setTimeout(resolve, 5000)); // 5 second delay + // Initialize the Erela Manager once the client is ready try { + // Log the actual connection details being used (without exposing the password) + const node = client.manager.nodes[0]; + logger.info(`Attempting to connect to Lavalink at ${node.options.host}:${node.options.port} with identifier "${node.options.identifier}"`); + client.manager.init(client.user.id); logger.info(`Erela.js Manager initialized for user ID: ${client.user.id}`); @@ -18,14 +26,19 @@ module.exports = { } catch (error) { logger.error(`Failed to initialize Erela.js Manager: ${error.message}`); - // Depending on requirements, you might want to exit or handle this differently + // Try to reconnect after a delay if initialization fails + setTimeout(() => { + try { + client.manager.init(client.user.id); + logger.info(`Erela.js Manager initialization retry successful`); + loadErelaEvents(client); + } catch (retryError) { + logger.error(`Retry initialization also failed: ${retryError.message}`); + } + }, 10000); // 10 second delay before retry } - // Placeholder for setting activity, etc. - // client.user.setActivity('Music!', { type: 'LISTENING' }); - - // Note: Slash command registration is typically done in a separate deploy script, - // not usually within the ready event for production bots. - // We will create a deploy-commands.js script later. + // Set activity status + client.user.setActivity('Music | /play', { type: 'LISTENING' }); }, }; diff --git a/src/index.js b/src/index.js index 0cafc39..66fa7c8 100644 --- a/src/index.js +++ b/src/index.js @@ -28,14 +28,16 @@ const client = new Client({ }); // Initialize Erela.js Manager -// We need the client to be ready before fully initializing the manager client.manager = new Manager({ nodes: [ { - host: process.env.LAVALINK_HOST || 'localhost', // Default host if not set - port: parseInt(process.env.LAVALINK_PORT || '2333'), // Default port if not set - password: process.env.LAVALINK_PASSWORD || 'youshallnotpass', // Default password if not set - secure: process.env.LAVALINK_SECURE === 'true', // Optional: Use true for wss:// + host: process.env.LAVALINK_HOST || 'localhost', + port: parseInt(process.env.LAVALINK_PORT || '2333'), + password: process.env.LAVALINK_PASSWORD || 'youshallnotpass', + secure: process.env.LAVALINK_SECURE === 'true', + retryAmount: 10, // Number of connection attempts + retryDelay: 5000, // 5 seconds between each retry + identifier: "lavalink", // Identifier for logs }, ], // Function to send raw voice data to Discord @@ -45,6 +47,9 @@ client.manager = new Manager({ }, }); +// Show the actual Lavalink connection details (without exposing the actual password) +logger.info(`Lavalink connection configured to: ${process.env.LAVALINK_HOST}:${process.env.LAVALINK_PORT} (Password: ${process.env.LAVALINK_PASSWORD ? '[SET]' : '[NOT SET]'})`); + // Collections for commands client.commands = new Collection();