twilight_model/http/interaction.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
//! Models used when responding to interactions over HTTP.
#![allow(deprecated)]
use super::attachment::Attachment;
use crate::{
application::command::CommandOptionChoice,
channel::message::{AllowedMentions, Component, Embed, MessageFlags},
};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
/// Interaction response sent to Discord.
///
/// See [Discord Docs/Interaction Object].
///
/// [Discord Docs/Interaction Object]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-structure
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct InteractionResponse {
/// Type of the response.
#[serde(rename = "type")]
pub kind: InteractionResponseType,
/// Data of the response.
///
/// This is required if the type is any of the following:
/// - [`ChannelMessageWithSource`]
/// - [`UpdateMessage`]
/// - [`Modal`]
/// - [`ApplicationCommandAutocompleteResult`]
///
/// [`ApplicationCommandAutocompleteResult`]: InteractionResponseType::ApplicationCommandAutocompleteResult
/// [`ChannelMessageWithSource`]: InteractionResponseType::ChannelMessageWithSource
/// [`Modal`]: InteractionResponseType::Modal
/// [`UpdateMessage`]: InteractionResponseType::UpdateMessage
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<InteractionResponseData>,
}
/// Data included in an interaction response.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct InteractionResponseData {
/// Allowed mentions of the response.
#[serde(skip_serializing_if = "Option::is_none")]
pub allowed_mentions: Option<AllowedMentions>,
/// List of attachments on the response.
#[serde(skip_serializing_if = "Option::is_none")]
pub attachments: Option<Vec<Attachment>>,
/// List of autocomplete alternatives.
///
/// Can only be used with
/// [`InteractionResponseType::ApplicationCommandAutocompleteResult`].
#[serde(skip_serializing_if = "Option::is_none")]
pub choices: Option<Vec<CommandOptionChoice>>,
/// List of components on the response.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub components: Option<Vec<Component>>,
/// Content of the response.
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// For [`InteractionResponseType::Modal`], user defined identifier.
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_id: Option<String>,
/// Embeds of the response.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub embeds: Option<Vec<Embed>>,
/// Interaction response data flags.
///
/// The supported flags are [`MessageFlags::SUPPRESS_EMBEDS`] and
/// [`MessageFlags::EPHEMERAL`].
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<MessageFlags>,
/// For [`InteractionResponseType::Modal`], title of the modal.
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
/// Whether the response is TTS.
#[serde(skip_serializing_if = "Option::is_none")]
pub tts: Option<bool>,
}
/// Type of interaction response.
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
#[non_exhaustive]
#[repr(u8)]
pub enum InteractionResponseType {
/// Used when responding to a Ping from Discord.
Pong = 1,
/// Responds to an interaction with a message.
ChannelMessageWithSource = 4,
/// Acknowledges an interaction, showing a loading state, and allowing for
/// the message to be edited later.
DeferredChannelMessageWithSource = 5,
/// Acknowledges a component interaction, allowing for the message to be
/// edited later.
///
/// This is only valid for components and modal submits.
DeferredUpdateMessage = 6,
/// Acknowledges a component interaction and edits the message.
///
/// This is only valid for components and modal submits.
UpdateMessage = 7,
/// Respond to an autocomplete interaction with suggested choices.
ApplicationCommandAutocompleteResult = 8,
/// Respond to an interaction with a popup modal.
Modal = 9,
/// Respond to an interaction with an upgrade button, only available
/// for apps with monetization enabled
///
/// Deprecated: Please send a [`InteractionResponseType::ChannelMessageWithSource`]
/// with an [`Button`](crate::channel::message::component::Button) with the style [`ButtonStyle::Premium`](crate::channel::message::component::ButtonStyle)
/// instead.
#[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,
}),
};
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,
],
);
}
}