Skip to content

Commit 413cb6b

Browse files
committed
Build config-rs for configuration management
1 parent 3bc2812 commit 413cb6b

File tree

6 files changed

+167
-36
lines changed

6 files changed

+167
-36
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 & 36 deletions
This file was deleted.

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ mod scheduler;
2525
mod tasks;
2626
mod utils;
2727

28+
mod config;
29+
30+
use config::CONFIG;
31+
2832
use anyhow::Context as _;
2933
use poise::{Context as PoiseContext, Framework, FrameworkOptions, PrefixFrameworkOptions};
3034
use reaction_roles::{handle_reaction, populate_data_with_reaction_roles};

src/tasks/status_update.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,33 @@ use serenity::all::{
2424
use serenity::async_trait;
2525

2626
use super::Task;
27+
<<<<<<< HEAD
2728
use crate::graphql::models::{Member, StreakWithMemberId};
2829
use crate::graphql::queries::{fetch_members, fetch_streaks, increment_streak, reset_streak};
2930
use crate::ids::{
3031
GROUP_FOUR_CHANNEL_ID, GROUP_ONE_CHANNEL_ID, GROUP_THREE_CHANNEL_ID, GROUP_TWO_CHANNEL_ID,
3132
STATUS_UPDATE_CHANNEL_ID,
33+
=======
34+
use crate::utils::time::time_until;
35+
use crate::{
36+
graphql::{
37+
models::Member,
38+
queries::{fetch_members, increment_streak, reset_streak},
39+
},
40+
>>>>>>> 948e8e3 (It now uses config-rs for configuration management instead of hard coded values)
3241
};
3342
use crate::utils::time::time_until;
3443

44+
<<<<<<< HEAD
3545
/// Checks for status updates daily at 5 AM.
46+
=======
47+
const TITLE_URL: &str = &CONFIG.status_update.title_url;
48+
const IMAGE_URL: &str = &CONFIG.status_update.image_url;
49+
const AUTHOR_URL: &str = &CONFIG.status_update.author_url;
50+
const ICON_URL: &str = &CONFIG.status_update.icon_url;
51+
52+
/// Checks for status updates daily at 9 AM.
53+
>>>>>>> 948e8e3 (It now uses config-rs for configuration management instead of hard coded values)
3654
pub struct StatusUpdateCheck;
3755

3856
#[async_trait]
@@ -78,6 +96,7 @@ async fn status_update_check(ctx: Context) -> anyhow::Result<()> {
7896
Ok(())
7997
}
8098

99+
<<<<<<< HEAD
81100
async fn get_updates(ctx: &Context) -> anyhow::Result<Vec<Message>> {
82101
let channel_ids = get_channel_ids();
83102
let mut updates = Vec::new();
@@ -105,6 +124,36 @@ fn get_channel_ids() -> Vec<ChannelId> {
105124
fn is_valid_status_update(msg: &Message) -> bool {
106125
let report_config = get_report_config();
107126
let content = msg.content.to_lowercase();
127+
=======
128+
// TOOD: Get IDs through ENV instead
129+
fn get_channel_ids() -> anyhow::Result<Vec<ChannelId>> {
130+
Ok(vec![
131+
ChannelId::new(CONFIG.channels.group_one),
132+
ChannelId::new(CONFIG.channels.group_two),
133+
ChannelId::new(CONFIG.channels.group_three),
134+
ChannelId::new(CONFIG.channels.group_four),
135+
])
136+
}
137+
138+
139+
async fn send_and_save_limiting_messages(
140+
channel_ids: &Vec<ChannelId>,
141+
ctx: &Context,
142+
) -> anyhow::Result<()> {
143+
trace!("Running send_and_save_limiting_messages()");
144+
let mut msg_ids: Vec<MessageId> = vec![];
145+
for channel_id in channel_ids {
146+
debug!("Sending message in {}", channel_id);
147+
let msg = channel_id
148+
.say(
149+
&ctx.http,
150+
"Collecting messages for status update report. Please do not delete this message.",
151+
)
152+
.await
153+
.with_context(|| {
154+
anyhow::anyhow!("Failed to send limiting message in channel {}", channel_id)
155+
})?;
156+
>>>>>>> 948e8e3 (It now uses config-rs for configuration management instead of hard coded values)
108157

109158
let is_within_timeframe = DateTime::<Utc>::from_timestamp(msg.timestamp.timestamp(), 0)
110159
.expect("Valid timestamp")
@@ -214,10 +263,40 @@ async fn generate_embed(
214263
description.push_str(&format_defaulters(&naughty_list));
215264
}
216265

266+
<<<<<<< HEAD
217267
let embed = CreateEmbed::new()
218268
.title("Status Update Report")
219269
.description(description)
220270
.color(serenity::all::Colour::new(0xeab308));
271+
=======
272+
let description = build_description(
273+
highest_streak,
274+
all_time_high,
275+
&highest_streak_members,
276+
&all_time_high_members,
277+
&record_breakers,
278+
&naughty_list,
279+
);
280+
let today = chrono::Local::now()
281+
.with_timezone(&Asia::Kolkata)
282+
.date_naive();
283+
284+
let mut embed = CreateEmbed::default()
285+
.title(format!("Status Update Report - {}", today))
286+
.url(&CONFIG.status_update.title_url)
287+
.description(description)
288+
.color(serenity::all::Colour::new(0xeab308))
289+
.timestamp(Timestamp::now())
290+
.author(
291+
CreateEmbedAuthor::new("amD")
292+
.url(&CONFIG.status_update.author_url)
293+
.icon_url(&CONFIG.status_update.icon_url),
294+
);
295+
296+
if naughty_list.is_empty() {
297+
embed = embed.image(&CONFIG.status_update.image_url);
298+
}
299+
>>>>>>> 948e8e3 (It now uses config-rs for configuration management instead of hard coded values)
221300

222301
Ok(embed)
223302
}

0 commit comments

Comments
 (0)