feat: pull the config for the festivals locally, using teh imported repo

This commit is contained in:
silver 2025-06-16 00:21:49 +01:00
parent 6d5ad8e418
commit 86a3af2a65
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
3 changed files with 74 additions and 60 deletions

View file

@ -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}
file = "_festivals.toml"

View file

@ -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);

View file

@ -1,5 +1,9 @@
use serde::Deserialize;
use std::{ffi::OsString, path::PathBuf};
use std::{ffi::OsString, fs, path::PathBuf};
pub mod get_config_icons {
use super::*;
use crate::Config;
#[derive(Deserialize)]
pub struct ConfigToml {
@ -7,10 +11,20 @@ pub struct ConfigToml {
pub festivals: Vec<ConfigTomlFestivals>,
}
#[derive(Deserialize)]
pub struct ConfigTomlLocal {
pub source: ConfigTomlSource,
}
#[derive(Deserialize)]
pub struct ConfigTomlRemote {
pub festivals: Vec<ConfigTomlFestivals>,
}
#[derive(Deserialize, Debug)]
pub struct ConfigTomlSource {
pub repo: String,
pub directory: String,
pub file: String,
}
#[derive(Deserialize, Debug)]
@ -27,11 +41,25 @@ pub struct ConfigTomlFestivalsTime {
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
}
pub fn get_config_icons() -> ConfigToml {
let toml_raw = include_str!("../../.server-icons.toml");
let config: ConfigToml = toml::from_str(toml_raw).unwrap();
config
// 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<Sqlite>, config_global: &Config, config_toml: &ConfigToml) {
pub async fn update_icon_main(ctx: &Context, db: &Pool<Sqlite>, 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);