forked from Skynet/discord-bot
fmt: cargo+clippy
This commit is contained in:
parent
51bc2f177f
commit
9d50efb757
5 changed files with 222 additions and 583 deletions
|
@ -1,30 +1,30 @@
|
|||
use chrono::{Datelike, Utc};
|
||||
use rand::{rngs::SmallRng, seq::IndexedRandom, SeedableRng};
|
||||
use serde::Deserialize;
|
||||
use serenity::{
|
||||
all::GuildId,
|
||||
async_trait,
|
||||
builder::{CreateAttachment, EditGuild},
|
||||
client::{Context, EventHandler},
|
||||
model::gateway::{GatewayIntents, Ready},
|
||||
Client,
|
||||
};
|
||||
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;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use chrono::{Datelike, Utc};
|
||||
use gdk_pixbuf::{Pixbuf, PixbufFormat, PixbufLoader};
|
||||
use gdk_pixbuf::prelude::PixbufLoaderExt;
|
||||
use rand::rngs::SmallRng;
|
||||
use rand::SeedableRng;
|
||||
use rand::seq::IndexedRandom;
|
||||
use resvg::usvg;
|
||||
use serde::Deserialize;
|
||||
use serenity::all::GuildId;
|
||||
use serenity::builder::{CreateAttachment, EditGuild};
|
||||
use skynet_discord_bot::{
|
||||
common::{
|
||||
database::{db_init, DataBase},
|
||||
renderer::{Args, Renderer},
|
||||
},
|
||||
get_config, get_now_iso, Config,
|
||||
};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::{
|
||||
ffi::OsString,
|
||||
fs,
|
||||
path::PathBuf,
|
||||
process::{self, Command},
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio::sync::RwLock;
|
||||
use skynet_discord_bot::common::renderer::{Args, Renderer};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -61,7 +61,6 @@ impl EventHandler for Handler {
|
|||
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;
|
||||
|
@ -82,19 +81,17 @@ async fn update_icon_main(ctx: Arc<Context>) {
|
|||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||
};
|
||||
|
||||
|
||||
|
||||
let config_global = config_lock.read().await;
|
||||
let config_toml = get_config_icons();
|
||||
let server = GuildId::new(689189992417067052);
|
||||
|
||||
|
||||
|
||||
// clone repo into local folder
|
||||
clone_repo(&config_global, &config_toml);
|
||||
|
||||
|
||||
// see if there is a current festival
|
||||
let festival_data = get_festival(&config_toml);
|
||||
|
||||
|
||||
// get a list of all the graphics files
|
||||
let logos = get_logos(&config_global, &config_toml);
|
||||
|
||||
|
@ -104,31 +101,29 @@ 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, &db,&server, logo_selected).await;
|
||||
logo_set(&ctx, &db, &server, logo_selected).await;
|
||||
}
|
||||
|
||||
struct FestivalData{
|
||||
#[derive(Debug)]
|
||||
struct FestivalData {
|
||||
current: Vec<String>,
|
||||
exclusions: Vec<String>,
|
||||
}
|
||||
|
||||
fn get_festival(config_toml: &ConfigToml)-> FestivalData {
|
||||
fn get_festival(config_toml: &ConfigToml) -> FestivalData {
|
||||
let today = Utc::now();
|
||||
let day = today.day();
|
||||
let month = today.month();
|
||||
let year = today.year();
|
||||
|
||||
|
||||
let mut result = FestivalData {
|
||||
current: vec![],
|
||||
exclusions: vec![],
|
||||
};
|
||||
|
||||
|
||||
for festival in &config_toml.festivals {
|
||||
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 {
|
||||
result.current.push(festival.name.to_owned());
|
||||
} else if (year >= festival.start.year && year <= festival.end.year) {
|
||||
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) {
|
||||
result.current.push(festival.name.to_owned());
|
||||
}
|
||||
} else if !festival.all_year {
|
||||
|
@ -144,24 +139,24 @@ struct ConfigToml {
|
|||
source: ConfigTomlSource,
|
||||
festivals: Vec<ConfigTomlFestivals>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ConfigTomlSource {
|
||||
repo: String,
|
||||
directory: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ConfigTomlFestivals {
|
||||
name: String,
|
||||
all_year: bool,
|
||||
start: ConfigTomlFestivalsTime,
|
||||
end: ConfigTomlFestivalsTime,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ConfigTomlFestivalsTime {
|
||||
day: u32,
|
||||
month: u32,
|
||||
year:i32
|
||||
year: i32,
|
||||
}
|
||||
|
||||
fn get_config_icons() -> ConfigToml {
|
||||
|
@ -169,56 +164,34 @@ fn get_config_icons() -> ConfigToml {
|
|||
let config: ConfigToml = toml::from_str(toml_raw).unwrap();
|
||||
config
|
||||
}
|
||||
fn clone_repo(config: &Config, config_toml: &ConfigToml){
|
||||
fn clone_repo(config: &Config, config_toml: &ConfigToml) {
|
||||
let url = &config_toml.source.repo;
|
||||
let folder = format!("{}/open-governance", &config.home);
|
||||
|
||||
Command::new("git")
|
||||
.arg("clone")
|
||||
.arg(url)
|
||||
.arg(&folder)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
Command::new("git").arg("clone").arg(url).arg(&folder).output().expect("failed to execute process");
|
||||
|
||||
Command::new("git")
|
||||
.arg("pull")
|
||||
.arg("origin")
|
||||
.arg("main")
|
||||
.current_dir(&folder)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
}
|
||||
|
||||
fn convert_svg_to_png(original: &PathBuf, out: &PathBuf){
|
||||
let mut f = File::open(original).unwrap();
|
||||
let mut buffer = Vec::new();
|
||||
// read the whole file
|
||||
f.read_to_end(&mut buffer).unwrap();
|
||||
let loader = PixbufLoader::with_mime_type("image/svg+xml")
|
||||
.expect("error loader");
|
||||
loader.write(&buffer).expect("TODO: panic message");
|
||||
loader.close().expect("TODO: panic message");
|
||||
let pixbuf = loader.pixbuf().expect("no pixbuf");
|
||||
|
||||
let (width, height) = (pixbuf.width(), pixbuf.height());
|
||||
println!("size: {}x{}", width, height);
|
||||
let bytes: Vec<u8> =
|
||||
pixbuf.save_to_bufferv("png", &[]).expect("must not error");
|
||||
fs::write(out, &bytes).expect("TODO: panic message");
|
||||
.arg("pull")
|
||||
.arg("origin")
|
||||
.arg("main")
|
||||
.current_dir(&folder)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct LogoData {
|
||||
name: OsString,
|
||||
path: PathBuf,
|
||||
}
|
||||
fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
||||
fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
||||
let folder = format!("{}/open-governance/{}", &config.home, &config_toml.source.directory);
|
||||
let folder_path = PathBuf::from(&folder);
|
||||
let mut folder_output = folder_path.clone();
|
||||
folder_output.push("converted");
|
||||
let paths = fs::read_dir(folder).unwrap();
|
||||
|
||||
let args = Args{
|
||||
let args = Args {
|
||||
input: folder_path.clone(),
|
||||
output: folder_output,
|
||||
colors: String::from(""),
|
||||
|
@ -226,15 +199,15 @@ fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
|||
height: 1024,
|
||||
};
|
||||
let mut r = Renderer::new(&args).unwrap();
|
||||
|
||||
|
||||
let mut logos = vec![];
|
||||
|
||||
|
||||
for tmp in paths.flatten() {
|
||||
let path_local = tmp.path().to_owned();
|
||||
let path_local2 = tmp.path().to_owned();
|
||||
let name = path_local2.file_name().unwrap().to_owned();
|
||||
let mut path = tmp.path();
|
||||
|
||||
|
||||
if path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
|
@ -250,14 +223,14 @@ fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
|||
path_new.pop();
|
||||
path_new.push("converted");
|
||||
path_new.push(filename);
|
||||
|
||||
|
||||
// check if exists
|
||||
if !path_new.exists() {
|
||||
// convert if it hasnt been converted already
|
||||
match r.render(&path_local, &args) {
|
||||
Ok(_) => log::info!("Successfully rendered all colors of {path_local:?}"),
|
||||
Err(e) => {
|
||||
log::error!("Failed to render {path_local:?}: {}", e)
|
||||
Ok(_) => {}
|
||||
Err(_e) => {
|
||||
dbg!("Failed to render {path_local:?}: {}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -266,18 +239,18 @@ fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
|||
}
|
||||
};
|
||||
|
||||
logos.push(LogoData{
|
||||
logos.push(LogoData {
|
||||
name,
|
||||
path,
|
||||
});
|
||||
|
||||
println!("Name: {}", &tmp.path().display());
|
||||
// println!("Name: {}", &tmp.path().display());
|
||||
}
|
||||
|
||||
|
||||
logos
|
||||
}
|
||||
|
||||
fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<LogoData>{
|
||||
fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<LogoData> {
|
||||
let mut filtered: Vec<LogoData> = vec![];
|
||||
|
||||
'outer: for logo in existing {
|
||||
|
@ -310,7 +283,6 @@ fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<L
|
|||
filtered
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, sqlx::FromRow)]
|
||||
pub struct ServerIcons {
|
||||
pub id: i64,
|
||||
|
@ -318,9 +290,9 @@ pub struct ServerIcons {
|
|||
pub date: String,
|
||||
}
|
||||
|
||||
async fn logo_set(ctx: &Arc<Context>, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData){
|
||||
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;
|
||||
logo_set_db(db, logo_selected).await;
|
||||
|
||||
let icon = CreateAttachment::path(logo_selected.path.to_str().unwrap_or_default()).await.unwrap();
|
||||
|
||||
|
@ -329,21 +301,27 @@ async fn logo_set(ctx: &Arc<Context>, db: &Pool<Sqlite>, server: &GuildId, logo
|
|||
server.edit(ctx, builder).await.unwrap();
|
||||
}
|
||||
|
||||
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData){
|
||||
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>(
|
||||
match sqlx::query_as::<_, ServerIcons>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_icons (name, date)
|
||||
VALUES (?1, ?2, ?3)
|
||||
VALUES (?1, ?2)
|
||||
",
|
||||
)
|
||||
.bind(name)
|
||||
.bind(get_now_iso(false))
|
||||
.fetch_optional(db)
|
||||
.await;
|
||||
.bind(name)
|
||||
.bind(get_now_iso(false))
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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