Skip to content

Commit 948e8e3

Browse files
committed
It now uses config-rs for configuration management instead of hard coded values
1 parent 6925f9e commit 948e8e3

File tree

6 files changed

+101
-52
lines changed

6 files changed

+101
-52
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ dotenv = "0.15.0"
1717
serenity = { version = "0.12.4", features = ["chrono"] }
1818
poise = "0.6.1"
1919
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
20+
config = "0.13"
21+
lazy_static = "1.4"

config.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[discord]
2+
roles_message_id = 1298636092886749294
3+
4+
[roles]
5+
archive = 1208457364274028574
6+
mobile = 1298553701094395936
7+
systems = 1298553801191718944
8+
ai = 1298553753523453952
9+
research = 1298553855474270219
10+
devops = 1298553883169132554
11+
web = 1298553910167994428
12+
13+
[channels]
14+
group_one = 1225098248293716008
15+
group_two = 1225098298935738489
16+
group_three = 1225098353378070710
17+
group_four = 1225098407216156712
18+
status_update = 764575524127244318
19+
20+
[status_update]
21+
title_url = "https://www.youtube.com/watch?v=epnuvyNj0FM"
22+
image_url = "https://media1.tenor.com/m/zAHCPvoyjNIAAAAd/yay-kitty.gif"
23+
author_url = "https://github.com/amfoss/amd"
24+
icon_url = "https://cdn.discordapp.com/avatars/1245352445736128696/da3c6f833b688f5afa875c9df5d86f91.webp?size=160"

src/config.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use config::{Config, File};
2+
use serde::Deserialize;
3+
use lazy_static::lazy_static;
4+
use std::sync::Arc;
5+
6+
#[derive(Debug, Deserialize)]
7+
pub struct Discord {
8+
pub roles_message_id: u64,
9+
}
10+
11+
#[derive(Debug, Deserialize)]
12+
pub struct Roles {
13+
pub archive: u64,
14+
pub mobile: u64,
15+
pub systems: u64,
16+
pub ai: u64,
17+
pub research: u64,
18+
pub devops: u64,
19+
pub web: u64,
20+
}
21+
22+
#[derive(Debug, Deserialize)]
23+
pub struct Channels {
24+
pub group_one: u64,
25+
pub group_two: u64,
26+
pub group_three: u64,
27+
pub group_four: u64,
28+
pub status_update: u64,
29+
}
30+
31+
#[derive(Debug, Deserialize)]
32+
pub struct StatusUpdate {
33+
pub title_url: String,
34+
pub image_url: String,
35+
pub author_url: String,
36+
pub icon_url: String,
37+
}
38+
39+
#[derive(Debug, Deserialize)]
40+
pub struct Settings {
41+
pub discord: Discord,
42+
pub roles: Roles,
43+
pub channels: Channels,
44+
pub status_update: StatusUpdate,
45+
}
46+
47+
lazy_static! {
48+
pub static ref CONFIG: Arc<Settings> = {
49+
let config = Config::builder()
50+
.add_source(File::with_name("config.toml"))
51+
.build()
52+
.expect("Failed to load configuration")
53+
.try_deserialize()
54+
.expect("Invalid configuration format");
55+
56+
Arc::new(config)
57+
};
58+
}

src/ids.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ mod tasks;
2828
/// Misc. helper functions that don't really have a place anywhere else.
2929
mod utils;
3030

31+
mod config;
32+
33+
use config::CONFIG;
34+
3135
use anyhow::Context as _;
3236
use poise::{Context as PoiseContext, Framework, FrameworkOptions, PrefixFrameworkOptions};
3337
use serenity::{

src/tasks/status_update.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ use crate::{
3535
models::Member,
3636
queries::{fetch_members, increment_streak, reset_streak},
3737
},
38-
ids::{
39-
GROUP_FOUR_CHANNEL_ID, GROUP_ONE_CHANNEL_ID, GROUP_THREE_CHANNEL_ID, GROUP_TWO_CHANNEL_ID,
40-
STATUS_UPDATE_CHANNEL_ID,
41-
},
4238
};
4339

44-
const TITLE_URL: &str = "https://www.youtube.com/watch?v=epnuvyNj0FM";
45-
const IMAGE_URL: &str = "https://media1.tenor.com/m/zAHCPvoyjNIAAAAd/yay-kitty.gif";
46-
const AUTHOR_URL: &str = "https://github.com/amfoss/amd";
47-
const ICON_URL: &str = "https://cdn.discordapp.com/avatars/1245352445736128696/da3c6f833b688f5afa875c9df5d86f91.webp?size=160";
40+
const TITLE_URL: &str = &CONFIG.status_update.title_url;
41+
const IMAGE_URL: &str = &CONFIG.status_update.image_url;
42+
const AUTHOR_URL: &str = &CONFIG.status_update.author_url;
43+
const ICON_URL: &str = &CONFIG.status_update.icon_url;
4844

4945
/// Checks for status updates daily at 9 AM.
5046
pub struct StatusUpdateCheck;
@@ -95,15 +91,15 @@ pub async fn check_status_updates(ctx: Context) -> anyhow::Result<()> {
9591

9692
// TOOD: Get IDs through ENV instead
9793
fn get_channel_ids() -> anyhow::Result<Vec<ChannelId>> {
98-
trace!("Getting channel ids...");
9994
Ok(vec![
100-
ChannelId::new(GROUP_ONE_CHANNEL_ID),
101-
ChannelId::new(GROUP_TWO_CHANNEL_ID),
102-
ChannelId::new(GROUP_THREE_CHANNEL_ID),
103-
ChannelId::new(GROUP_FOUR_CHANNEL_ID),
95+
ChannelId::new(CONFIG.channels.group_one),
96+
ChannelId::new(CONFIG.channels.group_two),
97+
ChannelId::new(CONFIG.channels.group_three),
98+
ChannelId::new(CONFIG.channels.group_four),
10499
])
105100
}
106101

102+
107103
async fn send_and_save_limiting_messages(
108104
channel_ids: &Vec<ChannelId>,
109105
ctx: &Context,
@@ -295,18 +291,18 @@ async fn generate_embed(
295291

296292
let mut embed = CreateEmbed::default()
297293
.title(format!("Status Update Report - {}", today))
298-
.url(TITLE_URL)
294+
.url(&CONFIG.status_update.title_url)
299295
.description(description)
300296
.color(serenity::all::Colour::new(0xeab308))
301297
.timestamp(Timestamp::now())
302298
.author(
303299
CreateEmbedAuthor::new("amD")
304-
.url(AUTHOR_URL)
305-
.icon_url(ICON_URL),
300+
.url(&CONFIG.status_update.author_url)
301+
.icon_url(&CONFIG.status_update.icon_url),
306302
);
307303

308304
if naughty_list.is_empty() {
309-
embed = embed.image(IMAGE_URL);
305+
embed = embed.image(&CONFIG.status_update.image_url);
310306
}
311307

312308
Ok(embed)

0 commit comments

Comments
 (0)