2025-06-14 18:00:40 +01:00
|
|
|
use chrono::{Datelike, Utc};
|
|
|
|
use rand::{rngs::SmallRng, seq::IndexedRandom, SeedableRng};
|
|
|
|
use serde::Deserialize;
|
2025-06-07 00:10:52 +01:00
|
|
|
use serenity::{
|
2025-06-14 18:00:40 +01:00
|
|
|
all::GuildId,
|
2025-06-07 00:10:52 +01:00
|
|
|
async_trait,
|
2025-06-14 18:00:40 +01:00
|
|
|
builder::{CreateAttachment, EditGuild},
|
2025-06-07 00:10:52 +01:00
|
|
|
client::{Context, EventHandler},
|
|
|
|
model::gateway::{GatewayIntents, Ready},
|
|
|
|
Client,
|
|
|
|
};
|
2025-06-14 18:00:40 +01:00
|
|
|
use skynet_discord_bot::{
|
|
|
|
common::{
|
|
|
|
database::{db_init, DataBase},
|
|
|
|
renderer::{Args, Renderer},
|
|
|
|
},
|
|
|
|
get_config, get_now_iso, Config,
|
|
|
|
};
|
2025-06-14 16:46:04 +01:00
|
|
|
use sqlx::{Pool, Sqlite};
|
2025-06-14 18:00:40 +01:00
|
|
|
use std::{
|
|
|
|
ffi::OsString,
|
|
|
|
fs,
|
|
|
|
path::PathBuf,
|
|
|
|
process::{self, Command},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2025-06-07 00:10:52 +01:00
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let config = get_config();
|
|
|
|
let db = match db_init(&config).await {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
|
|
|
|
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
|
|
|
|
// Build our client.
|
|
|
|
let mut client = Client::builder(&config.discord_token, intents)
|
|
|
|
.event_handler(Handler {})
|
|
|
|
.await
|
|
|
|
.expect("Error creating client");
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut data = client.data.write().await;
|
|
|
|
|
|
|
|
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
|
|
|
data.insert::<DataBase>(Arc::new(RwLock::new(db)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(why) = client.start().await {
|
|
|
|
println!("Client error: {:?}", why);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Handler;
|
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
|
|
|
let ctx = Arc::new(ctx);
|
|
|
|
println!("{} is connected!", ready.user.name);
|
|
|
|
|
|
|
|
// pull in the open governance repo
|
|
|
|
// u[date committee server
|
|
|
|
update_icon_main(Arc::clone(&ctx)).await;
|
|
|
|
|
|
|
|
// finish up
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_icon_main(ctx: Arc<Context>) {
|
|
|
|
let db_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let db = db_lock.read().await;
|
|
|
|
|
|
|
|
let config_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 00:10:52 +01:00
|
|
|
let config_global = config_lock.read().await;
|
|
|
|
let config_toml = get_config_icons();
|
|
|
|
let server = GuildId::new(689189992417067052);
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 00:10:52 +01:00
|
|
|
// clone repo into local folder
|
|
|
|
clone_repo(&config_global, &config_toml);
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 00:10:52 +01:00
|
|
|
// see if there is a current festival
|
|
|
|
let festival_data = get_festival(&config_toml);
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 00:10:52 +01:00
|
|
|
// get a list of all the graphics files
|
2025-06-07 19:55:36 +01:00
|
|
|
let logos = get_logos(&config_global, &config_toml);
|
2025-06-07 23:04:57 +01:00
|
|
|
|
|
|
|
// filter them so only the current season (if any) are active
|
|
|
|
let logos_filtered = logos_filter(&festival_data, logos);
|
2025-06-07 23:53:24 +01:00
|
|
|
|
2025-06-08 00:18:30 +01:00
|
|
|
let mut rng = SmallRng::from_os_rng();
|
2025-06-07 23:53:24 +01:00
|
|
|
let logo_selected = logos_filtered.choose(&mut rng).unwrap();
|
2025-06-08 00:18:30 +01:00
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
logo_set(&ctx, &db, &server, logo_selected).await;
|
2025-06-07 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct FestivalData {
|
2025-06-10 16:54:13 +01:00
|
|
|
current: Vec<String>,
|
2025-06-07 21:24:59 +01:00
|
|
|
exclusions: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
fn get_festival(config_toml: &ConfigToml) -> FestivalData {
|
2025-06-07 00:10:52 +01:00
|
|
|
let today = Utc::now();
|
|
|
|
let day = today.day();
|
|
|
|
let month = today.month();
|
|
|
|
let year = today.year();
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 21:24:59 +01:00
|
|
|
let mut result = FestivalData {
|
2025-06-10 16:54:13 +01:00
|
|
|
current: vec![],
|
2025-06-07 21:24:59 +01:00
|
|
|
exclusions: vec![],
|
|
|
|
};
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 00:10:52 +01:00
|
|
|
for festival in &config_toml.festivals {
|
2025-06-14 18:00:40 +01:00
|
|
|
if (day >= festival.start.day && day <= festival.end.day) && (month >= festival.start.month && month <= festival.end.month) {
|
|
|
|
if festival.start.year == 0 || festival.end.year == 0 || (year >= festival.start.year && year <= festival.end.year) {
|
2025-06-10 16:54:13 +01:00
|
|
|
result.current.push(festival.name.to_owned());
|
2025-06-07 22:29:00 +01:00
|
|
|
}
|
2025-06-07 00:10:52 +01:00
|
|
|
} else if !festival.all_year {
|
2025-06-07 21:24:59 +01:00
|
|
|
result.exclusions.push(festival.name.to_owned());
|
2025-06-07 00:10:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-07 21:24:59 +01:00
|
|
|
result
|
2025-06-07 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ConfigToml {
|
|
|
|
source: ConfigTomlSource,
|
|
|
|
festivals: Vec<ConfigTomlFestivals>,
|
|
|
|
}
|
2025-06-14 18:00:40 +01:00
|
|
|
#[derive(Deserialize, Debug)]
|
2025-06-07 00:10:52 +01:00
|
|
|
struct ConfigTomlSource {
|
|
|
|
repo: String,
|
|
|
|
directory: String,
|
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
#[derive(Deserialize, Debug)]
|
2025-06-07 00:10:52 +01:00
|
|
|
struct ConfigTomlFestivals {
|
|
|
|
name: String,
|
|
|
|
all_year: bool,
|
|
|
|
start: ConfigTomlFestivalsTime,
|
|
|
|
end: ConfigTomlFestivalsTime,
|
|
|
|
}
|
2025-06-14 18:00:40 +01:00
|
|
|
#[derive(Deserialize, Debug)]
|
2025-06-07 00:10:52 +01:00
|
|
|
struct ConfigTomlFestivalsTime {
|
|
|
|
day: u32,
|
|
|
|
month: u32,
|
2025-06-14 18:00:40 +01:00
|
|
|
year: i32,
|
2025-06-07 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_config_icons() -> ConfigToml {
|
|
|
|
let toml_raw = include_str!("../../.server-icons.toml");
|
|
|
|
let config: ConfigToml = toml::from_str(toml_raw).unwrap();
|
|
|
|
config
|
|
|
|
}
|
2025-06-14 18:00:40 +01:00
|
|
|
fn clone_repo(config: &Config, config_toml: &ConfigToml) {
|
2025-06-07 00:10:52 +01:00
|
|
|
let url = &config_toml.source.repo;
|
|
|
|
let folder = format!("{}/open-governance", &config.home);
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
Command::new("git").arg("clone").arg(url).arg(&folder).output().expect("failed to execute process");
|
2025-06-07 00:10:52 +01:00
|
|
|
|
|
|
|
Command::new("git")
|
2025-06-14 18:00:40 +01:00
|
|
|
.arg("pull")
|
|
|
|
.arg("origin")
|
|
|
|
.arg("main")
|
|
|
|
.current_dir(&folder)
|
|
|
|
.output()
|
|
|
|
.expect("failed to execute process");
|
2025-06-07 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
#[derive(Debug)]
|
2025-06-07 21:24:59 +01:00
|
|
|
struct LogoData {
|
|
|
|
name: OsString,
|
|
|
|
path: PathBuf,
|
|
|
|
}
|
2025-06-14 18:00:40 +01:00
|
|
|
fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
2025-06-07 00:10:52 +01:00
|
|
|
let folder = format!("{}/open-governance/{}", &config.home, &config_toml.source.directory);
|
2025-06-07 01:05:08 +01:00
|
|
|
let folder_path = PathBuf::from(&folder);
|
2025-06-07 22:44:47 +01:00
|
|
|
let mut folder_output = folder_path.clone();
|
|
|
|
folder_output.push("converted");
|
2025-06-07 00:10:52 +01:00
|
|
|
let paths = fs::read_dir(folder).unwrap();
|
2025-06-07 01:05:08 +01:00
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
let args = Args {
|
2025-06-07 01:05:08 +01:00
|
|
|
input: folder_path.clone(),
|
2025-06-07 22:44:47 +01:00
|
|
|
output: folder_output,
|
2025-06-07 01:05:08 +01:00
|
|
|
colors: String::from(""),
|
|
|
|
width: 1024,
|
|
|
|
height: 1024,
|
|
|
|
};
|
|
|
|
let mut r = Renderer::new(&args).unwrap();
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 19:55:36 +01:00
|
|
|
let mut logos = vec![];
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 19:55:36 +01:00
|
|
|
for tmp in paths.flatten() {
|
|
|
|
let path_local = tmp.path().to_owned();
|
|
|
|
let path_local2 = tmp.path().to_owned();
|
2025-06-07 21:24:59 +01:00
|
|
|
let name = path_local2.file_name().unwrap().to_owned();
|
|
|
|
let mut path = tmp.path();
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 22:44:47 +01:00
|
|
|
if path.is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
2025-06-07 19:55:36 +01:00
|
|
|
|
2025-06-07 01:05:08 +01:00
|
|
|
match tmp.path().extension() {
|
|
|
|
None => {}
|
|
|
|
Some(ext) => {
|
|
|
|
if ext == "svg" {
|
|
|
|
let mut path_new = path_local.clone();
|
|
|
|
path_new.set_extension("png");
|
2025-06-07 22:44:47 +01:00
|
|
|
let filename_tmp = path_new.clone();
|
|
|
|
let filename = filename_tmp.file_name().unwrap_or_default();
|
|
|
|
path_new.pop();
|
|
|
|
path_new.push("converted");
|
|
|
|
path_new.push(filename);
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 01:05:08 +01:00
|
|
|
// check if exists
|
2025-06-07 23:02:16 +01:00
|
|
|
if !path_new.exists() {
|
|
|
|
// convert if it hasnt been converted already
|
|
|
|
match r.render(&path_local, &args) {
|
2025-06-14 18:00:40 +01:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(_e) => {
|
|
|
|
dbg!("Failed to render {path_local:?}: {}");
|
2025-06-07 23:02:16 +01:00
|
|
|
}
|
2025-06-07 01:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
2025-06-07 21:24:59 +01:00
|
|
|
path = path_new;
|
2025-06-07 01:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
logos.push(LogoData {
|
2025-06-07 21:24:59 +01:00
|
|
|
name,
|
|
|
|
path,
|
|
|
|
});
|
2025-06-07 19:55:36 +01:00
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
// println!("Name: {}", &tmp.path().display());
|
2025-06-07 00:10:52 +01:00
|
|
|
}
|
2025-06-14 18:00:40 +01:00
|
|
|
|
2025-06-07 19:55:36 +01:00
|
|
|
logos
|
2025-06-07 23:04:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<LogoData> {
|
2025-06-07 23:04:57 +01:00
|
|
|
let mut filtered: Vec<LogoData> = vec![];
|
|
|
|
|
2025-06-10 16:54:13 +01:00
|
|
|
'outer: for logo in existing {
|
2025-06-07 23:04:57 +01:00
|
|
|
let name_lowercase0 = logo.name.to_ascii_lowercase();
|
|
|
|
let name_lowercase = name_lowercase0.to_str().unwrap_or_default();
|
|
|
|
|
2025-06-14 15:54:07 +01:00
|
|
|
if !festival_data.current.is_empty() {
|
|
|
|
// if its a current festival filter based on it
|
|
|
|
for festival in &festival_data.current {
|
|
|
|
if name_lowercase.contains(festival) {
|
|
|
|
filtered.push(logo);
|
|
|
|
continue 'outer;
|
|
|
|
}
|
2025-06-07 23:04:57 +01:00
|
|
|
}
|
2025-06-14 15:54:07 +01:00
|
|
|
} else {
|
|
|
|
// else filter using the excluded ones
|
|
|
|
let mut excluded = false;
|
|
|
|
for festival in &festival_data.exclusions {
|
|
|
|
if name_lowercase.contains(festival) {
|
|
|
|
excluded = true;
|
|
|
|
}
|
2025-06-07 23:04:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-14 15:54:07 +01:00
|
|
|
if !excluded {
|
|
|
|
filtered.push(logo);
|
|
|
|
}
|
2025-06-07 23:04:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered
|
2025-06-08 00:18:30 +01:00
|
|
|
}
|
|
|
|
|
2025-06-14 16:46:04 +01:00
|
|
|
#[derive(Debug, Clone, sqlx::FromRow)]
|
|
|
|
pub struct ServerIcons {
|
|
|
|
pub id: i64,
|
|
|
|
pub name: String,
|
|
|
|
pub date: String,
|
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
async fn logo_set(ctx: &Arc<Context>, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) {
|
2025-06-14 16:46:04 +01:00
|
|
|
// add to teh database
|
2025-06-14 18:00:40 +01:00
|
|
|
logo_set_db(db, logo_selected).await;
|
2025-06-14 16:46:04 +01:00
|
|
|
|
2025-06-08 00:18:30 +01:00
|
|
|
let icon = CreateAttachment::path(logo_selected.path.to_str().unwrap_or_default()).await.unwrap();
|
|
|
|
|
|
|
|
// assuming a `guild` has already been bound
|
|
|
|
let builder = EditGuild::new().icon(Some(&icon));
|
|
|
|
server.edit(ctx, builder).await.unwrap();
|
2025-06-14 16:46:04 +01:00
|
|
|
}
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) {
|
2025-06-14 16:46:04 +01:00
|
|
|
let name = logo_selected.name.to_str().unwrap_or_default();
|
|
|
|
|
2025-06-14 18:00:40 +01:00
|
|
|
match sqlx::query_as::<_, ServerIcons>(
|
2025-06-14 16:46:04 +01:00
|
|
|
"
|
|
|
|
INSERT OR REPLACE INTO server_icons (name, date)
|
2025-06-14 18:00:40 +01:00
|
|
|
VALUES (?1, ?2)
|
2025-06-14 16:46:04 +01:00
|
|
|
",
|
|
|
|
)
|
2025-06-14 18:00:40 +01:00
|
|
|
.bind(name)
|
|
|
|
.bind(get_now_iso(false))
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
dbg!(e);
|
|
|
|
}
|
|
|
|
}
|
2025-06-14 16:46:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fn command(config_toml: &ConfigToml, logo_selected: &LogoData){
|
|
|
|
// let web_url = format!("{}/src/branch/main/{}/{}", &config_toml.source.repo, &config_toml.source.directory, &logo_selected.name.to_str().unwrap_or_default());
|
2025-06-14 18:00:40 +01:00
|
|
|
// }
|