Moved albums to the database
+ Clippy + fmt
This commit is contained in:
parent
362977caa9
commit
8d35d350ad
@ -1,6 +1,7 @@
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::imgur;
|
use crate::imgur;
|
||||||
use crate::imgur::Image;
|
use crate::imgur::Image;
|
||||||
|
use crate::models::album::AlbumConfig;
|
||||||
use config::{Config, File};
|
use config::{Config, File};
|
||||||
use j_db::database::Database;
|
use j_db::database::Database;
|
||||||
use rand::prelude::SliceRandom;
|
use rand::prelude::SliceRandom;
|
||||||
@ -20,12 +21,6 @@ pub struct Args {
|
|||||||
pub cfg_path: PathBuf,
|
pub cfg_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
||||||
pub struct AlbumConfig {
|
|
||||||
pub name: String,
|
|
||||||
pub album_id: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
pub struct MotivationConfig {
|
pub struct MotivationConfig {
|
||||||
pub album: Vec<String>,
|
pub album: Vec<String>,
|
||||||
@ -43,9 +38,6 @@ pub struct BotConfig {
|
|||||||
pub db_path: PathBuf,
|
pub db_path: PathBuf,
|
||||||
pub admins: Vec<UserId>,
|
pub admins: Vec<UserId>,
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub albums: Vec<AlbumConfig>,
|
|
||||||
|
|
||||||
pub motivation: MotivationConfig,
|
pub motivation: MotivationConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,10 +67,10 @@ pub struct BotState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BotState {
|
impl BotState {
|
||||||
pub async fn new(cfg: &BotConfig) -> Result<Self, Error> {
|
pub async fn new(cfg: &BotConfig, db: &Database) -> Result<Self, Error> {
|
||||||
let mut albums: HashMap<String, Vec<Image>> = HashMap::new();
|
let mut albums: HashMap<String, Vec<Image>> = HashMap::new();
|
||||||
|
|
||||||
for album in &cfg.albums {
|
for album in db.filter(|_, _: &AlbumConfig| true)? {
|
||||||
albums.insert(
|
albums.insert(
|
||||||
album.name.clone(),
|
album.name.clone(),
|
||||||
imgur::get_album_images(&cfg.imgur_client_id, &album.album_id).await?,
|
imgur::get_album_images(&cfg.imgur_client_id, &album.album_id).await?,
|
||||||
@ -133,10 +125,11 @@ pub struct GlobalData {
|
|||||||
|
|
||||||
impl GlobalData {
|
impl GlobalData {
|
||||||
pub async fn new(args: Args, cfg: BotConfig) -> Result<Self, Error> {
|
pub async fn new(args: Args, cfg: BotConfig) -> Result<Self, Error> {
|
||||||
|
let db = Database::new(&cfg.db_path)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
args,
|
args,
|
||||||
bot_state: BotState::new(&cfg).await?,
|
bot_state: BotState::new(&cfg, &db).await?,
|
||||||
db: Database::new(&cfg.db_path)?,
|
db,
|
||||||
cfg,
|
cfg,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -145,7 +138,7 @@ impl GlobalData {
|
|||||||
let cfg = BotConfig::new(&self.args.cfg_path)?;
|
let cfg = BotConfig::new(&self.args.cfg_path)?;
|
||||||
|
|
||||||
self.cfg = cfg;
|
self.cfg = cfg;
|
||||||
self.bot_state = BotState::new(&self.cfg).await?;
|
self.bot_state = BotState::new(&self.cfg, &self.db).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
use crate::config::AlbumConfig;
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::imgur::get_album_images;
|
use crate::imgur::get_album_images;
|
||||||
|
use crate::models::album::AlbumConfig;
|
||||||
use crate::{command, group, GlobalData};
|
use crate::{command, group, GlobalData};
|
||||||
|
use j_db::model::JdbModel;
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
use serenity::framework::standard::{Args, CommandResult};
|
use serenity::framework::standard::{Args, CommandResult};
|
||||||
use serenity::model::channel::Message;
|
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 {
|
global_data
|
||||||
album_id,
|
.db
|
||||||
name: album_name.clone(),
|
.insert(AlbumConfig::new(&album_name, &album_id))?;
|
||||||
});
|
|
||||||
|
|
||||||
global_data
|
global_data
|
||||||
.bot_state
|
.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();
|
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||||
|
|
||||||
global_data
|
let album = AlbumConfig::get_album_by_name(&global_data.db, &album_name)?;
|
||||||
.cfg
|
|
||||||
.albums
|
|
||||||
.retain(|album| !album.name.eq_ignore_ascii_case(&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))
|
global_data.db.remove::<AlbumConfig>(album.id().unwrap())?;
|
||||||
.await?;
|
|
||||||
|
msg.reply(&ctx.http, format!("{} album removed!", album_name))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -105,10 +115,9 @@ async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
|
|||||||
let global_data = data.get::<GlobalData>().unwrap();
|
let global_data = data.get::<GlobalData>().unwrap();
|
||||||
|
|
||||||
let album_names: Vec<String> = global_data
|
let album_names: Vec<String> = global_data
|
||||||
.cfg
|
.db
|
||||||
.albums
|
.filter(|_, _: &AlbumConfig| true)?
|
||||||
.iter()
|
.map(|album| album.name)
|
||||||
.map(|album| album.name.clone())
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if album_names.is_empty() {
|
if album_names.is_empty() {
|
||||||
|
|||||||
@ -2,6 +2,8 @@ use crate::error::Error;
|
|||||||
use crate::models::insult_compliment::{RandomResponseTemplate, ResponseType};
|
use crate::models::insult_compliment::{RandomResponseTemplate, ResponseType};
|
||||||
use crate::models::random::RandomConfig;
|
use crate::models::random::RandomConfig;
|
||||||
use crate::{command, group, GlobalData, BAD_APPLE};
|
use crate::{command, group, GlobalData, BAD_APPLE};
|
||||||
|
use rand::prelude::SliceRandom;
|
||||||
|
use rand::thread_rng;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
@ -53,11 +55,11 @@ impl RandomCtx {
|
|||||||
pub fn new(user_name: &str, global_data: &GlobalData) -> Result<Self, Error> {
|
pub fn new(user_name: &str, global_data: &GlobalData) -> Result<Self, Error> {
|
||||||
let mut random_image: HashMap<String, String> = HashMap::new();
|
let mut random_image: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
for album in &global_data.cfg.albums {
|
for (album_name, images) in &global_data.bot_state.albums {
|
||||||
let image = global_data.bot_state.get_image(&album.name, Vec::new())?;
|
let image = images.choose(&mut thread_rng());
|
||||||
|
|
||||||
if let Some(image) = image {
|
if let Some(image) = image {
|
||||||
random_image.insert(album.name.clone(), image.link);
|
random_image.insert(album_name.clone(), image.link.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
41
src/models/album.rs
Normal file
41
src/models/album.rs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
use crate::error::Error;
|
||||||
|
use j_db::database::Database;
|
||||||
|
use j_db::model::JdbModel;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct AlbumConfig {
|
||||||
|
id: Option<u64>,
|
||||||
|
pub name: String,
|
||||||
|
pub album_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JdbModel for AlbumConfig {
|
||||||
|
fn id(&self) -> Option<u64> {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_id(&mut self, id: u64) {
|
||||||
|
self.id = Some(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tree() -> String {
|
||||||
|
"Album".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AlbumConfig {
|
||||||
|
pub fn new(name: &str, album_id: &str) -> AlbumConfig {
|
||||||
|
Self {
|
||||||
|
id: None,
|
||||||
|
name: name.to_string(),
|
||||||
|
album_id: album_id.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_album_by_name(db: &Database, name: &str) -> Result<Option<AlbumConfig>, Error> {
|
||||||
|
Ok(db
|
||||||
|
.filter(|_, album_config: &AlbumConfig| album_config.name.eq_ignore_ascii_case(name))?
|
||||||
|
.next())
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,2 +1,3 @@
|
|||||||
|
pub mod album;
|
||||||
pub mod insult_compliment;
|
pub mod insult_compliment;
|
||||||
pub mod random;
|
pub mod random;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user