Added album tags
+ clippy+fmt
This commit is contained in:
parent
79f023a225
commit
13bacac0c7
@ -1,5 +1,8 @@
|
||||
use crate::config::AlbumConfig;
|
||||
use crate::{command, group, GlobalData};
|
||||
use crate::error::Error;
|
||||
use crate::imgur::Image;
|
||||
use crate::{command, group, imgur, GlobalData};
|
||||
use rand::prelude::SliceRandom;
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{Args, CommandResult};
|
||||
use serenity::model::channel::Message;
|
||||
@ -112,3 +115,69 @@ async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn parse_album(
|
||||
ctx: &Context,
|
||||
msg: &Message,
|
||||
album_name: &str,
|
||||
tags: Vec<&str>,
|
||||
) -> Result<(), Error> {
|
||||
let data = ctx.data.read().await;
|
||||
let global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let album = global_data
|
||||
.cfg
|
||||
.albums
|
||||
.iter()
|
||||
.find(|album| album.name.to_lowercase() == album_name);
|
||||
|
||||
if let Some(album) = album {
|
||||
match get_image(album, global_data, tags).await {
|
||||
Ok(image) => {
|
||||
if let Some(image) = image {
|
||||
msg.reply(&ctx.http, &image.link).await?;
|
||||
} else {
|
||||
msg.reply(&ctx.http, "No image found ;(").await?;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
msg.reply(&ctx.http, "Unable to get album, try again later.")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_image(
|
||||
album_config: &AlbumConfig,
|
||||
global_data: &GlobalData,
|
||||
tags: Vec<&str>,
|
||||
) -> Result<Option<Image>, Error> {
|
||||
let album =
|
||||
imgur::get_album_images(&global_data.cfg.imgur_client_id, &album_config.album_id).await?;
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
let album = if tags.is_empty() {
|
||||
album
|
||||
} else {
|
||||
album
|
||||
.iter()
|
||||
.filter(|img| {
|
||||
for tag in &tags {
|
||||
if let Some(desc) = &img.description {
|
||||
if desc.to_lowercase().contains(&tag.to_lowercase()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
})
|
||||
.cloned()
|
||||
.collect()
|
||||
};
|
||||
|
||||
Ok(album.choose(&mut rng).cloned())
|
||||
}
|
||||
|
||||
@ -3,9 +3,7 @@ pub mod celeryman;
|
||||
pub mod color;
|
||||
pub mod joke;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::{help, hook, imgur, GlobalData};
|
||||
use rand::prelude::SliceRandom;
|
||||
use crate::{help, hook, GlobalData};
|
||||
use serenity::async_trait;
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{
|
||||
@ -71,48 +69,21 @@ pub async fn after(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn parse_album(ctx: &Context, msg: &Message, album_name: &str) -> Result<(), Error> {
|
||||
let data = ctx.data.read().await;
|
||||
let global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let album = global_data
|
||||
.cfg
|
||||
.albums
|
||||
.iter()
|
||||
.find(|album| album.name.to_lowercase() == album_name);
|
||||
|
||||
if let Some(album) = album {
|
||||
match imgur::get_album_images(&global_data.cfg.imgur_client_id, &album.album_id).await {
|
||||
Ok(album) => {
|
||||
let image = {
|
||||
let mut rng = rand::thread_rng();
|
||||
album.choose(&mut rng)
|
||||
};
|
||||
|
||||
if let Some(image) = image {
|
||||
msg.reply(&ctx.http, &image.link).await?;
|
||||
} else {
|
||||
msg.reply(&ctx.http, "Album empty, try and add some images.")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
msg.reply(&ctx.http, "Unable to get album, try again later.")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[hook]
|
||||
pub async fn unrecognised_command_hook(
|
||||
ctx: &Context,
|
||||
msg: &Message,
|
||||
unrecognised_command_name: &str,
|
||||
) {
|
||||
if let Err(e) = parse_album(ctx, msg, unrecognised_command_name).await {
|
||||
let contents_split: Vec<&str> = msg.content.split(' ').collect();
|
||||
|
||||
let tags = if contents_split.len() > 1 {
|
||||
contents_split[1..].to_vec()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
if let Err(e) = album::parse_album(ctx, msg, unrecognised_command_name, tags).await {
|
||||
println!("Error processing album command: {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user