From 0f4524ea637ef29aba7f2dab964015274d0ff0c9 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 16 Jun 2025 05:19:58 +0100 Subject: [PATCH] feat: tidied up the command outouts --- db/migrations/11_server-icons.sql | 1 + src/commands/server_icon.rs | 37 ++++++++++++++++++++++--------- src/common/server_icon.rs | 13 ++++++----- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/db/migrations/11_server-icons.sql b/db/migrations/11_server-icons.sql index 37d8b5b..20fb472 100644 --- a/db/migrations/11_server-icons.sql +++ b/db/migrations/11_server-icons.sql @@ -2,6 +2,7 @@ CREATE TABLE IF NOT EXISTS server_icons ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, + path TEXT NOT NULL, date TEXT NOT NULL ); diff --git a/src/commands/server_icon.rs b/src/commands/server_icon.rs index a6114c5..4ce10ac 100644 --- a/src/commands/server_icon.rs +++ b/src/commands/server_icon.rs @@ -51,7 +51,7 @@ pub(crate) mod user { .add_sub_option(CreateCommandOption::new(CommandOptionType::SubCommand, "icon", "Information on current icon.")) .add_sub_option(CreateCommandOption::new(CommandOptionType::SubCommand, "festival", "Information on current festival.")), ) - .add_option(CreateCommandOption::new(CommandOptionType::SubCommand, "stats", "Some Stats.")) + .add_option(CreateCommandOption::new(CommandOptionType::SubCommand, "stats", "How many times the particular icon has been used")) } fn get_logo_url(config_toml: &ConfigTomlLocal, logo_name: &str) -> String { @@ -64,6 +64,7 @@ pub(crate) mod user { pub(crate) mod icon { use super::*; + use serenity::all::{CreateAttachment, EditInteractionResponse}; use sqlx::{Pool, Sqlite}; @@ -77,7 +78,15 @@ pub(crate) mod user { let config_toml = get_config_icons::minimal(); if let Some(logo) = get_current_icon(&db).await { - get_logo_url(&config_toml, &logo.name) + let attachment = CreateAttachment::path(&logo.path).await.unwrap(); + match command.edit_response(&ctx.http, EditInteractionResponse::new().new_attachment(attachment)).await { + Ok(_) => {} + Err(e) => { + dbg!(e); + } + } + + format!("[{}]({})", &logo.name, get_logo_url(&config_toml, &logo.name)) } else { "Could not find current icon".to_string() } @@ -102,8 +111,8 @@ pub(crate) mod user { } pub(crate) mod festival { - use serenity::all::{CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption}; - use skynet_discord_bot::common::server_icon::get_config_icons; + use serenity::all::{CommandInteraction, Context}; + use skynet_discord_bot::common::server_icon::{get_config_icons, update_icon::get_festival}; use skynet_discord_bot::Config; // use this to return what current festivals are active? @@ -116,13 +125,13 @@ pub(crate) mod user { let config_toml = get_config_icons::full(&config); - let mut response = vec![]; + let response = get_festival(&config_toml).current; - for festival in &config_toml.festivals { - response.push(festival.name.to_owned()); + if response.is_empty() { + "No festival currently active".to_string() + } else { + format!("Festivals active: {}", response.join(", ")) } - - format!("Festivals active: {}", response.join(", ")) } } } @@ -170,6 +179,10 @@ pub(crate) mod user { } fn fmt_msg(config_toml: &ConfigTomlLocal, totals: &Vec) -> String { + let mut totals_local = totals.clone(); + totals_local.sort_by_key(|x| x.times); + totals_local.reverse(); + // msg can be a max 2000 chars long let mut limit = 2000 - 3; @@ -177,7 +190,7 @@ pub(crate) mod user { for CountResult { name, times, - } in totals + } in &totals_local { let current_leading = if times < &10 { "00" @@ -189,7 +202,9 @@ pub(crate) mod user { let url = get_logo_url(config_toml, name); - let line = format!("{}{} <{}>", current_leading, times, url); + // the `` is so that the numbers will be rendered in monospaced font + // the <> is to suppress the URL embed + let line = format!("``{}{}`` [{}](<{}>)", current_leading, times, name, url); let length = line.len() + 1; diff --git a/src/common/server_icon.rs b/src/common/server_icon.rs index 3f88d34..ab4b991 100644 --- a/src/common/server_icon.rs +++ b/src/common/server_icon.rs @@ -72,6 +72,7 @@ pub struct LogoData { pub struct ServerIcons { pub id: i64, pub name: String, + pub path: String, pub date: String, } @@ -120,12 +121,12 @@ pub mod update_icon { } #[derive(Debug)] - struct FestivalData { - current: Vec, + pub struct FestivalData { + pub current: Vec, exclusions: Vec, } - fn get_festival(config_toml: &ConfigToml) -> FestivalData { + pub fn get_festival(config_toml: &ConfigToml) -> FestivalData { let today = Utc::now(); let day = today.day(); let month = today.month(); @@ -276,15 +277,17 @@ pub mod update_icon { async fn logo_set_db(db: &Pool, logo_selected: &LogoData) { let name = logo_selected.name.to_str().unwrap_or_default(); + let path = logo_selected.path.to_str().unwrap_or_default(); match sqlx::query_as::<_, ServerIcons>( " - INSERT OR REPLACE INTO server_icons (name, date) - VALUES (?1, ?2) + INSERT OR REPLACE INTO server_icons (name, date, path) + VALUES (?1, ?2, ?3) ", ) .bind(name) .bind(get_now_iso(false)) + .bind(path) .fetch_optional(db) .await {