|
| 1 | +use serenity::builder::CreateEmbed; |
| 2 | +use serenity::model::prelude::interaction::application_command::{ApplicationCommandInteraction, CommandDataOptionValue}; |
| 3 | +use serenity::prelude::Context; |
| 4 | +use serenity::utils::Colour; |
| 5 | + |
| 6 | +use super::{arg_opt, respond_err, respond_embed}; |
| 7 | + |
| 8 | +pub(super) async fn embed(ctx: &Context, interaction: &ApplicationCommandInteraction) { |
| 9 | + let title = arg_opt(interaction, "title"); |
| 10 | + let description = arg_opt(interaction, "description"); |
| 11 | + let color = arg_opt(interaction, "color"); |
| 12 | + let url = arg_opt(interaction, "url"); |
| 13 | + let footer_text = arg_opt(interaction, "footer"); |
| 14 | + let image = arg_opt(interaction, "image"); |
| 15 | + |
| 16 | + interaction.defer(ctx).await.expect("Failed to defer interaction"); |
| 17 | + |
| 18 | + let mut embed = CreateEmbed::default(); |
| 19 | + |
| 20 | + if let Some(CommandDataOptionValue::String(title)) = title { |
| 21 | + embed.title(title); |
| 22 | + } |
| 23 | + |
| 24 | + if let Some(CommandDataOptionValue::String(description)) = description { |
| 25 | + embed.description(description); |
| 26 | + } |
| 27 | + |
| 28 | + if let Some(CommandDataOptionValue::String(color)) = &color { |
| 29 | + match hex::decode(color.to_ascii_lowercase().replace("#", "")) { |
| 30 | + Ok(hex_arr) => { |
| 31 | + embed.color(Colour::from_rgb(hex_arr[0], hex_arr[1], hex_arr[2])); |
| 32 | + }, |
| 33 | + Err(e) => { |
| 34 | + let title = "Invalid color provided"; |
| 35 | + let content = &format!("The color '{}' is not a valid hexadecimal color: {}", &color, e); |
| 36 | + return respond_err(ctx, interaction, title, content).await |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if let Some(CommandDataOptionValue::String(url)) = url { |
| 42 | + match url.parse::<reqwest::Url>() { |
| 43 | + Ok(_) => { |
| 44 | + if embed.0.contains_key("title") { |
| 45 | + embed.url(url); |
| 46 | + } else { |
| 47 | + let title = "Invalid parameters"; |
| 48 | + let content = "A title is required for a url to function"; |
| 49 | + return respond_err(ctx, interaction, title, content).await |
| 50 | + } |
| 51 | + }, |
| 52 | + Err(e) => { |
| 53 | + let title = "Invalid url provided"; |
| 54 | + let content = &format!("The url '{}' is not a valid url: {}", url, e); |
| 55 | + return respond_err(ctx, interaction, title, content).await |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if let Some(CommandDataOptionValue::String(footer_text)) = footer_text { |
| 61 | + embed.footer(|f| f.text(footer_text)); |
| 62 | + } |
| 63 | + |
| 64 | + if let Some(CommandDataOptionValue::String(image)) = image { |
| 65 | + match image.parse::<reqwest::Url>() { |
| 66 | + Ok(_) => { |
| 67 | + embed.image(image); |
| 68 | + }, |
| 69 | + Err(e) => { |
| 70 | + let title = "Invalid image url provided"; |
| 71 | + let content = &format!("The image url '{}' is not a valid image url: {}", image, e); |
| 72 | + |
| 73 | + return respond_err(ctx, interaction, title, content).await |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if embed.0.contains_key("title") || embed.0.contains_key("description") || embed.0.contains_key("footer") { |
| 79 | + respond_embed(ctx, interaction, &embed, false).await |
| 80 | + } else { |
| 81 | + respond_err(ctx, interaction, "Failed to respond with embed", "Embed does not have any content").await |
| 82 | + } |
| 83 | +} |
0 commit comments