Skip to main content

zaun_community_protocol/command/
interaction.rs

1//! Interaction commands (specification §8.14): `interaction.create` from members, and the
2//! bot's `interaction.respond`, follow-ups and original edits.
3
4mod raw;
5mod validate;
6
7use serde::{Deserialize, Serialize};
8
9use super::object::{Choice, Component, InteractionType, MessageBody, OptionValue, SubmittedInput};
10use crate::ids::{InteractionId, MessageId};
11
12/// `interaction.create` data, by type.
13#[derive(Debug, Clone, PartialEq)]
14pub enum CreateData {
15    /// Types 2 and 4.
16    Command {
17        /// The command.
18        command_id: crate::ids::CommandId,
19        /// Values given (one `focused` for type 4).
20        options: Vec<OptionValue>,
21        /// `USER` / `MESSAGE` target, base64url.
22        target_id: Option<String>,
23    },
24    /// Type 3.
25    Component {
26        /// The message holding the component.
27        message_id: MessageId,
28        /// The component.
29        custom_id: String,
30        /// Chosen values of a select.
31        values: Vec<String>,
32    },
33    /// Type 5.
34    Modal {
35        /// The interaction that opened the modal.
36        modal_id: InteractionId,
37        /// The modal's `custom_id`.
38        custom_id: String,
39        /// The inputs.
40        components: Vec<SubmittedInput>,
41    },
42}
43
44/// `interaction.create`.
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46#[serde(try_from = "raw::RawCreate", into = "raw::RawCreate")]
47pub struct Create {
48    /// 2–5.
49    pub kind: InteractionType,
50    /// By type.
51    pub data: CreateData,
52}
53
54/// `interaction.create` response.
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
56pub struct Created {
57    /// The new interaction.
58    pub interaction_id: InteractionId,
59}
60
61/// Interaction response types (Discord's callback numbers).
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
63#[serde(try_from = "u8", into = "u8")]
64pub enum ResponseType {
65    /// 4: a message.
66    ChannelMessage,
67    /// 5: a loading message, edited later.
68    DeferredChannelMessage,
69    /// 6: acknowledge a component without a visible change yet.
70    DeferredUpdateMessage,
71    /// 7: edit the component's message.
72    UpdateMessage,
73    /// 8: autocomplete choices.
74    AutocompleteResult,
75    /// 9: open a modal.
76    Modal,
77}
78
79impl TryFrom<u8> for ResponseType {
80    type Error = String;
81    fn try_from(v: u8) -> Result<Self, String> {
82        Ok(match v {
83            4 => Self::ChannelMessage,
84            5 => Self::DeferredChannelMessage,
85            6 => Self::DeferredUpdateMessage,
86            7 => Self::UpdateMessage,
87            8 => Self::AutocompleteResult,
88            9 => Self::Modal,
89            _ => return Err(format!("unsupported response type {v}")),
90        })
91    }
92}
93
94impl From<ResponseType> for u8 {
95    fn from(t: ResponseType) -> Self {
96        t as Self + 4
97    }
98}
99
100/// Response data, by type.
101#[derive(Debug, Clone, PartialEq)]
102pub enum ResponseData {
103    /// Types 4 and 7.
104    Message(Box<MessageBody>),
105    /// Type 5: only `EPHEMERAL` may be set.
106    Deferred {
107        /// Flags.
108        flags: Option<u32>,
109    },
110    /// Type 6.
111    None,
112    /// Type 8.
113    Autocomplete {
114        /// ≤ 25.
115        choices: Vec<Choice>,
116    },
117    /// Type 9.
118    Modal {
119        /// 1–100 bytes.
120        custom_id: String,
121        /// 1–45 scalars.
122        title: String,
123        /// 1–5 labels or text displays.
124        components: Vec<Component>,
125    },
126}
127
128/// `interaction.respond`.
129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
130#[serde(try_from = "raw::RawRespond", into = "raw::RawRespond")]
131pub struct Respond {
132    /// The bot's own interaction.
133    pub interaction_id: InteractionId,
134    /// What to do.
135    pub kind: ResponseType,
136    /// By type.
137    pub data: ResponseData,
138}
139
140/// `interaction.respond` and `interaction.followup` response.
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
142pub struct Replied {
143    /// The channel message created or edited; absent for ephemeral and invisible responses.
144    #[serde(default, skip_serializing_if = "Option::is_none")]
145    pub message_id: Option<MessageId>,
146}
147
148/// `interaction.followup` and `interaction.original.edit`.
149#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
150pub struct Followup {
151    /// The interaction.
152    pub interaction_id: InteractionId,
153    /// The message.
154    #[serde(flatten)]
155    pub body: MessageBody,
156}
157
158/// `interaction.original.edit`: absent fields stay unchanged.
159#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
160pub struct OriginalEdit {
161    /// The interaction.
162    pub interaction_id: InteractionId,
163    /// The changes.
164    #[serde(flatten)]
165    pub body: MessageBody,
166}
167
168/// `interaction.original.delete`.
169#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
170pub struct OriginalDelete {
171    /// The interaction.
172    pub interaction_id: InteractionId,
173}