Skip to main content

zaun_community_protocol/command/object/
interaction.rs

1//! Interactions as delivered to bots, and their parts (specification ยง8.14).
2
3mod 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/// `type` of an interaction (Discord's numbers; `PING` is not used).
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
19#[serde(try_from = "u8", into = "u8")]
20pub enum InteractionType {
21    /// 2: a slash, user or message command.
22    ApplicationCommand,
23    /// 3: a button or select.
24    MessageComponent,
25    /// 4: partial input of an autocomplete option.
26    Autocomplete,
27    /// 5: a submitted modal.
28    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/// An option value as the invoker gave it: a string, number, boolean or id.
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum OptionValueKind {
54    /// Booleans.
55    Bool(bool),
56    /// Integers and numbers.
57    Number(f64),
58    /// Strings and ids (base64url).
59    String(String),
60}
61
62/// One option of an invoked command.
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct OptionValue {
65    /// The option's name.
66    pub name: String,
67    /// The option's type.
68    #[serde(rename = "type")]
69    pub kind: OptionType,
70    /// Absent for subcommands and groups.
71    #[serde(default, skip_serializing_if = "Option::is_none")]
72    pub value: Option<OptionValueKind>,
73    /// A subcommand's options.
74    #[serde(default, skip_serializing_if = "Vec::is_empty")]
75    pub options: Vec<OptionValue>,
76    /// The option being typed (autocomplete only).
77    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
78    pub focused: bool,
79}
80
81/// One submitted modal input.
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83pub struct SubmittedInput {
84    /// The component's `id`.
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub id: Option<u32>,
87    /// The component's `custom_id`.
88    pub custom_id: String,
89    /// Text inputs and checkboxes (`"true"`/`"false"`).
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub value: Option<String>,
92    /// Selects, radio and checkbox groups, file uploads (attachment ids).
93    #[serde(default, skip_serializing_if = "Vec::is_empty")]
94    pub values: Vec<String>,
95}
96
97/// Objects an interaction refers to, resolved by the host.
98#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
99pub struct Resolved {
100    /// Members.
101    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
102    pub members: BTreeMap<MemberId, Member>,
103    /// Roles.
104    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
105    pub roles: BTreeMap<RoleId, Role>,
106    /// Channels.
107    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
108    pub channels: BTreeMap<ChannelId, Channel>,
109    /// Messages.
110    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
111    pub messages: BTreeMap<MessageId, Message>,
112    /// Attachments.
113    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
114    pub attachments: BTreeMap<AttachmentId, Attachment>,
115}
116
117impl Resolved {
118    /// Nothing resolved.
119    #[must_use]
120    pub fn is_empty(&self) -> bool {
121        *self == Self::default()
122    }
123}
124
125/// `data` of an interaction, by type.
126#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
127#[serde(untagged)]
128pub enum InteractionData {
129    /// Types 2 and 4.
130    Command {
131        /// The command.
132        command_id: CommandId,
133        /// Its name.
134        name: String,
135        /// Slash, user or message.
136        #[serde(rename = "type")]
137        kind: CommandType,
138        /// The values given.
139        #[serde(default, skip_serializing_if = "Vec::is_empty")]
140        options: Vec<OptionValue>,
141        /// Referenced objects.
142        #[serde(default, skip_serializing_if = "Resolved::is_empty")]
143        resolved: Resolved,
144        /// The member (`USER`) or message (`MESSAGE`) acted on, base64url.
145        #[serde(default, skip_serializing_if = "Option::is_none")]
146        target_id: Option<String>,
147    },
148    /// Type 3.
149    Component {
150        /// The component's `custom_id`.
151        custom_id: String,
152        /// Its type number.
153        component_type: u8,
154        /// Chosen values of a select.
155        #[serde(default, skip_serializing_if = "Vec::is_empty")]
156        values: Vec<String>,
157        /// Entities chosen in an entity select.
158        #[serde(default, skip_serializing_if = "Resolved::is_empty")]
159        resolved: Resolved,
160    },
161    /// Type 5.
162    Modal {
163        /// The modal's `custom_id`.
164        custom_id: String,
165        /// The inputs.
166        components: Vec<SubmittedInput>,
167        /// Uploaded files and chosen entities.
168        #[serde(default, skip_serializing_if = "Resolved::is_empty")]
169        resolved: Resolved,
170    },
171}
172
173/// An interaction as delivered to its bot (`interaction.created`).
174#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
175pub struct Interaction {
176    /// Host-assigned.
177    pub interaction_id: InteractionId,
178    /// The kind.
179    #[serde(rename = "type")]
180    pub kind: InteractionType,
181    /// The bot it is for.
182    pub bot_id: BotId,
183    /// Where it happened.
184    pub channel_id: ChannelId,
185    /// The invoker, as the bot may see it.
186    pub member: Member,
187    /// The bot's effective permissions in the channel.
188    pub app_permissions: Permissions,
189    /// The invoker's client locale.
190    #[serde(default, skip_serializing_if = "Option::is_none")]
191    pub locale: Option<String>,
192    /// Type 3: the message holding the component.
193    #[serde(default, skip_serializing_if = "Option::is_none")]
194    pub message: Option<Box<Message>>,
195    /// By type.
196    pub data: InteractionData,
197    /// When the host received it, ms.
198    pub created_at: u64,
199}
200
201/// `interaction_metadata` of a message that answers an interaction.
202#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
203pub struct InteractionMetadata {
204    /// The interaction.
205    pub interaction_id: InteractionId,
206    /// Its type.
207    #[serde(rename = "type")]
208    pub kind: InteractionType,
209    /// The command's name, for commands.
210    #[serde(default, skip_serializing_if = "Option::is_none")]
211    pub name: Option<String>,
212    /// The invoker.
213    pub member_id: MemberId,
214}