#![allow(deprecated)]
use super::attachment::Attachment;
use crate::{
application::command::CommandOptionChoice,
channel::message::{AllowedMentions, Component, Embed, MessageFlags},
poll::Poll,
};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct InteractionResponse {
#[serde(rename = "type")]
pub kind: InteractionResponseType,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<InteractionResponseData>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct InteractionResponseData {
#[serde(skip_serializing_if = "Option::is_none")]
pub allowed_mentions: Option<AllowedMentions>,
#[serde(skip_serializing_if = "Option::is_none")]
pub attachments: Option<Vec<Attachment>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub choices: Option<Vec<CommandOptionChoice>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub components: Option<Vec<Component>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub embeds: Option<Vec<Embed>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<MessageFlags>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tts: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub poll: Option<Poll>,
}
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
#[non_exhaustive]
#[repr(u8)]
pub enum InteractionResponseType {
Pong = 1,
ChannelMessageWithSource = 4,
DeferredChannelMessageWithSource = 5,
DeferredUpdateMessage = 6,
UpdateMessage = 7,
ApplicationCommandAutocompleteResult = 8,
Modal = 9,
#[deprecated(note = "Deprecated by Discord in favor of Premium Buttons")]
PremiumRequired = 10,
}
#[cfg(test)]
mod tests {
use crate::{
channel::message::MessageFlags,
http::{
attachment::Attachment,
interaction::{InteractionResponse, InteractionResponseData, InteractionResponseType},
},
};
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::{assert_fields, assert_impl_all};
use std::fmt::Debug;
assert_fields!(
InteractionResponseData: allowed_mentions,
choices,
components,
content,
embeds,
flags,
tts
);
assert_impl_all!(
InteractionResponseData: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync
);
#[test]
fn interaction_response() {
let value = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(InteractionResponseData {
allowed_mentions: None,
attachments: None,
choices: None,
components: None,
content: Some("test".into()),
custom_id: None,
embeds: None,
flags: Some(MessageFlags::EPHEMERAL),
title: None,
tts: None,
poll: None,
}),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "InteractionResponse",
len: 2,
},
Token::Str("type"),
Token::U8(4),
Token::Str("data"),
Token::Some,
Token::Struct {
name: "InteractionResponseData",
len: 2,
},
Token::Str("content"),
Token::Some,
Token::Str("test"),
Token::Str("flags"),
Token::Some,
Token::U64(64),
Token::StructEnd,
Token::StructEnd,
],
);
}
#[test]
fn interaction_response_with_attachments() {
let value = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(InteractionResponseData {
attachments: Some(Vec::from([Attachment {
description: None,
file: "file data".into(),
filename: "filename.jpg".into(),
id: 1,
}])),
..InteractionResponseData::default()
}),
};
serde_test::assert_ser_tokens(
&value,
&[
Token::Struct {
name: "InteractionResponse",
len: 2,
},
Token::Str("type"),
Token::U8(InteractionResponseType::ChannelMessageWithSource as u8),
Token::Str("data"),
Token::Some,
Token::Struct {
name: "InteractionResponseData",
len: 1,
},
Token::Str("attachments"),
Token::Some,
Token::Seq { len: Some(1) },
Token::Struct {
name: "Attachment",
len: 2,
},
Token::Str("filename"),
Token::Str("filename.jpg"),
Token::Str("id"),
Token::U64(1),
Token::StructEnd,
Token::SeqEnd,
Token::StructEnd,
Token::StructEnd,
],
);
}
}