From 95192bd0b9b5770e9c33e70045af8f63e482ba7b Mon Sep 17 00:00:00 2001 From: "oupson1er@gmail.com" Date: Tue, 23 Mar 2021 09:28:01 +0100 Subject: [PATCH] Work on interactions --- src/commands/interaction.rs | 191 +++++++++++++++++++++++------------- 1 file changed, 125 insertions(+), 66 deletions(-) diff --git a/src/commands/interaction.rs b/src/commands/interaction.rs index 4e06c8b..4f9e1d6 100644 --- a/src/commands/interaction.rs +++ b/src/commands/interaction.rs @@ -1,6 +1,7 @@ use serde::Serialize; use serde_json::Value; use serenity::{ + builder::CreateEmbed, client::Context, framework::standard::CommandResult, model::{channel::Embed, guild::Member, Permissions}, @@ -62,27 +63,13 @@ pub(crate) async fn handle_interaction(ctx: &Context, object: Value) -> CommandR if permissions.administrator() { goulag(ctx, data, guild_id, &author_member, id, token).await? } else { - let _r = reqwest::Client::new() - .post(format!( - "https://discord.com/api/v8/interactions/{}/{}/callback", - id, token - )) - .json(&Response { - response_type: 4, - data: ResponseData { - tts: false, - embeds: Some(vec![Embed::fake(|e| { - super::embed_author(e, Some(&author_member)) - .title("Error") - .description("You don't have the right to do that") - .colour((247, 76, 0)) - })]), - content: None, - flags: 0, - }, - }) - .send() - .await?; + Response::new_with_embed(4, |e| { + super::embed_author(e, Some(&author_member)) + .title("Error") + .description("You don't have the right to do that") + .colour((247, 76, 0)) + }).send(token, id) + .await?; } } _ => (), @@ -139,68 +126,140 @@ async fn goulag( .await?; member.add_role(&ctx.http, mute_role).await?; - let _r = reqwest::Client::new() - .post(format!( - "https://discord.com/api/v8/interactions/{}/{}/callback", - id, token - )) - .json(&Response { - response_type: 4, - data: ResponseData { - tts: false, - embeds: Some(vec![Embed::fake(|e| { - super::embed_author(e, Some(author)) - .title("Mute") - .description(format!("{} was muted", member.display_name())) - .colour((247, 76, 0)) - })]), - content: None, - flags: 0, - }, - }) - .send() - .await?; + + Response::new_with_embed(5, |e| { + super::embed_author(e, Some(author)) + .title("Mute") + .description(format!("{} was muted", member.display_name())) + .colour((247, 76, 0)) + }) + .send(token, id) + .await?; return Ok(()); } } - let _r = reqwest::Client::new() - .post(format!( - "https://discord.com/api/v8/interactions/{}/{}/callback", - id, token - )) - .json(&Response { - response_type: 4, - data: ResponseData { - tts: false, - embeds: Some(vec![Embed::fake(|e| { - super::embed_author(e, Some(author)) - .title("Error") - .description("Unknown mute role") - .colour((247, 76, 0)) - })]), - content: None, - flags: 0, - }, - }) - .send() - .await?; + Response::new_with_embed(4, |e| { + super::embed_author(e, Some(author)) + .title("Error") + .description("Unknown mute role") + .colour((247, 76, 0)) + }) + .send(token, id) + .await?; Ok(()) } #[derive(Debug, Serialize)] -struct ResponseData { +pub(crate) struct ResponseData { tts: bool, content: Option, embeds: Option>, flags: u64, } +impl ResponseData { + pub(crate) fn embed(mut self, f: F) -> Self + where + F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed, + { + let embeds = self.embeds.get_or_insert(Default::default()); + + embeds.push(Embed::fake(f)); + + self + } + + /// Get a reference to the response data's tts. + pub(crate) fn tts(&self) -> &bool { + &self.tts + } + + /// Get a reference to the response data's content. + pub(crate) fn content(&self) -> &Option { + &self.content + } + + /// Get a reference to the response data's flags. + pub(crate) fn flags(&self) -> &u64 { + &self.flags + } + + /// Set the response data's content. + pub(crate) fn set_content(mut self, content: Option) -> Self { + self.content = content; + self + } + + /// Set the response data's tts. + pub(crate) fn set_tts(mut self, tts: bool) -> Self { + self.tts = tts; + self + } + + /// Set the response data's flags. + pub(crate) fn set_flags(mut self, flags: u64) -> Self { + self.flags = flags; + self + } + + /// Get a reference to the response data's embeds. + pub(crate) fn embeds(&self) -> &Option> { + &self.embeds + } + + /// Get a mutable reference to the response data's embeds. + pub(crate) fn embeds_mut(&mut self) -> &mut Option> { + &mut self.embeds + } + + /// Set the response data's embeds. + pub(crate) fn set_embeds(mut self, embeds: Option>) -> Self { + self.embeds = embeds; + self + } +} + +impl Default for ResponseData { + fn default() -> Self { + Self { + tts: false, + content: None, + embeds: None, + flags: 0, + } + } +} + #[derive(Debug, Serialize)] -struct Response { +pub(crate) struct Response { #[serde(rename(serialize = "type"))] response_type: u64, - data: ResponseData, + data: Option, +} + +impl Response { + pub(crate) fn new_with_embed(response_type: u64, f: F) -> Self + where + F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed, + { + Response { + response_type, + data: Some(ResponseData::default().embed(f)), + } + } + + pub(crate) async fn send(&self, token: &str, id: &str) -> CommandResult { + reqwest::Client::new() + .post(format!( + "https://discord.com/api/v8/interactions/{}/{}/callback", + id, token + )) + .json(self) + .send() + .await?; + Ok(()) + } }