diff --git a/.server-icons.toml b/.server-icons.toml index 587f63b..eeb302c 100644 --- a/.server-icons.toml +++ b/.server-icons.toml @@ -3,21 +3,4 @@ [source] repo = "https://forgejo.skynet.ie/Computer_Society/open-goverance" directory = "Resources/Logo_Variants" - -[[festivals]] -name = "pride" -all_year = true -start = { day = 1, month = 6, year = 0} -end = { day = 30, month = 6, year = 0} - -[[festivals]] -name = "christmas" -all_year = false -start = { day = 1, month = 12, year = 0} -end = { day = 31, month = 12, year = 0} - -[[festivals]] -name = "halloween" -all_year = false -start = { day = 1, month = 12, year = 0} -end = { day = 31, month = 12, year = 0} \ No newline at end of file +file = "_festivals.toml" \ No newline at end of file diff --git a/src/bin/update_server-icon.rs b/src/bin/update_server-icon.rs index 4d49490..ff4cef9 100644 --- a/src/bin/update_server-icon.rs +++ b/src/bin/update_server-icon.rs @@ -4,15 +4,12 @@ use serenity::{ model::gateway::{GatewayIntents, Ready}, Client, }; -use skynet_discord_bot::common::server_icon; +use skynet_discord_bot::common::server_icon::{get_config_icons, update_icon}; use skynet_discord_bot::{ common::database::{db_init, DataBase}, get_config, Config, }; -use std::{ - process::self, - sync::Arc, -}; +use std::{process, sync::Arc}; use tokio::sync::RwLock; #[tokio::main] @@ -62,9 +59,9 @@ impl EventHandler for Handler { }; let config_global = config_lock.read().await; - let config_toml = server_icon::get_config_icons(); + let config_toml = get_config_icons::minimal(); - server_icon::update_icon::update_icon_main(&ctx, &db, &config_global, &config_toml).await; + update_icon::update_icon_main(&ctx, &db, &config_global, &config_toml).await; // finish up process::exit(0); diff --git a/src/common/server_icon.rs b/src/common/server_icon.rs index 4a489be..3f88d34 100644 --- a/src/common/server_icon.rs +++ b/src/common/server_icon.rs @@ -1,37 +1,65 @@ use serde::Deserialize; -use std::{ffi::OsString, path::PathBuf}; +use std::{ffi::OsString, fs, path::PathBuf}; -#[derive(Deserialize)] -pub struct ConfigToml { - pub source: ConfigTomlSource, - pub festivals: Vec, -} +pub mod get_config_icons { + use super::*; + use crate::Config; -#[derive(Deserialize, Debug)] -pub struct ConfigTomlSource { - pub repo: String, - pub directory: String, -} + #[derive(Deserialize)] + pub struct ConfigToml { + pub source: ConfigTomlSource, + pub festivals: Vec, + } -#[derive(Deserialize, Debug)] -pub struct ConfigTomlFestivals { - pub name: String, - pub all_year: bool, - pub start: ConfigTomlFestivalsTime, - pub end: ConfigTomlFestivalsTime, -} + #[derive(Deserialize)] + pub struct ConfigTomlLocal { + pub source: ConfigTomlSource, + } + #[derive(Deserialize)] + pub struct ConfigTomlRemote { + pub festivals: Vec, + } -#[derive(Deserialize, Debug)] -pub struct ConfigTomlFestivalsTime { - pub day: u32, - pub month: u32, - pub year: i32, -} + #[derive(Deserialize, Debug)] + pub struct ConfigTomlSource { + pub repo: String, + pub directory: String, + pub file: String, + } -pub fn get_config_icons() -> ConfigToml { - let toml_raw = include_str!("../../.server-icons.toml"); - let config: ConfigToml = toml::from_str(toml_raw).unwrap(); - config + #[derive(Deserialize, Debug)] + pub struct ConfigTomlFestivals { + pub name: String, + pub all_year: bool, + pub start: ConfigTomlFestivalsTime, + pub end: ConfigTomlFestivalsTime, + } + + #[derive(Deserialize, Debug)] + pub struct ConfigTomlFestivalsTime { + pub day: u32, + pub month: u32, + pub year: i32, + } + pub fn minimal() -> ConfigTomlLocal { + let toml_raw_min = include_str!("../../.server-icons.toml"); + let config_min: ConfigTomlLocal = toml::from_str(toml_raw_min).unwrap(); + config_min + } + + // since a copy of the festival file is in the repo we just need to get to it + pub fn full(config: &Config) -> ConfigToml { + let config_source = minimal(); + + let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file); + let contents = fs::read_to_string(file_path).expect("Should have been able to read the file"); + let config_festivals: ConfigTomlRemote = toml::from_str(&contents).unwrap(); + + ConfigToml { + source: config_source.source, + festivals: config_festivals.festivals, + } + } } #[derive(Debug)] @@ -50,7 +78,10 @@ pub struct ServerIcons { pub mod update_icon { use super::*; use crate::{ - common::renderer::{Args, Renderer}, + common::{ + renderer::{Args, Renderer}, + server_icon::get_config_icons::{self, ConfigToml, ConfigTomlLocal}, + }, get_now_iso, Config, }; use chrono::{Datelike, Utc}; @@ -61,20 +92,23 @@ pub mod update_icon { client::Context, }; use sqlx::{Pool, Sqlite}; - use std::{fs, process::Command}; + use std::process::Command; /// Update the server icon, pulling from open governance. - pub async fn update_icon_main(ctx: &Context, db: &Pool, config_global: &Config, config_toml: &ConfigToml) { + pub async fn update_icon_main(ctx: &Context, db: &Pool, config_global: &Config, config_toml_local: &ConfigTomlLocal) { let server = GuildId::new(689189992417067052); // clone repo into local folder - clone_repo(config_global, config_toml); + clone_repo(config_global, config_toml_local); + + // now the repo has been downloaded/updated we can now access the festivals + let config_toml = get_config_icons::full(config_global); // see if there is a current festival - let festival_data = get_festival(config_toml); + let festival_data = get_festival(&config_toml); // get a list of all the graphics files - let logos = get_logos(config_global, config_toml); + let logos = get_logos(config_global, &config_toml); // filter them so only the current season (if any) are active let logos_filtered = logos_filter(&festival_data, logos); @@ -115,7 +149,7 @@ pub mod update_icon { result } - fn clone_repo(config: &Config, config_toml: &ConfigToml) { + fn clone_repo(config: &Config, config_toml: &ConfigTomlLocal) { let url = &config_toml.source.repo; let folder = format!("{}/open-governance", &config.home);