mara/src/commands/furbooru.rs

58 lines
1.4 KiB
Rust

use furbooru::Client;
use robespierre::framework::standard::{macros::command, CommandResult, FwContext};
use robespierre::model::MessageExt;
use robespierre::UserData;
use robespierre_models::channel::Message;
use std::sync::Arc;
use tokio::sync::Mutex;
pub struct ClientKey;
impl robespierre::typemap::Key for ClientKey {
type Value = Arc<Mutex<Client>>;
}
pub fn make(username: String, api_key: String) -> Arc<Mutex<Client>> {
Arc::new(Mutex::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_impl(ctx: &FwContext, msg: &Message, args: &str) -> CommandResult {
let data = ctx.data_lock_read().await;
let client = data.get::<ClientKey>().unwrap().clone();
let client = client.lock().await;
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(())
}