Work on interactions

This commit is contained in:
oupson1er@gmail.com 2021-03-23 09:28:01 +01:00
parent ccd9a07e37
commit 95192bd0b9
1 changed files with 125 additions and 66 deletions

View File

@ -1,6 +1,7 @@
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use serenity::{ use serenity::{
builder::CreateEmbed,
client::Context, client::Context,
framework::standard::CommandResult, framework::standard::CommandResult,
model::{channel::Embed, guild::Member, Permissions}, model::{channel::Embed, guild::Member, Permissions},
@ -62,27 +63,13 @@ pub(crate) async fn handle_interaction(ctx: &Context, object: Value) -> CommandR
if permissions.administrator() { if permissions.administrator() {
goulag(ctx, data, guild_id, &author_member, id, token).await? goulag(ctx, data, guild_id, &author_member, id, token).await?
} else { } else {
let _r = reqwest::Client::new() Response::new_with_embed(4, |e| {
.post(format!( super::embed_author(e, Some(&author_member))
"https://discord.com/api/v8/interactions/{}/{}/callback", .title("Error")
id, token .description("You don't have the right to do that")
)) .colour((247, 76, 0))
.json(&Response { }).send(token, id)
response_type: 4, .await?;
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?;
} }
} }
_ => (), _ => (),
@ -139,68 +126,140 @@ async fn goulag(
.await?; .await?;
member.add_role(&ctx.http, mute_role).await?; member.add_role(&ctx.http, mute_role).await?;
let _r = reqwest::Client::new()
.post(format!( Response::new_with_embed(5, |e| {
"https://discord.com/api/v8/interactions/{}/{}/callback", super::embed_author(e, Some(author))
id, token .title("Mute")
)) .description(format!("{} was muted", member.display_name()))
.json(&Response { .colour((247, 76, 0))
response_type: 4, })
data: ResponseData { .send(token, id)
tts: false, .await?;
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?;
return Ok(()); return Ok(());
} }
} }
let _r = reqwest::Client::new() Response::new_with_embed(4, |e| {
.post(format!( super::embed_author(e, Some(author))
"https://discord.com/api/v8/interactions/{}/{}/callback", .title("Error")
id, token .description("Unknown mute role")
)) .colour((247, 76, 0))
.json(&Response { })
response_type: 4, .send(token, id)
data: ResponseData { .await?;
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?;
Ok(()) Ok(())
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct ResponseData { pub(crate) struct ResponseData {
tts: bool, tts: bool,
content: Option<String>, content: Option<String>,
embeds: Option<Vec<Value>>, embeds: Option<Vec<Value>>,
flags: u64, flags: u64,
} }
impl ResponseData {
pub(crate) fn embed<F>(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<String> {
&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<String>) -> 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<Vec<Value>> {
&self.embeds
}
/// Get a mutable reference to the response data's embeds.
pub(crate) fn embeds_mut(&mut self) -> &mut Option<Vec<Value>> {
&mut self.embeds
}
/// Set the response data's embeds.
pub(crate) fn set_embeds(mut self, embeds: Option<Vec<Value>>) -> Self {
self.embeds = embeds;
self
}
}
impl Default for ResponseData {
fn default() -> Self {
Self {
tts: false,
content: None,
embeds: None,
flags: 0,
}
}
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct Response { pub(crate) struct Response {
#[serde(rename(serialize = "type"))] #[serde(rename(serialize = "type"))]
response_type: u64, response_type: u64,
data: ResponseData, data: Option<ResponseData>,
}
impl Response {
pub(crate) fn new_with_embed<F>(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(())
}
} }