Skip to main content

zaun_client/bots/
message.rs

1//! A bot message's components, flags and interaction, and clicking its components.
2
3use zaun_community_protocol::command::interaction::CreateData;
4use zaun_community_protocol::command::object::{
5    Component, InteractionMetadata, InteractionType, flags,
6};
7use zaun_community_protocol::ids::InteractionId;
8
9use crate::error::Result;
10use crate::objects::Message;
11
12impl Message {
13    /// Its buttons, selects and layout (bots only).
14    #[must_use]
15    pub fn components(&self) -> &[Component] {
16        &self.components
17    }
18
19    /// Its flags (`flags::*`).
20    #[must_use]
21    pub const fn flags(&self) -> u32 {
22        self.flags
23    }
24
25    /// Whether a deferred bot reply is still "thinking…".
26    #[must_use]
27    pub const fn is_loading(&self) -> bool {
28        self.flags & flags::LOADING != 0
29    }
30
31    /// The interaction this bot reply answers.
32    #[must_use]
33    pub const fn interaction(&self) -> Option<&InteractionMetadata> {
34        self.interaction.as_ref()
35    }
36
37    /// Clicks a button, or chooses `values` in a select, of this message.
38    ///
39    /// # Errors
40    /// The host refused (no such enabled component, values out of bounds).
41    pub fn click(&self, custom_id: &str, values: Vec<String>) -> Result<InteractionId> {
42        let data = CreateData::Component {
43            message_id: self.id,
44            custom_id: custom_id.to_owned(),
45            values,
46        };
47        let created: zaun_community_protocol::command::interaction::Created =
48            self.channel.community.client.command(
49                self.channel.community.id,
50                Some(self.channel.id),
51                zaun_community_protocol::command::interaction::Create {
52                    kind: InteractionType::MessageComponent,
53                    data,
54                },
55            )?;
56        Ok(created.interaction_id)
57    }
58}