Skip to content

Commit

Permalink
feat: move DISCORD_AUTHORIZATION_TOKEN to .env
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 8, 2022
1 parent 3c14cbe commit 42098b9
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The Discord authorization token for the bot, requires the MESSAGE_CONTENT intent
DISCORD_AUTHORIZATION_TOKEN=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
configuration.json
configuration.json
.env
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion configuration.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"$schema": "./configuration.schema.json",
"discord_authorization_token": "",
"administrators": {
"roles": [0],
"users": [0]
Expand Down
5 changes: 0 additions & 5 deletions configuration.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::sync::Arc;

use chrono::{DateTime, Duration, NaiveDateTime, Utc};
Expand Down Expand Up @@ -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::<BotConfiguration>(Arc::new(RwLock::new(configuration)));

// Start the Discord bot.
if let Err(why) = client.start().await {
error!("{:?}", why);
} else {
Expand Down
1 change: 0 additions & 1 deletion src/model/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Introduction>,
pub message_responses: Vec<MessageResponse>,
Expand Down

0 comments on commit 42098b9

Please sign in to comment.