feat: cleaned up remaining unwraps, and then clippy+fmt
This commit is contained in:
parent
652dd6ff1c
commit
9134feee4e
3 changed files with 105 additions and 33 deletions
|
@ -17,7 +17,7 @@ pub(crate) mod admin {
|
||||||
pub(crate) mod change {
|
pub(crate) mod change {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String {
|
||||||
let db_lock = {
|
let db_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
||||||
|
@ -78,11 +78,12 @@ pub(crate) mod user {
|
||||||
let config_toml = get_config_icons::minimal();
|
let config_toml = get_config_icons::minimal();
|
||||||
|
|
||||||
if let Some(logo) = get_current_icon(&db).await {
|
if let Some(logo) = get_current_icon(&db).await {
|
||||||
let attachment = CreateAttachment::path(&logo.path).await.unwrap();
|
if let Ok(attachment) = CreateAttachment::path(&logo.path).await {
|
||||||
match command.edit_response(&ctx.http, EditInteractionResponse::new().new_attachment(attachment)).await {
|
match command.edit_response(&ctx.http, EditInteractionResponse::new().new_attachment(attachment)).await {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
dbg!(e);
|
dbg!(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +117,7 @@ pub(crate) mod user {
|
||||||
use skynet_discord_bot::Config;
|
use skynet_discord_bot::Config;
|
||||||
|
|
||||||
// use this to return what current festivals are active?
|
// use this to return what current festivals are active?
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String {
|
||||||
let config_lock = {
|
let config_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||||
|
@ -141,7 +142,7 @@ pub(crate) mod user {
|
||||||
use super::*;
|
use super::*;
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
|
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String {
|
||||||
let db_lock = {
|
let db_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
||||||
|
@ -178,8 +179,8 @@ pub(crate) mod user {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_msg(config_toml: &ConfigTomlLocal, totals: &Vec<CountResult>) -> String {
|
fn fmt_msg(config_toml: &ConfigTomlLocal, totals: &[CountResult]) -> String {
|
||||||
let mut totals_local = totals.clone();
|
let mut totals_local = totals.to_owned();
|
||||||
totals_local.sort_by_key(|x| x.times);
|
totals_local.sort_by_key(|x| x.times);
|
||||||
totals_local.reverse();
|
totals_local.reverse();
|
||||||
|
|
||||||
|
|
|
@ -43,8 +43,16 @@ pub mod get_config_icons {
|
||||||
}
|
}
|
||||||
pub fn minimal() -> ConfigTomlLocal {
|
pub fn minimal() -> ConfigTomlLocal {
|
||||||
let toml_raw_min = include_str!("../../.server-icons.toml");
|
let toml_raw_min = include_str!("../../.server-icons.toml");
|
||||||
let config_min: ConfigTomlLocal = toml::from_str(toml_raw_min).unwrap();
|
toml::from_str::<ConfigTomlLocal>(toml_raw_min).unwrap_or_else(|e| {
|
||||||
config_min
|
dbg!(e);
|
||||||
|
ConfigTomlLocal {
|
||||||
|
source: ConfigTomlSource {
|
||||||
|
repo: "".to_string(),
|
||||||
|
directory: "".to_string(),
|
||||||
|
file: "".to_string(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// since a copy of the festival file is in the repo we just need to get to it
|
// since a copy of the festival file is in the repo we just need to get to it
|
||||||
|
@ -52,12 +60,21 @@ pub mod get_config_icons {
|
||||||
let config_source = minimal();
|
let config_source = minimal();
|
||||||
|
|
||||||
let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file);
|
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 contents = fs::read_to_string(file_path).unwrap_or_else(|e| {
|
||||||
let config_festivals: ConfigTomlRemote = toml::from_str(&contents).unwrap();
|
dbg!(e);
|
||||||
|
"".to_string()
|
||||||
|
});
|
||||||
|
let festivals = match toml::from_str::<ConfigTomlRemote>(&contents) {
|
||||||
|
Ok(config_festivals) => config_festivals.festivals,
|
||||||
|
Err(e) => {
|
||||||
|
dbg!(e);
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ConfigToml {
|
ConfigToml {
|
||||||
source: config_source.source,
|
source: config_source.source,
|
||||||
festivals: config_festivals.festivals,
|
festivals,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,15 +171,26 @@ pub mod update_icon {
|
||||||
let url = &config_toml.source.repo;
|
let url = &config_toml.source.repo;
|
||||||
let folder = format!("{}/open-governance", &config.home);
|
let folder = format!("{}/open-governance", &config.home);
|
||||||
|
|
||||||
Command::new("git").arg("clone").arg(url).arg(&folder).output().expect("failed to execute process");
|
if let Err(e) = Command::new("git")
|
||||||
|
// clone the repo, gracefully "fails"
|
||||||
|
.arg("clone")
|
||||||
|
.arg(url)
|
||||||
|
.arg(&folder)
|
||||||
|
.output()
|
||||||
|
{
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
|
|
||||||
Command::new("git")
|
if let Err(e) = Command::new("git")
|
||||||
|
// Update the repo
|
||||||
.arg("pull")
|
.arg("pull")
|
||||||
.arg("origin")
|
.arg("origin")
|
||||||
.arg("main")
|
.arg("main")
|
||||||
.current_dir(&folder)
|
.current_dir(&folder)
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to execute process");
|
{
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec<LogoData> {
|
||||||
|
@ -170,7 +198,13 @@ pub mod update_icon {
|
||||||
let folder_path = PathBuf::from(&folder);
|
let folder_path = PathBuf::from(&folder);
|
||||||
let mut folder_output = folder_path.clone();
|
let mut folder_output = folder_path.clone();
|
||||||
folder_output.push("converted");
|
folder_output.push("converted");
|
||||||
let paths = fs::read_dir(folder).unwrap();
|
let paths = match fs::read_dir(folder) {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
dbg!(e);
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let args = Args {
|
let args = Args {
|
||||||
input: folder_path.clone(),
|
input: folder_path.clone(),
|
||||||
|
@ -179,14 +213,26 @@ pub mod update_icon {
|
||||||
width: 1024,
|
width: 1024,
|
||||||
height: 1024,
|
height: 1024,
|
||||||
};
|
};
|
||||||
let mut r = Renderer::new(&args).unwrap();
|
let mut r = match Renderer::new(&args) {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
let _ = dbg!(e);
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let mut logos = vec![];
|
let mut logos = vec![];
|
||||||
|
|
||||||
for tmp in paths.flatten() {
|
for tmp in paths.flatten() {
|
||||||
let path_local = tmp.path().to_owned();
|
let path_local = tmp.path().to_owned();
|
||||||
let path_local2 = tmp.path().to_owned();
|
let path_local2 = tmp.path().to_owned();
|
||||||
let name = path_local2.file_name().unwrap().to_owned();
|
let name = match path_local2.file_name() {
|
||||||
|
None => {
|
||||||
|
dbg!(path_local2);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Some(x) => x.to_owned(),
|
||||||
|
};
|
||||||
let mut path = tmp.path();
|
let mut path = tmp.path();
|
||||||
|
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
|
@ -200,7 +246,13 @@ pub mod update_icon {
|
||||||
let mut path_new = path_local.clone();
|
let mut path_new = path_local.clone();
|
||||||
path_new.set_extension("png");
|
path_new.set_extension("png");
|
||||||
let filename_tmp = path_new.clone();
|
let filename_tmp = path_new.clone();
|
||||||
let filename = filename_tmp.file_name().unwrap_or_default();
|
let filename = match filename_tmp.file_name() {
|
||||||
|
None => {
|
||||||
|
dbg!(filename_tmp);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Some(x) => x,
|
||||||
|
};
|
||||||
path_new.pop();
|
path_new.pop();
|
||||||
path_new.push("converted");
|
path_new.push("converted");
|
||||||
path_new.push(filename);
|
path_new.push(filename);
|
||||||
|
@ -266,18 +318,36 @@ pub mod update_icon {
|
||||||
|
|
||||||
async fn logo_set(ctx: &Context, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) {
|
async fn logo_set(ctx: &Context, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) {
|
||||||
// add to teh database
|
// add to teh database
|
||||||
logo_set_db(db, logo_selected).await;
|
if !logo_set_db(db, logo_selected).await {
|
||||||
|
// something went wrong
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let icon = CreateAttachment::path(logo_selected.path.to_str().unwrap_or_default()).await.unwrap();
|
if let Some(logo_path) = logo_selected.path.to_str() {
|
||||||
|
match CreateAttachment::path(logo_path).await {
|
||||||
// assuming a `guild` has already been bound
|
Ok(icon) => {
|
||||||
let builder = EditGuild::new().icon(Some(&icon));
|
// assuming a `guild` has already been bound
|
||||||
server.edit(ctx, builder).await.unwrap();
|
let builder = EditGuild::new().icon(Some(&icon));
|
||||||
|
if let Err(e) = server.edit(ctx, builder).await {
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) {
|
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) -> bool {
|
||||||
let name = logo_selected.name.to_str().unwrap_or_default();
|
let name = match logo_selected.name.to_str() {
|
||||||
let path = logo_selected.path.to_str().unwrap_or_default();
|
None => return false,
|
||||||
|
Some(x) => x,
|
||||||
|
};
|
||||||
|
let path = match logo_selected.path.to_str() {
|
||||||
|
None => return false,
|
||||||
|
Some(x) => x,
|
||||||
|
};
|
||||||
|
|
||||||
match sqlx::query_as::<_, ServerIcons>(
|
match sqlx::query_as::<_, ServerIcons>(
|
||||||
"
|
"
|
||||||
|
@ -294,7 +364,9 @@ pub mod update_icon {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
dbg!(e);
|
dbg!(e);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,7 @@ pub mod commands;
|
||||||
|
|
||||||
use crate::commands::role_adder::tools::on_role_change;
|
use crate::commands::role_adder::tools::on_role_change;
|
||||||
use serenity::all::{
|
use serenity::all::{
|
||||||
ActivityData, Command, CommandDataOption, CommandDataOptionValue, CommandOptionType, CreateMessage, EditInteractionResponse, GuildId,
|
ActivityData, Command, CommandDataOptionValue, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent, Interaction,
|
||||||
GuildMemberUpdateEvent, Interaction,
|
|
||||||
};
|
};
|
||||||
use serenity::model::guild::Member;
|
use serenity::model::guild::Member;
|
||||||
use serenity::{
|
use serenity::{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue