forked from Skynet/discord-bot
Various refactors
This commit is contained in:
parent
e5a40261d9
commit
cde3e93565
2 changed files with 38 additions and 66 deletions
|
@ -67,17 +67,11 @@ impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves {
|
|||
}
|
||||
|
||||
fn get_discord_from_row(row: &SqliteRow) -> Option<UserId> {
|
||||
match row.try_get("discord") {
|
||||
Ok(x) => {
|
||||
let tmp: i64 = x;
|
||||
if tmp == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(UserId::from(tmp as u64))
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
let x: i64 = row.try_get("discord").ok()?;
|
||||
if x == 0 {
|
||||
return None;
|
||||
}
|
||||
Some(UserId::from(x as u64))
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
|
|
|
@ -60,17 +60,11 @@ pub mod get_config_icons {
|
|||
let config_source = minimal();
|
||||
|
||||
let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file);
|
||||
let contents = fs::read_to_string(file_path).unwrap_or_else(|e| {
|
||||
dbg!(e);
|
||||
"".to_string()
|
||||
});
|
||||
let festivals = match toml::from_str::<ConfigTomlRemote>(&contents) {
|
||||
Ok(config_festivals) => config_festivals.festivals,
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
vec![]
|
||||
}
|
||||
};
|
||||
let contents = fs::read_to_string(file_path).map_err(|e| dbg!(e)).unwrap_or_default();
|
||||
let festivals = toml::from_str::<ConfigTomlRemote>(&contents)
|
||||
.map(|config| config.festivals)
|
||||
.map_err(|e| dbg!(e))
|
||||
.unwrap_or_default();
|
||||
|
||||
ConfigToml {
|
||||
source: config_source.source,
|
||||
|
@ -308,16 +302,15 @@ pub mod update_icon {
|
|||
fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<LogoData> {
|
||||
let mut filtered: Vec<LogoData> = vec![];
|
||||
|
||||
let allowed_files = vec![".png", ".jpeg", ".gif", ".svg"];
|
||||
let allowed_extensions = ["png", "jpeg", "gif", "svg"];
|
||||
'outer: for logo in existing {
|
||||
let name_lowercase0 = logo.name.to_ascii_lowercase();
|
||||
let name_lowercase = name_lowercase0.to_str().unwrap_or_default();
|
||||
let mut allowed = false;
|
||||
for allowed_type in &allowed_files {
|
||||
if name_lowercase.ends_with(allowed_type) {
|
||||
allowed = true;
|
||||
}
|
||||
}
|
||||
let name_lowercase = logo.name.to_ascii_lowercase();
|
||||
let name_lowercase = name_lowercase.to_str().unwrap_or_default();
|
||||
|
||||
let allowed = {
|
||||
let extension = name_lowercase.split('.').next_back().unwrap_or_default();
|
||||
allowed_extensions.contains(&extension)
|
||||
};
|
||||
if !allowed {
|
||||
continue;
|
||||
}
|
||||
|
@ -332,13 +325,7 @@ pub mod update_icon {
|
|||
}
|
||||
} else {
|
||||
// else filter using the excluded ones
|
||||
let mut excluded = false;
|
||||
for festival in &festival_data.exclusions {
|
||||
if name_lowercase.contains(festival) {
|
||||
excluded = true;
|
||||
}
|
||||
}
|
||||
|
||||
let excluded = festival_data.exclusions.iter().any(|festival| name_lowercase.contains(festival));
|
||||
if !excluded {
|
||||
filtered.push(logo);
|
||||
}
|
||||
|
@ -350,38 +337,33 @@ pub mod update_icon {
|
|||
|
||||
async fn logo_set(ctx: &Context, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) {
|
||||
// add to the database
|
||||
if !logo_set_db(db, logo_selected).await {
|
||||
if logo_set_db(db, logo_selected).await.is_err() {
|
||||
// something went wrong
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(logo_path) = logo_selected.path.to_str() {
|
||||
match CreateAttachment::path(logo_path).await {
|
||||
Ok(icon) => {
|
||||
// assuming a `guild` has already been bound
|
||||
let builder = EditGuild::new().icon(Some(&icon));
|
||||
if let Err(e) = server.edit(ctx, builder).await {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let Some(logo_path) = logo_selected.path.to_str() else {
|
||||
return;
|
||||
};
|
||||
match CreateAttachment::path(logo_path).await {
|
||||
Ok(icon) => {
|
||||
// assuming a `guild` has already been bound
|
||||
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) -> bool {
|
||||
let name = match logo_selected.name.to_str() {
|
||||
None => return false,
|
||||
Some(x) => x,
|
||||
};
|
||||
let path = match logo_selected.path.to_str() {
|
||||
None => return false,
|
||||
Some(x) => x,
|
||||
};
|
||||
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) -> Result<(), ()> {
|
||||
let name = logo_selected.name.to_str().ok_or(())?;
|
||||
let path = logo_selected.path.to_str().ok_or(())?;
|
||||
|
||||
match sqlx::query_as::<_, ServerIcons>(
|
||||
sqlx::query_as::<_, ServerIcons>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_icons (name, date, path)
|
||||
VALUES (?1, ?2, ?3)
|
||||
|
@ -392,13 +374,9 @@ pub mod update_icon {
|
|||
.bind(path)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
.map_err(|e| {
|
||||
dbg!(e);
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue