Skip to content

Build config-rs for configuration management #51

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dotenv = "0.15.0"
serenity = { version = "0.12.4", features = ["chrono"] }
poise = "0.6.1"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
config = "0.13"
lazy_static = "1.4"
24 changes: 24 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[discord]
roles_message_id = 1298636092886749294

[roles]
archive = 1208457364274028574
mobile = 1298553701094395936
systems = 1298553801191718944
ai = 1298553753523453952
research = 1298553855474270219
devops = 1298553883169132554
web = 1298553910167994428

[channels]
group_one = 1225098248293716008
group_two = 1225098298935738489
group_three = 1225098353378070710
group_four = 1225098407216156712
status_update = 764575524127244318

[status_update]
title_url = "https://www.youtube.com/watch?v=epnuvyNj0FM"
image_url = "https://media1.tenor.com/m/zAHCPvoyjNIAAAAd/yay-kitty.gif"
author_url = "https://github.com/amfoss/amd"
icon_url = "https://cdn.discordapp.com/avatars/1245352445736128696/da3c6f833b688f5afa875c9df5d86f91.webp?size=160"
58 changes: 58 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use config::{Config, File};
use serde::Deserialize;
use lazy_static::lazy_static;
use std::sync::Arc;

#[derive(Debug, Deserialize)]
pub struct Discord {
pub roles_message_id: u64,
}

#[derive(Debug, Deserialize)]
pub struct Roles {
pub archive: u64,
pub mobile: u64,
pub systems: u64,
pub ai: u64,
pub research: u64,
pub devops: u64,
pub web: u64,
}

#[derive(Debug, Deserialize)]
pub struct Channels {
pub group_one: u64,
pub group_two: u64,
pub group_three: u64,
pub group_four: u64,
pub status_update: u64,
}

#[derive(Debug, Deserialize)]
pub struct StatusUpdate {
pub title_url: String,
pub image_url: String,
pub author_url: String,
pub icon_url: String,
}

#[derive(Debug, Deserialize)]
pub struct Settings {
pub discord: Discord,
pub roles: Roles,
pub channels: Channels,
pub status_update: StatusUpdate,
}

lazy_static! {
pub static ref CONFIG: Arc<Settings> = {
let config = Config::builder()
.add_source(File::with_name("config.toml"))
.build()
.expect("Failed to load configuration")
.try_deserialize()
.expect("Invalid configuration format");

Arc::new(config)
};
}
1 change: 0 additions & 1 deletion src/graphql/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub struct Member {

#[derive(Debug, Deserialize, Clone)]
pub struct AttendanceRecord {
#[serde(rename = "memberId")]
pub name: String,
pub year: i32,
#[serde(rename = "isPresent")]
Expand Down
36 changes: 0 additions & 36 deletions src/ids.rs

This file was deleted.

4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ mod scheduler;
mod tasks;
mod utils;

mod config;

use config::CONFIG;

use anyhow::Context as _;
use poise::{Context as PoiseContext, Framework, FrameworkOptions, PrefixFrameworkOptions};
use reaction_roles::{handle_reaction, populate_data_with_reaction_roles};
Expand Down
59 changes: 42 additions & 17 deletions src/tasks/status_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ use serenity::async_trait;
use super::Task;
use crate::graphql::models::{Member, StreakWithMemberId};
use crate::graphql::queries::{fetch_members, fetch_streaks, increment_streak, reset_streak};
use crate::ids::{
GROUP_FOUR_CHANNEL_ID, GROUP_ONE_CHANNEL_ID, GROUP_THREE_CHANNEL_ID, GROUP_TWO_CHANNEL_ID,
STATUS_UPDATE_CHANNEL_ID,
};
use crate::utils::time::time_until;

/// Checks for status updates daily at 5 AM.
const TITLE_URL: &str = &CONFIG.status_update.title_url;
const IMAGE_URL: &str = &CONFIG.status_update.image_url;
const AUTHOR_URL: &str = &CONFIG.status_update.author_url;
const ICON_URL: &str = &CONFIG.status_update.icon_url;

/// Checks for status updates daily at 9 AM.
pub struct StatusUpdateCheck;

#[async_trait]
Expand Down Expand Up @@ -92,14 +94,14 @@ async fn get_updates(ctx: &Context) -> anyhow::Result<Vec<Message>> {
Ok(updates)
}

// TODO: Replace hardcoded set with configurable list
fn get_channel_ids() -> Vec<ChannelId> {
vec![
ChannelId::new(GROUP_ONE_CHANNEL_ID),
ChannelId::new(GROUP_TWO_CHANNEL_ID),
ChannelId::new(GROUP_THREE_CHANNEL_ID),
ChannelId::new(GROUP_FOUR_CHANNEL_ID),
]
// TOOD: Get IDs through ENV instead
fn get_channel_ids() -> anyhow::Result<Vec<ChannelId>> {
Ok(vec![
ChannelId::new(CONFIG.channels.group_one),
ChannelId::new(CONFIG.channels.group_two),
ChannelId::new(CONFIG.channels.group_three),
ChannelId::new(CONFIG.channels.group_four),
])
}

fn is_valid_status_update(msg: &Message) -> bool {
Expand Down Expand Up @@ -214,10 +216,33 @@ async fn generate_embed(
description.push_str(&format_defaulters(&naughty_list));
}

let embed = CreateEmbed::new()
.title("Status Update Report")
let description = build_description(
highest_streak,
all_time_high,
&highest_streak_members,
&all_time_high_members,
&record_breakers,
&naughty_list,
);
let today = chrono::Local::now()
.with_timezone(&Asia::Kolkata)
.date_naive();

let mut embed = CreateEmbed::default()
.title(format!("Status Update Report - {}", today))
.url(TITLE_URL)
.description(description)
.color(serenity::all::Colour::new(0xeab308));
.color(serenity::all::Colour::new(0xeab308))
.timestamp(Timestamp::now())
.author(
CreateEmbedAuthor::new("amD")
.url(AUTHOR_URL)
.icon_url(ICON_URL),
);

if naughty_list.is_empty() {
embed = embed.image(IMAGE_URL);
}

Ok(embed)
}
Expand All @@ -232,7 +257,7 @@ fn format_members(members: &[Member]) -> String {

format!("{}\n", list)
} else {
String::from("More than five members hold this record!")
String::from("More than five members hold this record!\n")
}
}

Expand All @@ -242,7 +267,7 @@ fn format_defaulters(naughty_list: &GroupedMember) -> String {
description.push_str(&format!("## Group {}\n", group));
for member in missed_members {
let status = match member.streak[0].current_streak {
0 => ":x",
0 => ":x:",
-1 => ":x::x:",
_ => ":headstone:",
};
Expand Down