FrenBot/src/discord/album.rs

78 lines
2.1 KiB
Rust

use crate::album_manager::AlbumQuery;
use crate::config::GlobalData;
use crate::discord::Context;
use crate::error::Error;
use poise::serenity_prelude::{Attachment, MessageBuilder};
#[poise::command(prefix_command, category = "Albums")]
pub async fn add_image(
ctx: Context<'_>,
#[description = "Album to add image to"] album_name: String,
#[description = "Image tags"] tags: Vec<String>,
#[description = "Image to upload"] images: Vec<Attachment>,
) -> Result<(), Error> {
for attachment in &images {
let data = attachment.download().await?;
ctx.data()
.picox
.add_image(&album_name, tags.clone(), &attachment.filename, data)
.await?;
}
let plural = if images.len() > 1 { "s" } else { "" };
ctx.reply(format!("Image{} added to {}!", plural, album_name))
.await?;
Ok(())
}
#[poise::command(prefix_command, category = "Albums")]
pub async fn list_albums(ctx: Context<'_>) -> Result<(), Error> {
let albums = ctx
.data()
.picox
.query_album(AlbumQuery { album_name: None })
.await?;
let album_names: Vec<String> = albums.iter().map(|a| a.album_name.clone()).collect();
if album_names.is_empty() {
ctx.reply("There are no albums in picox!").await?;
} else {
let mut message_builder = MessageBuilder::new();
message_builder.push_bold_line("Albums:");
for album in &album_names {
message_builder.push_line(format!("* {}", album));
}
ctx.reply(message_builder.build()).await?;
}
Ok(())
}
pub async fn parse_album(
ctx: &poise::serenity_prelude::Context,
msg: &poise::serenity_prelude::Message,
data: &GlobalData,
album_name: &str,
tags: Vec<&str>,
) -> Result<bool, Error> {
let img = match data.picox.get_random_image(album_name, tags).await {
Ok(img) => img,
Err(_) => return Ok(false),
};
if let Some(img) = img {
msg.reply(&ctx.http, img.link).await?;
} else {
msg.reply(&ctx.http, "No image found :(").await?;
}
Ok(true)
}