From 6118231117e3e03185677e4163838e38a13dbdfa Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 19 Jun 2023 20:49:39 -0700 Subject: [PATCH] Use API for welcome modal --- settings.yaml | 15 ++++++++++ src/main.rs | 3 ++ src/models/mod.rs | 3 ++ src/models/settings.rs | 56 +++++++++++++++++++++++++++++++++++++ src/routes/get_challenge.rs | 9 ++++-- templates/modal.html.tera | 19 ++++++------- 6 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 settings.yaml create mode 100644 src/models/settings.rs diff --git a/settings.yaml b/settings.yaml new file mode 100644 index 0000000..b11607c --- /dev/null +++ b/settings.yaml @@ -0,0 +1,15 @@ +guilds: + # Tegaki Tuesday + - id: 814700630958276649 + channel: 819038172927098910 + hidden: true + # English-Japanese Language Exchange + - id: 189571157446492161 + channel: 352299136307036160 + recommended: true + # 日本語と英語 - JP & EN + - id: 116379774825267202 + channel: 346809059376234497 + # Yudai La Piñata + - id: 582147335476346880 + channel: 839852933377425488 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8e41c2d..82c02f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use sass_rocket_fairing::SassFairing; use std::env; mod models; +use models::Settings; mod utils; @@ -30,7 +31,9 @@ async fn rocket() -> _ { env::var("SECRET") .expect("Must have SECRET defined in .env for private cookie persistence"), )); + rocket::custom(config) + .manage(Settings::new(&http).await.unwrap()) .manage(http) .mount( "/", diff --git a/src/models/mod.rs b/src/models/mod.rs index 41f1ca0..02844c2 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -3,3 +3,6 @@ pub use user::User; mod challenge; pub use challenge::Challenge; + +mod settings; +pub use settings::Settings; diff --git a/src/models/settings.rs b/src/models/settings.rs new file mode 100644 index 0000000..3eb42c9 --- /dev/null +++ b/src/models/settings.rs @@ -0,0 +1,56 @@ +use derive_more::From; +use poise::serenity_prelude::Http; +use serde::{Deserialize, Serialize}; +use std::fs; + +#[derive(Serialize, Deserialize)] +pub struct Settings { + pub guilds: Vec, +} + +impl Settings { + pub async fn new(http: &Http) -> Result { + let file = fs::read_to_string("settings.yaml").unwrap(); + let mut settings: Self = serde_yaml::from_str(&file)?; + settings.load(http).await?; + Ok(settings) + } + + pub async fn load(&mut self, http: &Http) -> poise::serenity_prelude::Result<()> { + for guild in self.guilds.iter_mut() { + guild.load(http).await?; + } + Ok(()) + } +} + +#[derive(From, Debug)] +pub enum GetSettingsError { + Io(std::io::Error), + Deserialize(serde_yaml::Error), + Serenity(poise::serenity_prelude::Error), +} + +#[derive(Serialize, Deserialize)] +pub struct Guild { + pub id: u64, + #[serde(default)] + pub hidden: bool, + #[serde(default)] + pub recommended: bool, + pub channel: u64, + // None case means guild is not yet loaded + #[serde(default)] + pub icon: Option>, + #[serde(default)] + pub name: Option, +} + +impl Guild { + pub async fn load(&mut self, http: &Http) -> poise::serenity_prelude::Result<()> { + let server = http.get_guild(self.id).await?; + self.icon = Some(server.icon); + self.name = Some(server.name); + Ok(()) + } +} \ No newline at end of file diff --git a/src/routes/get_challenge.rs b/src/routes/get_challenge.rs index 4d10b69..4f1beb7 100644 --- a/src/routes/get_challenge.rs +++ b/src/routes/get_challenge.rs @@ -1,10 +1,12 @@ -use rocket::http::CookieJar; +use std::ops::Deref; + +use rocket::{http::CookieJar, State}; use rocket_dyn_templates::{context, Template}; -use crate::models::{Challenge, User}; +use crate::models::{Challenge, User, Settings}; #[get("/")] -pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template { +pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>, settings: &State) -> Template { println!( "{:?}", cookies @@ -15,6 +17,7 @@ pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template "index", context! { challenge, + settings: settings.deref(), user: User::get(cookies).await.unwrap(), content: Challenge::get(challenge), }, diff --git a/templates/modal.html.tera b/templates/modal.html.tera index e656215..2a66e78 100644 --- a/templates/modal.html.tera +++ b/templates/modal.html.tera @@ -3,21 +3,18 @@

In order to participate in challenges, you must be a member of a participating Discord server.

Join a participating server

- -
- Server icon -
日本語と英語 - JP & EN
-
-
- Server icon -
Yudai La Piñata
-
+ {% endfor %}