Moved albums to the database

+ Clippy + fmt
This commit is contained in:
2023-01-20 15:01:31 -07:00
parent 362977caa9
commit 8d35d350ad
5 changed files with 79 additions and 33 deletions
+25 -16
View File
@@ -1,7 +1,8 @@
use crate::config::AlbumConfig;
use crate::error::Error;
use crate::imgur::get_album_images;
use crate::models::album::AlbumConfig;
use crate::{command, group, GlobalData};
use j_db::model::JdbModel;
use serenity::client::Context;
use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message;
@@ -54,10 +55,9 @@ async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
}
};
global_data.cfg.albums.push(AlbumConfig {
album_id,
name: album_name.clone(),
});
global_data
.db
.insert(AlbumConfig::new(&album_name, &album_id))?;
global_data
.bot_state
@@ -82,15 +82,25 @@ async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult
let global_data = data.get_mut::<GlobalData>().unwrap();
global_data
.cfg
.albums
.retain(|album| !album.name.eq_ignore_ascii_case(&album_name));
let album = AlbumConfig::get_album_by_name(&global_data.db, &album_name)?;
global_data.bot_state.albums.remove(&album_name);
match album {
None => {
msg.reply(
&ctx.http,
"I already got rid of that one, or I never had it. Who knows!",
)
.await?;
}
Some(album) => {
global_data.bot_state.albums.remove(&album_name);
msg.reply(&ctx.http, format!("{} album removed!", album_name))
.await?;
global_data.db.remove::<AlbumConfig>(album.id().unwrap())?;
msg.reply(&ctx.http, format!("{} album removed!", album_name))
.await?;
}
}
Ok(())
}
@@ -105,10 +115,9 @@ async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
let global_data = data.get::<GlobalData>().unwrap();
let album_names: Vec<String> = global_data
.cfg
.albums
.iter()
.map(|album| album.name.clone())
.db
.filter(|_, _: &AlbumConfig| true)?
.map(|album| album.name)
.collect();
if album_names.is_empty() {
+5 -3
View File
@@ -2,6 +2,8 @@ use crate::error::Error;
use crate::models::insult_compliment::{RandomResponseTemplate, ResponseType};
use crate::models::random::RandomConfig;
use crate::{command, group, GlobalData, BAD_APPLE};
use rand::prelude::SliceRandom;
use rand::thread_rng;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serenity::client::Context;
@@ -53,11 +55,11 @@ impl RandomCtx {
pub fn new(user_name: &str, global_data: &GlobalData) -> Result<Self, Error> {
let mut random_image: HashMap<String, String> = HashMap::new();
for album in &global_data.cfg.albums {
let image = global_data.bot_state.get_image(&album.name, Vec::new())?;
for (album_name, images) in &global_data.bot_state.albums {
let image = images.choose(&mut thread_rng());
if let Some(image) = image {
random_image.insert(album.name.clone(), image.link);
random_image.insert(album_name.clone(), image.link.clone());
}
}