Use API for welcome modal
This commit is contained in:
parent
839d6f80ab
commit
6118231117
6 changed files with 91 additions and 14 deletions
15
settings.yaml
Normal file
15
settings.yaml
Normal file
|
@ -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
|
|
@ -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(
|
||||
"/",
|
||||
|
|
|
@ -3,3 +3,6 @@ pub use user::User;
|
|||
|
||||
mod challenge;
|
||||
pub use challenge::Challenge;
|
||||
|
||||
mod settings;
|
||||
pub use settings::Settings;
|
||||
|
|
56
src/models/settings.rs
Normal file
56
src/models/settings.rs
Normal file
|
@ -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<Guild>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub async fn new(http: &Http) -> Result<Self, GetSettingsError> {
|
||||
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<Option<String>>,
|
||||
#[serde(default)]
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
|
@ -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("/<challenge>")]
|
||||
pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template {
|
||||
pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>, settings: &State<Settings>) -> 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),
|
||||
},
|
||||
|
|
|
@ -3,21 +3,18 @@
|
|||
<p>In order to participate in challenges, you must be a member of a participating Discord server.</p>
|
||||
<h2>Join a participating server</h2>
|
||||
<div class="servers">
|
||||
<div class="recommended">
|
||||
<img src="https://cdn.discordapp.com/icons/189571157446492161/a_5df28dc3a43bc6780cb084b2ad896a9f.webp?size=96" alt="Server icon">
|
||||
<div class="name">English-Japanese Language Exchange</div>
|
||||
{% for guild in settings.guilds %}
|
||||
{% if guild.hidden %}{% continue %}{% endif %}
|
||||
<div{% if guild.recommended %} class="recommended"{% endif %}>
|
||||
<img src="https://cdn.discordapp.com/icons/{{ guild.id }}/{{ guild.icon }}.webp?size=96" alt="Server icon">
|
||||
<div class="name">{{ guild.name }}</div>
|
||||
{% if guild.recommended %}
|
||||
<div class="label-wrapper">
|
||||
<div class="label">Recommended</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div>
|
||||
<img src="https://cdn.discordapp.com/icons/116379774825267202/a_425c9b6640fdc30deb5f97eddcf8e6a7.webp?size=96" alt="Server icon">
|
||||
<div class="name">日本語と英語 - JP & EN</div>
|
||||
</div>
|
||||
<div>
|
||||
<img src="https://cdn.discordapp.com/icons/582147335476346880/677049f139f08cf670e20e8c5f0a656e.webp?size=96" alt="Server icon">
|
||||
<div class="name">Yudai La Piñata</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</dialog>
|
||||
<script>
|
||||
|
|
Loading…
Add table
Reference in a new issue