fix(docker): Update environment variables and improve connection handling for Lavalink

This commit is contained in:
Jose Daniel G. Percy 2025-04-24 00:05:57 +08:00
parent 0b86b5d891
commit 537a8c6709
4 changed files with 50 additions and 16 deletions

View File

@ -1,7 +1,7 @@
server: server:
port: 2333 port: 2333
address: 0.0.0.0 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: logging:
level: level:
@ -22,4 +22,10 @@ lavalink:
bufferDurationMs: 400 bufferDurationMs: 400
frameBufferDurationMs: 5000 frameBufferDurationMs: 5000
gc-warnings: true gc-warnings: true
plugins: [] 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"]

View File

@ -2,16 +2,22 @@ services:
lavalink: lavalink:
image: fredboat/lavalink:latest image: fredboat/lavalink:latest
container_name: lavalink container_name: lavalink
restart: unless-stopped
networks:
- bot-network
ports: ports:
- "2333:2333" - "2333:2333"
environment: environment:
- LAVALINK_PASSWORD=${LAVALINK_PASSWORD} - LAVALINK_SERVER_PASSWORD=${LAVALINK_PASSWORD}
volumes: volumes:
- ./application.yml:/opt/Lavalink/application.yml:ro,Z - ./application.yml:/opt/Lavalink/application.yml:ro,Z
bot: bot:
build: . build: .
container_name: discord-music-bot container_name: discord-music-bot
restart: unless-stopped
networks:
- bot-network
env_file: env_file:
- .env - .env
environment: environment:
@ -20,3 +26,7 @@ services:
- LAVALINK_PASSWORD=${LAVALINK_PASSWORD} - LAVALINK_PASSWORD=${LAVALINK_PASSWORD}
depends_on: depends_on:
- lavalink - lavalink
networks:
bot-network:
driver: bridge

View File

@ -5,11 +5,19 @@ const loadErelaEvents = require('../structures/ErelaEvents'); // Import the Erel
module.exports = { module.exports = {
name: Events.ClientReady, name: Events.ClientReady,
once: true, // This event should only run once once: true, // This event should only run once
execute(client) { async execute(client) {
logger.info(`Ready! Logged in as ${client.user.tag}`); 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 // Initialize the Erela Manager once the client is ready
try { 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); client.manager.init(client.user.id);
logger.info(`Erela.js Manager initialized for user ID: ${client.user.id}`); logger.info(`Erela.js Manager initialized for user ID: ${client.user.id}`);
@ -18,14 +26,19 @@ module.exports = {
} catch (error) { } catch (error) {
logger.error(`Failed to initialize Erela.js Manager: ${error.message}`); 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. // Set activity status
// client.user.setActivity('Music!', { type: 'LISTENING' }); client.user.setActivity('Music | /play', { 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.
}, },
}; };

View File

@ -28,14 +28,16 @@ const client = new Client({
}); });
// Initialize Erela.js Manager // Initialize Erela.js Manager
// We need the client to be ready before fully initializing the manager
client.manager = new Manager({ client.manager = new Manager({
nodes: [ nodes: [
{ {
host: process.env.LAVALINK_HOST || 'localhost', // Default host if not set host: process.env.LAVALINK_HOST || 'localhost',
port: parseInt(process.env.LAVALINK_PORT || '2333'), // Default port if not set port: parseInt(process.env.LAVALINK_PORT || '2333'),
password: process.env.LAVALINK_PASSWORD || 'youshallnotpass', // Default password if not set password: process.env.LAVALINK_PASSWORD || 'youshallnotpass',
secure: process.env.LAVALINK_SECURE === 'true', // Optional: Use true for wss:// 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 // 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 // Collections for commands
client.commands = new Collection(); client.commands = new Collection();