feat: save the selected image to teh library
This commit is contained in:
parent
3523dac46e
commit
51bc2f177f
2 changed files with 44 additions and 5 deletions
8
db/migrations/11_server-icons.sql
Normal file
8
db/migrations/11_server-icons.sql
Normal file
|
@ -0,0 +1,8 @@
|
|||
|
||||
CREATE TABLE IF NOT EXISTS server_icons (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
date TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_name ON server_icons (name);
|
|
@ -4,8 +4,8 @@ use serenity::{
|
|||
model::gateway::{GatewayIntents, Ready},
|
||||
Client,
|
||||
};
|
||||
use skynet_discord_bot::common::database::{db_init, DataBase};
|
||||
use skynet_discord_bot::{get_config, Config};
|
||||
use skynet_discord_bot::common::database::{db_init, DataBase, RoleAdder};
|
||||
use skynet_discord_bot::{get_config, get_now_iso, Config};
|
||||
use std::{fs, process, sync::Arc};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs::File;
|
||||
|
@ -22,6 +22,7 @@ use resvg::usvg;
|
|||
use serde::Deserialize;
|
||||
use serenity::all::GuildId;
|
||||
use serenity::builder::{CreateAttachment, EditGuild};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use tokio::sync::RwLock;
|
||||
use skynet_discord_bot::common::renderer::{Args, Renderer};
|
||||
|
||||
|
@ -103,7 +104,7 @@ async fn update_icon_main(ctx: Arc<Context>) {
|
|||
let mut rng = SmallRng::from_os_rng();
|
||||
let logo_selected = logos_filtered.choose(&mut rng).unwrap();
|
||||
|
||||
logo_set(&ctx, &server, logo_selected).await;
|
||||
logo_set(&ctx, &db,&server, logo_selected).await;
|
||||
}
|
||||
|
||||
struct FestivalData{
|
||||
|
@ -309,10 +310,40 @@ fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<L
|
|||
filtered
|
||||
}
|
||||
|
||||
async fn logo_set(ctx: &Arc<Context>, server: &GuildId, logo_selected: &LogoData){
|
||||
|
||||
#[derive(Debug, Clone, sqlx::FromRow)]
|
||||
pub struct ServerIcons {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub date: String,
|
||||
}
|
||||
|
||||
async fn logo_set(ctx: &Arc<Context>, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData){
|
||||
// add to teh database
|
||||
logo_set_db(db ,logo_selected).await;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData){
|
||||
let name = logo_selected.name.to_str().unwrap_or_default();
|
||||
|
||||
sqlx::query_as::<_, ServerIcons>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_icons (name, date)
|
||||
VALUES (?1, ?2, ?3)
|
||||
",
|
||||
)
|
||||
.bind(name)
|
||||
.bind(get_now_iso(false))
|
||||
.fetch_optional(db)
|
||||
.await;
|
||||
}
|
||||
|
||||
// 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());
|
||||
// }
|
Loading…
Add table
Add a link
Reference in a new issue