Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toml config #5

Merged
merged 12 commits into from Jun 22, 2022
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/target
/cache
config.toml
13 changes: 13 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -11,9 +11,11 @@ tracing = "0.1"
tracing-subscriber = "0.2"
tracing-futures = "0.2"
tokio = { version = "1.5.0", features = ["full"] }
toml = "0.5"
futures = "0.3.14"
byteorder = "1.4.3"
samplerate = "0.2.4"
oleggtro marked this conversation as resolved.
Show resolved Hide resolved
serde = "1.0"

[dependencies.serenity]
version = "0.10"
Expand Down
51 changes: 42 additions & 9 deletions src/main.rs
@@ -1,4 +1,5 @@
use std::env;
use std::fs::read_to_string;

use songbird::input;
use songbird::SerenityInit;
Expand All @@ -11,6 +12,8 @@ use lib::player::{SpotifyPlayer, SpotifyPlayerKey};
use librespot::core::mercury::MercuryError;
use librespot::playback::config::Bitrate;
use librespot::playback::player::PlayerEvent;
use toml::from_str;
use serde::Deserialize;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{sleep, Duration};
Expand All @@ -33,6 +36,18 @@ impl TypeMapKey for UserIdKey {
type Value = id::UserId;
}

#[derive(Deserialize)]
pub struct Config {
#[serde(rename = "DISCORD_TOKEN")]
pub discord_token: String,
#[serde(rename = "SPOTIFY_USERNAME")]
pub spotify_username: String,
#[serde(rename = "SPOTIFY_PASSWORD")]
pub spotify_password: String,
#[serde(rename = "DISCORD_USER_ID")]
pub discord_user_id: String,
}

#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, ctx: Context, ready: Ready) {
Expand Down Expand Up @@ -267,16 +282,13 @@ impl EventHandler for Handler {
async fn main() {
tracing_subscriber::fmt::init();

// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

let framework = StandardFramework::new();
let username =
env::var("SPOTIFY_USERNAME").expect("Expected a Spotify username in the environment");
let password =
env::var("SPOTIFY_PASSWORD").expect("Expected a Spotify password in the environment");
let user_id =
env::var("DISCORD_USER_ID").expect("Expected a Discord user ID in the environment");

// Configure the client with your Discord bot token in the environment.
load_config_value!("DISCORD_TOKEN", discord_token, token,);
load_config_value!("SPOTIFY_USERNAME", spotify_username, username,);
load_config_value!("SPOTIFY_PASSWORD", spotify_password, password,);
load_config_value!("DISCORD_USER_ID", discord_user_id, user_id,);

let mut cache_dir = None;

Expand All @@ -302,3 +314,24 @@ async fn main() {
.await
.map_err(|why| println!("Client ended: {:?}", why));
}

// I hate macros, this should be illegal
// idk how to combine those three parameters
#[macro_export]
macro_rules! load_config_value {
oleggtro marked this conversation as resolved.
Show resolved Hide resolved
(
$env_name:expr,
$field_name:ident,
$var_name:ident,
) => {
let $var_name = match env::var($env_name) {
Ok(val) => val,
Err(_) => {
let config_string = read_to_string("config.toml").expect("couldn't load config from config.toml");
let config: Config =
toml::from_str(&config_string).expect("couldn't deserialize config");
config.$field_name
},
};
};
}