Work on interactions
This commit is contained in:
parent
ccd9a07e37
commit
95192bd0b9
|
@ -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,26 +63,12 @@ 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| {
|
||||
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))
|
||||
})]),
|
||||
content: None,
|
||||
flags: 0,
|
||||
},
|
||||
})
|
||||
.send()
|
||||
}).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| {
|
||||
|
||||
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))
|
||||
})]),
|
||||
content: None,
|
||||
flags: 0,
|
||||
},
|
||||
})
|
||||
.send()
|
||||
.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| {
|
||||
Response::new_with_embed(4, |e| {
|
||||
super::embed_author(e, Some(author))
|
||||
.title("Error")
|
||||
.description("Unknown mute role")
|
||||
.colour((247, 76, 0))
|
||||
})]),
|
||||
content: None,
|
||||
flags: 0,
|
||||
},
|
||||
})
|
||||
.send()
|
||||
.send(token, id)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct ResponseData {
|
||||
pub(crate) struct ResponseData {
|
||||
tts: bool,
|
||||
content: Option<String>,
|
||||
embeds: Option<Vec<Value>>,
|
||||
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)]
|
||||
struct Response {
|
||||
pub(crate) struct Response {
|
||||
#[serde(rename(serialize = "type"))]
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue