zaun_community_protocol/command/object/
interaction.rs1mod body;
4
5pub use body::{MessageBody, flags};
6
7use std::collections::BTreeMap;
8
9use serde::{Deserialize, Serialize};
10
11use super::{Attachment, Channel, CommandType, Member, Message, OptionType, Role};
12use crate::bits::Permissions;
13use crate::ids::{
14 AttachmentId, BotId, ChannelId, CommandId, InteractionId, MemberId, MessageId, RoleId,
15};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
19#[serde(try_from = "u8", into = "u8")]
20pub enum InteractionType {
21 ApplicationCommand,
23 MessageComponent,
25 Autocomplete,
27 ModalSubmit,
29}
30
31impl TryFrom<u8> for InteractionType {
32 type Error = String;
33 fn try_from(v: u8) -> Result<Self, String> {
34 match v {
35 2 => Ok(Self::ApplicationCommand),
36 3 => Ok(Self::MessageComponent),
37 4 => Ok(Self::Autocomplete),
38 5 => Ok(Self::ModalSubmit),
39 _ => Err(format!("unsupported interaction type {v}")),
40 }
41 }
42}
43
44impl From<InteractionType> for u8 {
45 fn from(t: InteractionType) -> Self {
46 t as Self + 2
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum OptionValueKind {
54 Bool(bool),
56 Number(f64),
58 String(String),
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct OptionValue {
65 pub name: String,
67 #[serde(rename = "type")]
69 pub kind: OptionType,
70 #[serde(default, skip_serializing_if = "Option::is_none")]
72 pub value: Option<OptionValueKind>,
73 #[serde(default, skip_serializing_if = "Vec::is_empty")]
75 pub options: Vec<OptionValue>,
76 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
78 pub focused: bool,
79}
80
81#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83pub struct SubmittedInput {
84 #[serde(default, skip_serializing_if = "Option::is_none")]
86 pub id: Option<u32>,
87 pub custom_id: String,
89 #[serde(default, skip_serializing_if = "Option::is_none")]
91 pub value: Option<String>,
92 #[serde(default, skip_serializing_if = "Vec::is_empty")]
94 pub values: Vec<String>,
95}
96
97#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
99pub struct Resolved {
100 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
102 pub members: BTreeMap<MemberId, Member>,
103 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
105 pub roles: BTreeMap<RoleId, Role>,
106 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
108 pub channels: BTreeMap<ChannelId, Channel>,
109 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
111 pub messages: BTreeMap<MessageId, Message>,
112 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
114 pub attachments: BTreeMap<AttachmentId, Attachment>,
115}
116
117impl Resolved {
118 #[must_use]
120 pub fn is_empty(&self) -> bool {
121 *self == Self::default()
122 }
123}
124
125#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
127#[serde(untagged)]
128pub enum InteractionData {
129 Command {
131 command_id: CommandId,
133 name: String,
135 #[serde(rename = "type")]
137 kind: CommandType,
138 #[serde(default, skip_serializing_if = "Vec::is_empty")]
140 options: Vec<OptionValue>,
141 #[serde(default, skip_serializing_if = "Resolved::is_empty")]
143 resolved: Resolved,
144 #[serde(default, skip_serializing_if = "Option::is_none")]
146 target_id: Option<String>,
147 },
148 Component {
150 custom_id: String,
152 component_type: u8,
154 #[serde(default, skip_serializing_if = "Vec::is_empty")]
156 values: Vec<String>,
157 #[serde(default, skip_serializing_if = "Resolved::is_empty")]
159 resolved: Resolved,
160 },
161 Modal {
163 custom_id: String,
165 components: Vec<SubmittedInput>,
167 #[serde(default, skip_serializing_if = "Resolved::is_empty")]
169 resolved: Resolved,
170 },
171}
172
173#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
175pub struct Interaction {
176 pub interaction_id: InteractionId,
178 #[serde(rename = "type")]
180 pub kind: InteractionType,
181 pub bot_id: BotId,
183 pub channel_id: ChannelId,
185 pub member: Member,
187 pub app_permissions: Permissions,
189 #[serde(default, skip_serializing_if = "Option::is_none")]
191 pub locale: Option<String>,
192 #[serde(default, skip_serializing_if = "Option::is_none")]
194 pub message: Option<Box<Message>>,
195 pub data: InteractionData,
197 pub created_at: u64,
199}
200
201#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
203pub struct InteractionMetadata {
204 pub interaction_id: InteractionId,
206 #[serde(rename = "type")]
208 pub kind: InteractionType,
209 #[serde(default, skip_serializing_if = "Option::is_none")]
211 pub name: Option<String>,
212 pub member_id: MemberId,
214}