mara/src/commands/furbooru.rs

57 lines
1.4 KiB
Rust

use furbooru::Client;
use robespierre::framework::standard::{
extractors::RawArgs, macros::command, CommandResult, FwContext,
};
use robespierre::model::MessageExt;
use robespierre::UserData;
use robespierre_models::channel::Message;
use std::sync::Arc;
pub struct ClientKey;
impl robespierre::typemap::Key for ClientKey {
type Value = Arc<Client>;
}
pub fn make(username: String, api_key: String) -> Arc<Client> {
Arc::new(Client::new(user_agent(username), api_key).unwrap())
}
pub fn user_agent(username: String) -> String {
format!(
"{}/{} (owner: {})",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
username,
)
}
#[command]
pub async fn search(ctx: &FwContext, msg: &Message, args: RawArgs) -> CommandResult {
let data = ctx.data_lock_read().await;
let client = data.get::<ClientKey>().unwrap().clone();
let args = format!("{}", args.0);
let mut imgs = client.image_search(args, 0_u64).await?;
let mut response = String::new();
imgs.truncate(3);
for mut post in imgs.into_iter() {
post.tags.sort();
post.tags.truncate(8);
response.push_str(&format!(
"<{}> - {} - <{}>\n",
format!("http://furbooru.org/{}", post.id),
post.tags.join(", "),
post.view_url,
));
}
msg.reply(ctx, response).await?;
Ok(())
}