Skip to main content

zaun_client/bots/
channel.rs

1//! Using bots from a channel: slash, user and message commands, modal submissions and
2//! autocomplete (ยง8.14). Answers arrive as events.
3
4use zaun_community_protocol::command::interaction::{Create, CreateData};
5use zaun_community_protocol::command::object::{InteractionType, OptionValue, SubmittedInput};
6use zaun_community_protocol::ids::{CommandId, InteractionId};
7
8use crate::error::Result;
9use crate::objects::Channel;
10
11impl Channel {
12    fn interact(&self, kind: InteractionType, data: CreateData) -> Result<InteractionId> {
13        let created: zaun_community_protocol::command::interaction::Created = self
14            .community
15            .client
16            .command(self.community.id, Some(self.id), Create { kind, data })?;
17        Ok(created.interaction_id)
18    }
19
20    /// Runs a bot command here. `target` is the member (user command) or message (message
21    /// command) acted on, base64url. The bot's answer arrives as an event.
22    ///
23    /// # Errors
24    /// The host refused (no `USE_APPLICATION_COMMANDS`, bad options) or did not answer.
25    pub fn invoke_command(
26        &self,
27        command: CommandId,
28        options: Vec<OptionValue>,
29        target: Option<String>,
30    ) -> Result<InteractionId> {
31        let data = CreateData::Command {
32            command_id: command,
33            options,
34            target_id: target,
35        };
36        self.interact(InteractionType::ApplicationCommand, data)
37    }
38
39    /// Asks the bot for suggestions for the option marked `focused`; they arrive as
40    /// [`Event::Autocomplete`](crate::Event::Autocomplete).
41    ///
42    /// # Errors
43    /// As [`invoke_command`](Self::invoke_command).
44    pub fn autocomplete(
45        &self,
46        command: CommandId,
47        options: Vec<OptionValue>,
48    ) -> Result<InteractionId> {
49        let data = CreateData::Command {
50            command_id: command,
51            options,
52            target_id: None,
53        };
54        self.interact(InteractionType::Autocomplete, data)
55    }
56
57    /// Submits a modal a bot opened ([`Event::ModalOpen`](crate::Event::ModalOpen)): `modal`
58    /// is the interaction that opened it.
59    ///
60    /// # Errors
61    /// The host refused (a required field missing, too late, submitted already).
62    pub fn submit_modal(
63        &self,
64        modal: InteractionId,
65        custom_id: &str,
66        components: Vec<SubmittedInput>,
67    ) -> Result<InteractionId> {
68        let data = CreateData::Modal {
69            modal_id: modal,
70            custom_id: custom_id.to_owned(),
71            components,
72        };
73        self.interact(InteractionType::ModalSubmit, data)
74    }
75}