feat: move DISCORD_AUTHORIZATION_TOKEN to .env

This commit is contained in:
oSumAtrIX 2022-07-08 22:26:05 +02:00
parent 3c14cbe6a4
commit 42098b9db5
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
8 changed files with 29 additions and 9 deletions

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
# The Discord authorization token for the bot, requires the MESSAGE_CONTENT intent
DISCORD_AUTHORIZATION_TOKEN=

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target /target
configuration.json configuration.json
.env

7
Cargo.lock generated
View File

@ -152,6 +152,12 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.31" version = "0.8.31"
@ -678,6 +684,7 @@ name = "revanced-discord-bot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"dotenv",
"log", "log",
"regex", "regex",
"serde", "serde",

View File

@ -18,6 +18,7 @@ panic = "abort"
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
dotenv = "0.15"
serde_json = "1.0" serde_json = "1.0"
tokio = { version = "1.0", features = ["rt-multi-thread"] } tokio = { version = "1.0", features = ["rt-multi-thread"] }
log = "0.4" log = "0.4"

View File

@ -1,6 +1,5 @@
{ {
"$schema": "./configuration.schema.json", "$schema": "./configuration.schema.json",
"discord_authorization_token": "",
"administrators": { "administrators": {
"roles": [0], "roles": [0],
"users": [0] "users": [0]

View File

@ -4,12 +4,7 @@
"title": "Configuration schema", "title": "Configuration schema",
"description": "The Revanced Discord bot configuration schema.", "description": "The Revanced Discord bot configuration schema.",
"type": "object", "type": "object",
"required": ["discord_authorization_token"],
"properties": { "properties": {
"discord_authorization_token": {
"type": "string",
"description": "The authorization token for the Discord bot."
},
"administrators": { "administrators": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -1,3 +1,4 @@
use std::env;
use std::sync::Arc; use std::sync::Arc;
use chrono::{DateTime, Duration, NaiveDateTime, Utc}; use chrono::{DateTime, Duration, NaiveDateTime, Utc};
@ -223,22 +224,37 @@ impl EventHandler for Handler {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Initialize the logging framework.
log::set_logger(&LOGGER) log::set_logger(&LOGGER)
.map(|()| log::set_max_level(LevelFilter::Warn)) .map(|()| log::set_max_level(LevelFilter::Warn))
.expect("Could not set logger."); .expect("Could not set logger.");
// Set up the configuration.
let configuration = load_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( let mut client = Client::builder(
&configuration.discord_authorization_token, &token,
GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT, GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT,
) )
.event_handler(Handler) .event_handler(Handler)
.await .await
.expect("Failed to create client"); .expect("Failed to create client");
// Save the configuration.
client.data.write().await.insert::<BotConfiguration>(Arc::new(RwLock::new(configuration))); client.data.write().await.insert::<BotConfiguration>(Arc::new(RwLock::new(configuration)));
// Start the Discord bot.
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
error!("{:?}", why); error!("{:?}", why);
} else { } else {

View File

@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize)] #[derive(Default, Serialize, Deserialize)]
pub struct Configuration { pub struct Configuration {
pub discord_authorization_token: String,
pub administrators: Administrators, pub administrators: Administrators,
pub thread_introductions: Vec<Introduction>, pub thread_introductions: Vec<Introduction>,
pub message_responses: Vec<MessageResponse>, pub message_responses: Vec<MessageResponse>,