diff --git a/bin/radio_horizon_production.dart b/bin/radio_horizon_production.dart index fdd9213..7ee5256 100644 --- a/bin/radio_horizon_production.dart +++ b/bin/radio_horizon_production.dart @@ -35,6 +35,12 @@ Future main() async { }, ); + final commands = CommandsPlugin( + prefix: null, + guild: devGuildId, + options: CommandsOptions(logErrors: dev), + ); + final lavalinkClient = await LavalinkClient.connect( Uri( host: lavalinkAddress, @@ -47,22 +53,17 @@ Future main() async { final lavalinkPlugin = LavalinkPlugin.usingClient(lavalinkClient); - final commands = CommandsPlugin( - prefix: mentionOr((_) => prefix), - options: const CommandsOptions( - logErrors: false, - ), - ) + commands ..addCommand(info) - // ..addCommand(skip) - // ..addCommand(stop) - // ..addCommand(join) - // ..addCommand(leave) - // ..addCommand(pause) - // ..addCommand(resume) - // ..addCommand(volume) - ..addCommand(music); - // ..addCommand(radio); + ..addCommand(skip) + ..addCommand(stop) + ..addCommand(join) + ..addCommand(leave) + ..addCommand(pause) + ..addCommand(resume) + ..addCommand(volume) + ..addCommand(music) + ..addCommand(radio); commands.onCommandError.listen(commandErrorHandler); diff --git a/docker-compose.yml b/docker-compose.yml index dd7cda5..fff4bde 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,9 +24,6 @@ services: - 2333 volumes: - ./output.lavalink.yml:/opt/Lavalink/application.yml - depends_on: - update_lavalink_config: - condition: service_completed_successfully shazam_api: build: @@ -35,15 +32,3 @@ services: - 5000 environment: FLASK_ENV: development - - update_lavalink_config: - build: - context: ./custom_lavalink - container_name: update_lavalink_config - restart: no - volumes: - - ./lavalink.yml:/app/lavalink.yml - - ./:/output-dir - env_file: - - .env/.env.development - command: ["--input", "lavalink.yml", "--output", "/output-dir/output.lavalink.yml"] diff --git a/lib/src/settings.dart b/lib/src/settings.dart index 679f1da..d3e318e 100644 --- a/lib/src/settings.dart +++ b/lib/src/settings.dart @@ -68,7 +68,7 @@ final bool dev = getEnvBool('RG_DEV'); final devGuildId = dev ? Snowflake.parse(getEnv('RG_DEV_GUILD_ID')) : null; /// The bot's app id. -final clientId = dev ? Snowflake.parse(getEnv('CLIENT_ID')) : null; +final clientId = Snowflake.parse(getEnv('CLIENT_ID')); /// The address of the lavalink running server to connect to. String lavalinkAddress = getEnv('LAVALINK_ADDRESS'); @@ -82,12 +82,6 @@ String lavalinkPassword = getEnv('LAVALINK_PASSWORD'); /// Whether to use or not ssl to establish a connection. bool lavalinkUseSSL = getEnvBool('LAVALINK_USE_SSL', def: false); -/// The api key for the song recognition service, in Rapid Api. -/// -/// Find yours in https://rapidapi.com/mirzahadjaevaguzal/api/shazam-song-recognizer -String rapidapiShazamSongRecognizerKey = - getEnv('RAPIDAPI_SHAZAM_SONG_RECOGNIZER_KEY'); - /// The Sentry DSN to use for this instance. String sentryDsn = getEnv('SENTRY_DSN'); diff --git a/portainer.yml b/portainer.yml index 8964781..601728b 100644 --- a/portainer.yml +++ b/portainer.yml @@ -11,7 +11,6 @@ services: depends_on: - lavalink - shazam_api - # mount a volume for the .env file volumes: - /server/docker/radiohorizon/config/.env:/app/.env @@ -20,9 +19,10 @@ services: container_name: update_lavalink_config restart: no volumes: - - /server/docker/radiohorizon/config/:/output-dir + - /server/docker/radiohorizon/config/.env:/app/.env + - /server/docker/radiohorizon/config:/output-dir command: - ["--input", "lavalink.yml", "--output", "/output-dir/output.lavalink.yml"] + ["--input", "/output-dir/lavalink.yml", "--output", "/output-dir/output.lavalink.yml", "--env", "production"] lavalink: image: ghcr.io/lavalink-devs/lavalink:4 @@ -31,7 +31,7 @@ services: expose: - 2333 volumes: - - /server/docker/radiohorizon/config/lavalink.yml:/opt/Lavalink/application.yml + - /server/docker/radiohorizon/config/output.lavalink.yml:/opt/Lavalink/application.yml depends_on: update_lavalink_config: condition: service_completed_successfully diff --git a/update_lavalink_config/Dockerfile b/update_lavalink_config/Dockerfile index 77ec9fa..ec0c6f9 100644 --- a/update_lavalink_config/Dockerfile +++ b/update_lavalink_config/Dockerfile @@ -11,7 +11,7 @@ COPY . /app RUN dart pub get # Compile the Dart script to a native executable (optional but recommended) -RUN dart compile exe update_lavalink_config.dart -o update_lavalink_config +RUN dart compile exe bin/update_lavalink_config.dart -o update_lavalink_config # Set the entrypoint to the compiled executable ENTRYPOINT ["./update_lavalink_config"] diff --git a/update_lavalink_config/update_lavalink_config.dart b/update_lavalink_config/bin/update_lavalink_config.dart similarity index 81% rename from update_lavalink_config/update_lavalink_config.dart rename to update_lavalink_config/bin/update_lavalink_config.dart index a68bb64..39badfc 100644 --- a/update_lavalink_config/update_lavalink_config.dart +++ b/update_lavalink_config/bin/update_lavalink_config.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:args/args.dart'; +import 'package:custom_lavalink/dotenv.dart'; import 'package:yaml_edit/yaml_edit.dart'; // A tool that automatically edits the lavalink.yml file to populate the @@ -46,6 +47,13 @@ void main(List args) { help: 'Deezer master decryption key. Will default to the ' 'DEEZER_MASTER_DECRYPTION_KEY environment variable.', ) + ..addOption( + 'env', + abbr: 'e', + defaultsTo: 'none', + allowed: ['development', 'production', 'none'], + help: 'Environment to use. Defaults to "development"', + ) ..addFlag( 'clear', negatable: false, @@ -73,10 +81,28 @@ void main(List args) { final inputFilePath = argResults['input'] as String; final outputFilePath = argResults['output'] as String? ?? inputFilePath; + final env = argResults['env'] as String; + + // Load the environment variables + if (env != 'none') { + if (env == 'development') { + dotEnvFlavour = DotEnvFlavour.development; + stdout.writeln('Loading environment variables from .env.development'); + } else if (env == 'production') { + dotEnvFlavour = DotEnvFlavour.production; + stdout.writeln('Loading environment variables from .env.production'); + } + + dotEnvFlavour.initialize(); + } + // Helper function to get environment variable or command-line argument String? getEnvOrArg(String argName, String envName) { if (clear) return null; - return argResults[argName] as String? ?? Platform.environment[envName]; + return argResults[argName] as String? ?? + dotEnvFlavour.dotenv[envName] ?? + Platform.environment[envName] ?? + String.fromEnvironment(envName); } // Retrieve the environment variables @@ -89,6 +115,8 @@ void main(List args) { 'DEEZER_MASTER_DECRYPTION_KEY', ); + stdout.writeln('Updating lavalink.yml file...'); + // Ensure required variables are not null if ((lavalinkPassword == null || youtubeOAuthRefreshToken == null || @@ -149,7 +177,10 @@ void main(List args) { final newContent = editor.toString(); final outputFile = File(outputFilePath); - outputFile.deleteSync(recursive: true); + if (outputFile.existsSync()) { + outputFile.deleteSync(recursive: true); + } + outputFile.createSync(recursive: true); outputFile.writeAsStringSync(newContent); diff --git a/update_lavalink_config/lib/dotenv.dart b/update_lavalink_config/lib/dotenv.dart new file mode 100644 index 0000000..108b932 --- /dev/null +++ b/update_lavalink_config/lib/dotenv.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2022, Tomás Sasovsky +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. + +import 'package:dotenv/dotenv.dart'; +import 'package:logging/logging.dart'; + +enum DotEnvFlavour { + development('.env/.env.development'), + production('.env/.env.production'); + + const DotEnvFlavour(this.path); + + final String path; + void initialize() { + try { + dotenv.load([path]); + } catch (_) { + Logger('DotEnv').warning('Failed to load $path'); + } + } + + DotEnv get dotenv => _dotEnv; +} + +var _dotEnv = DotEnv(includePlatformEnvironment: true); +late DotEnvFlavour dotEnvFlavour; diff --git a/update_lavalink_config/pubspec.yaml b/update_lavalink_config/pubspec.yaml index 32dc80e..1e9e524 100644 --- a/update_lavalink_config/pubspec.yaml +++ b/update_lavalink_config/pubspec.yaml @@ -8,4 +8,6 @@ environment: dependencies: args: ^2.6.0 + dotenv: ^4.2.0 + logging: ^1.3.0 yaml_edit: ^2.2.1