From 42098b9db54900cae49e6adaac39cbd1630a8de1 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Fri, 8 Jul 2022 22:26:05 +0200 Subject: [PATCH] feat: move `DISCORD_AUTHORIZATION_TOKEN` to `.env` --- .env.example | 2 ++ .gitignore | 3 ++- Cargo.lock | 7 +++++++ Cargo.toml | 1 + configuration.example.json | 1 - configuration.schema.json | 5 ----- src/main.rs | 18 +++++++++++++++++- src/model/application.rs | 1 - 8 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..13a342c --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +# The Discord authorization token for the bot, requires the MESSAGE_CONTENT intent +DISCORD_AUTHORIZATION_TOKEN= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4928fc5..6841fb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -configuration.json \ No newline at end of file +configuration.json +.env \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 54377be..12f106f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "encoding_rs" version = "0.8.31" @@ -678,6 +684,7 @@ name = "revanced-discord-bot" version = "0.1.0" dependencies = [ "chrono", + "dotenv", "log", "regex", "serde", diff --git a/Cargo.toml b/Cargo.toml index 541548c..94938d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ panic = "abort" [dependencies] serde = { version = "1.0", features = ["derive"] } +dotenv = "0.15" serde_json = "1.0" tokio = { version = "1.0", features = ["rt-multi-thread"] } log = "0.4" diff --git a/configuration.example.json b/configuration.example.json index 9267855..9a042ae 100644 --- a/configuration.example.json +++ b/configuration.example.json @@ -1,6 +1,5 @@ { "$schema": "./configuration.schema.json", - "discord_authorization_token": "", "administrators": { "roles": [0], "users": [0] diff --git a/configuration.schema.json b/configuration.schema.json index f4f92b8..e253c94 100644 --- a/configuration.schema.json +++ b/configuration.schema.json @@ -4,12 +4,7 @@ "title": "Configuration schema", "description": "The Revanced Discord bot configuration schema.", "type": "object", - "required": ["discord_authorization_token"], "properties": { - "discord_authorization_token": { - "type": "string", - "description": "The authorization token for the Discord bot." - }, "administrators": { "type": "object", "properties": { diff --git a/src/main.rs b/src/main.rs index 05fc0a7..bf6b578 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::env; use std::sync::Arc; use chrono::{DateTime, Duration, NaiveDateTime, Utc}; @@ -223,22 +224,37 @@ impl EventHandler for Handler { #[tokio::main] async fn main() { + // Initialize the logging framework. log::set_logger(&LOGGER) .map(|()| log::set_max_level(LevelFilter::Warn)) .expect("Could not set logger."); + // Set up the configuration. let configuration = load_configuration(); + // Get the Discord authorization token. + dotenv::dotenv().ok(); + let token = match env::vars().find(|(key, _)| key == "DISCORD_AUTHORIZATION_TOKEN") { + Some((_, value)) => value, + None => { + error!("Environment variable DISCORD_AUTHORIZATION_TOKEN unset."); + std::process::exit(1); + }, + }; + + // Create the Discord bot client. let mut client = Client::builder( - &configuration.discord_authorization_token, + &token, GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT, ) .event_handler(Handler) .await .expect("Failed to create client"); + // Save the configuration. client.data.write().await.insert::(Arc::new(RwLock::new(configuration))); + // Start the Discord bot. if let Err(why) = client.start().await { error!("{:?}", why); } else { diff --git a/src/model/application.rs b/src/model/application.rs index 4dd68be..89d9381 100644 --- a/src/model/application.rs +++ b/src/model/application.rs @@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize}; #[derive(Default, Serialize, Deserialize)] pub struct Configuration { - pub discord_authorization_token: String, pub administrators: Administrators, pub thread_introductions: Vec, pub message_responses: Vec,