Skip to main content

zaun_client/bots/
interaction.rs

1//! An interaction delivered to this client as a bot (`interaction.created`), and its answers.
2
3use zaun_community_protocol::command::interaction::{
4    Followup, OriginalDelete, OriginalEdit, Replied, Respond,
5};
6use zaun_community_protocol::command::object::{
7    Interaction as Data, InteractionData, InteractionType, MessageBody, OptionValue,
8};
9use zaun_community_protocol::ids::InteractionId;
10
11use super::Response;
12use crate::error::Result;
13use crate::objects::{Channel, Community, Member};
14
15/// Someone used one of this bot's commands, components or modals. Answer within 3 s
16/// ([`respond`](Self::respond), or [`Response::defer`] first); follow-ups and edits work for
17/// 15 minutes.
18#[derive(Debug, Clone)]
19pub struct Interaction {
20    pub(crate) community: Community,
21    pub(crate) data: Box<Data>,
22}
23
24impl Interaction {
25    /// Its id.
26    #[must_use]
27    pub fn id(&self) -> InteractionId {
28        self.data.interaction_id
29    }
30
31    /// Command, component, autocomplete or modal submit.
32    #[must_use]
33    pub fn kind(&self) -> InteractionType {
34        self.data.kind
35    }
36
37    /// Everything the host sent.
38    #[must_use]
39    pub fn data(&self) -> &Data {
40        &self.data
41    }
42
43    /// The channel it happened in.
44    #[must_use]
45    pub fn channel(&self) -> Channel {
46        self.community.channel(self.data.channel_id)
47    }
48
49    /// Who used it.
50    #[must_use]
51    pub fn member(&self) -> Member {
52        self.community.member(self.data.member.member_id)
53    }
54
55    /// The command's name, for a command or autocomplete.
56    #[must_use]
57    pub fn command_name(&self) -> Option<&str> {
58        match &self.data.data {
59            InteractionData::Command { name, .. } => Some(name),
60            InteractionData::Component { .. } | InteractionData::Modal { .. } => None,
61        }
62    }
63
64    /// The options given, for a command or autocomplete.
65    #[must_use]
66    pub fn options(&self) -> &[OptionValue] {
67        match &self.data.data {
68            InteractionData::Command { options, .. } => options,
69            InteractionData::Component { .. } | InteractionData::Modal { .. } => &[],
70        }
71    }
72
73    /// The clicked component's or submitted modal's `custom_id`.
74    #[must_use]
75    pub fn custom_id(&self) -> Option<&str> {
76        match &self.data.data {
77            InteractionData::Component { custom_id, .. }
78            | InteractionData::Modal { custom_id, .. } => Some(custom_id),
79            InteractionData::Command { .. } => None,
80        }
81    }
82
83    /// A submitted modal input's text (`value`) by `custom_id`.
84    #[must_use]
85    pub fn field(&self, custom_id: &str) -> Option<&str> {
86        match &self.data.data {
87            InteractionData::Modal { components, .. } => components
88                .iter()
89                .find(|c| c.custom_id == custom_id)
90                .and_then(|c| c.value.as_deref()),
91            InteractionData::Command { .. } | InteractionData::Component { .. } => None,
92        }
93    }
94
95    /// Answers it (once, within 3 s).
96    ///
97    /// # Errors
98    /// The host refused (too late, answered already, a body it does not accept).
99    pub fn respond(&self, response: Response) -> Result<Replied> {
100        let body = Respond {
101            interaction_id: self.id(),
102            kind: response.kind,
103            data: response.data,
104        };
105        self.community.client.command(self.community.id, None, body)
106    }
107
108    /// Answers with a text message in the channel.
109    ///
110    /// # Errors
111    /// As [`respond`](Self::respond).
112    pub fn reply(&self, content: impl Into<String>) -> Result<Replied> {
113        self.respond(Response::text(content))
114    }
115
116    /// Answers with a message only the invoker sees.
117    ///
118    /// # Errors
119    /// As [`respond`](Self::respond).
120    pub fn reply_ephemeral(&self, content: impl Into<String>) -> Result<Replied> {
121        self.respond(Response::ephemeral(super::respond::text(content)))
122    }
123
124    /// Another message after the answer (up to 10, within 15 minutes).
125    ///
126    /// # Errors
127    /// The host refused or did not answer.
128    pub fn followup(&self, body: MessageBody) -> Result<Replied> {
129        let body = Followup {
130            interaction_id: self.id(),
131            body,
132        };
133        self.community.client.command(self.community.id, None, body)
134    }
135
136    /// Edits the answer (ends a deferred "thinking…"); absent fields stay.
137    ///
138    /// # Errors
139    /// The host refused or did not answer.
140    pub fn edit_original(&self, body: MessageBody) -> Result<()> {
141        let body = OriginalEdit {
142            interaction_id: self.id(),
143            body,
144        };
145        self.community
146            .client
147            .command(self.community.id, None, body)
148            .map(|_| ())
149    }
150
151    /// Deletes the answer.
152    ///
153    /// # Errors
154    /// The host refused or did not answer.
155    pub fn delete_original(&self) -> Result<()> {
156        let body = OriginalDelete {
157            interaction_id: self.id(),
158        };
159        self.community
160            .client
161            .command(self.community.id, None, body)
162            .map(|_| ())
163    }
164}