Skip to content

Commit

Permalink
feat(shiggy): add back metadata but allow raw
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Oct 22, 2023
1 parent f40d0e6 commit 07730be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
32 changes: 28 additions & 4 deletions src/commands/shiggy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,47 @@ use poise::{serenity_prelude as serenity, CreateReply};

#[derive(serde::Deserialize)]
struct SafebooruResponse {
file_url: reqwest::Url,
id: i32,
source: String,
tag_string: String,
file_url: String,
}

/// Fetch a random shiggy
#[poise::command(slash_command)]
pub async fn shiggy(ctx: Context<'_>) -> Result<()> {
pub async fn shiggy(
ctx: Context<'_>,

#[description = "Whether to send the image URL only"]
#[flag]
raw: bool,
) -> Result<()> {
ctx.defer().await?;
let mut url = "https://safebooru.donmai.us/posts/random.json".parse::<reqwest::Url>()?;
url.query_pairs_mut()
.append_pair("tags", "kemomimi-chan_(naga_u) naga_u")
.append_pair("only", "file_url");
.append_pair("only", "id,source,tag_string,file_url");

let resp = crate::reqwest_client::HTTP.get(url).send().await?;

if resp.status().is_success() {
let data: SafebooruResponse = resp.json().await?;
ctx.say(data.file_url).await?;
if !raw {
ctx.send(
CreateReply::new().embed(
serenity::CreateEmbed::new()
.title(data.id.to_string())
.field("Tags", data.tag_string.replace("_", "\\_"), false)
.field("Source", data.source, false)
.url(format!("https://safebooru.donmai.us/posts/{}", data.id))
.image(data.file_url)
.color(0xfef9c3),
),
)
.await?;
} else {
ctx.say(data.file_url).await?;
}
} else {
ctx.send(
CreateReply::new().embed(
Expand Down
38 changes: 36 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::{Error, Result};
use owo_colors::OwoColorize;

use poise::{
serenity_prelude::{Client, FullEvent, GatewayIntents},
Framework, FrameworkOptions,
serenity_prelude::{Client, CreateEmbed, FullEvent, GatewayIntents},
CreateReply, Framework, FrameworkError, FrameworkOptions,
};

use crate::utils::Pluralize;
Expand Down Expand Up @@ -50,6 +50,40 @@ async fn main() -> Result<()> {
Ok(())
})
},
on_error: |err| {
Box::pin(async move {
match err {
FrameworkError::Setup { error, .. } => eprintln!("{}", error),
FrameworkError::Command { error, ctx, .. } => {
eprintln!(
"Encountered error handling command {}: {}",
ctx.invoked_command_name(),
error
);

ctx.send(
CreateReply::new().embed(
CreateEmbed::new()
.title("An error occurred!")
.description(format!("```\n{}\n```", error)),
),
)
.await
.ok();
}
FrameworkError::EventHandler { error, .. } => {
eprintln!("{}", error);
}
FrameworkError::CommandPanic {
payload: Some(payload),
..
} => {
eprintln!("{}", payload);
}
_ => {}
}
})
},
..Default::default()
},
|ctx, ready, framework| {
Expand Down

0 comments on commit 07730be

Please sign in to comment.