Skip to main content

zaun_client/bots/
respond.rs

1//! A bot's answer to an interaction (§8.14 callback types 4–9).
2
3use zaun_community_protocol::command::interaction::{ResponseData, ResponseType};
4use zaun_community_protocol::command::object::{Choice, Component, MessageBody, flags};
5
6/// What a bot answers an interaction with. Build one with the constructors.
7#[derive(Debug, Clone, PartialEq)]
8pub struct Response {
9    pub(crate) kind: ResponseType,
10    pub(crate) data: ResponseData,
11}
12
13impl Response {
14    /// A message in the channel (type 4).
15    #[must_use]
16    pub fn message(body: MessageBody) -> Self {
17        Self {
18            kind: ResponseType::ChannelMessage,
19            data: ResponseData::Message(Box::new(body)),
20        }
21    }
22
23    /// A text message in the channel.
24    #[must_use]
25    pub fn text(content: impl Into<String>) -> Self {
26        Self::message(text(content))
27    }
28
29    /// A message only the invoker sees (type 4 with `EPHEMERAL`).
30    #[must_use]
31    pub fn ephemeral(mut body: MessageBody) -> Self {
32        body.flags = Some(body.flags.unwrap_or(0) | flags::EPHEMERAL);
33        Self::message(body)
34    }
35
36    /// "Thinking…" now, the message later with `edit_original` (type 5).
37    #[must_use]
38    pub fn defer(ephemeral: bool) -> Self {
39        let flags = ephemeral.then_some(flags::EPHEMERAL);
40        Self {
41            kind: ResponseType::DeferredChannelMessage,
42            data: ResponseData::Deferred { flags },
43        }
44    }
45
46    /// Acknowledge a component click without a visible change yet (type 6).
47    #[must_use]
48    pub const fn defer_update() -> Self {
49        Self {
50            kind: ResponseType::DeferredUpdateMessage,
51            data: ResponseData::None,
52        }
53    }
54
55    /// Edit the message holding the clicked component (type 7).
56    #[must_use]
57    pub fn update(body: MessageBody) -> Self {
58        Self {
59            kind: ResponseType::UpdateMessage,
60            data: ResponseData::Message(Box::new(body)),
61        }
62    }
63
64    /// Autocomplete suggestions, up to 25 (type 8).
65    #[must_use]
66    pub fn choices(choices: Vec<Choice>) -> Self {
67        Self {
68            kind: ResponseType::AutocompleteResult,
69            data: ResponseData::Autocomplete { choices },
70        }
71    }
72
73    /// Open `modal` (type 9).
74    #[must_use]
75    pub fn show(modal: crate::ui::Modal) -> Self {
76        Self::modal(modal.custom_id, modal.title, modal.components)
77    }
78
79    /// Open a modal (type 9): 1–5 labels or text displays.
80    #[must_use]
81    pub fn modal(
82        custom_id: impl Into<String>,
83        title: impl Into<String>,
84        components: Vec<Component>,
85    ) -> Self {
86        Self {
87            kind: ResponseType::Modal,
88            data: ResponseData::Modal {
89                custom_id: custom_id.into(),
90                title: title.into(),
91                components,
92            },
93        }
94    }
95}
96
97/// A message body of just `content`.
98#[must_use]
99pub fn text(content: impl Into<String>) -> MessageBody {
100    MessageBody {
101        content: Some(content.into()),
102        ..MessageBody::default()
103    }
104}