mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-20 01:08:55 +00:00
Core: asyncify images
This commit is contained in:
parent
284b406dec
commit
d607a0d701
1 changed files with 23 additions and 14 deletions
|
@ -15,24 +15,24 @@ use youmubot_prelude::*;
|
||||||
#[description = "🖼️ Find an image with a given tag on Danbooru[nsfw]!"]
|
#[description = "🖼️ Find an image with a given tag on Danbooru[nsfw]!"]
|
||||||
#[min_args(1)]
|
#[min_args(1)]
|
||||||
#[bucket("images")]
|
#[bucket("images")]
|
||||||
pub fn nsfw(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
|
pub async fn nsfw(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||||
message_command(ctx, msg, args, Rating::Explicit)
|
message_command(ctx, msg, args, Rating::Explicit).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[command]
|
#[command]
|
||||||
#[description = "🖼️ Find an image with a given tag on Danbooru[safe]!"]
|
#[description = "🖼️ Find an image with a given tag on Danbooru[safe]!"]
|
||||||
#[min_args(1)]
|
#[min_args(1)]
|
||||||
#[bucket("images")]
|
#[bucket("images")]
|
||||||
pub fn image(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
|
pub async fn image(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||||
message_command(ctx, msg, args, Rating::Safe)
|
message_command(ctx, msg, args, Rating::Safe).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[check]
|
#[check]
|
||||||
#[name = "nsfw"]
|
#[name = "nsfw"]
|
||||||
fn nsfw_check(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
|
async fn nsfw_check(ctx: &Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
|
||||||
let channel = msg.channel_id.to_channel(&ctx).unwrap();
|
let channel = msg.channel_id.to_channel(&ctx).await.unwrap();
|
||||||
if !(match channel {
|
if !(match channel {
|
||||||
Channel::Guild(guild_channel) => guild_channel.read().nsfw,
|
Channel::Guild(guild_channel) => guild_channel.nsfw,
|
||||||
_ => true,
|
_ => true,
|
||||||
}) {
|
}) {
|
||||||
CheckResult::Failure(Reason::User("😣 YOU FREAKING PERVERT!!!".to_owned()))
|
CheckResult::Failure(Reason::User("😣 YOU FREAKING PERVERT!!!".to_owned()))
|
||||||
|
@ -41,22 +41,31 @@ fn nsfw_check(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn message_command(ctx: &mut Context, msg: &Message, args: Args, rating: Rating) -> CommandResult {
|
async fn message_command(
|
||||||
|
ctx: &Context,
|
||||||
|
msg: &Message,
|
||||||
|
args: Args,
|
||||||
|
rating: Rating,
|
||||||
|
) -> CommandResult {
|
||||||
let tags = args.remains().unwrap_or("touhou");
|
let tags = args.remains().unwrap_or("touhou");
|
||||||
let http = ctx.data.get_cloned::<HTTPClient>();
|
let image = get_image(
|
||||||
let image = get_image(&http, rating, tags)?;
|
ctx.data.read().await.get::<HTTPClient>().unwrap(),
|
||||||
|
rating,
|
||||||
|
tags,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
match image {
|
match image {
|
||||||
None => msg.reply(&ctx, "🖼️ No image found...\n💡 Tip: In danbooru, character names follow Japanese standards (last name before first name), so **Hakurei Reimu** might give you an image while **Reimu Hakurei** won't."),
|
None => msg.reply(&ctx, "🖼️ No image found...\n💡 Tip: In danbooru, character names follow Japanese standards (last name before first name), so **Hakurei Reimu** might give you an image while **Reimu Hakurei** won't.").await,
|
||||||
Some(url) => msg.reply(
|
Some(url) => msg.reply(
|
||||||
&ctx,
|
&ctx,
|
||||||
format!("🖼️ Here's the image you requested!\n\n{}", url),
|
format!("🖼️ Here's the image you requested!\n\n{}", url),
|
||||||
),
|
).await,
|
||||||
}?;
|
}?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets an image URL.
|
// Gets an image URL.
|
||||||
fn get_image(
|
async fn get_image(
|
||||||
client: &<HTTPClient as TypeMapKey>::Value,
|
client: &<HTTPClient as TypeMapKey>::Value,
|
||||||
rating: Rating,
|
rating: Rating,
|
||||||
tags: &str,
|
tags: &str,
|
||||||
|
@ -72,7 +81,7 @@ fn get_image(
|
||||||
.query(&[("limit", "1"), ("random", "true")])
|
.query(&[("limit", "1"), ("random", "true")])
|
||||||
.build()?;
|
.build()?;
|
||||||
println!("{:?}", req.url());
|
println!("{:?}", req.url());
|
||||||
let response: Vec<PostResponse> = client.execute(req)?.json()?;
|
let response: Vec<PostResponse> = client.execute(req).await?.json().await?;
|
||||||
Ok(response
|
Ok(response
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
|
|
Loading…
Add table
Reference in a new issue