Skip to main content

zaun_bot/
button.rs

1//! [`Button`] (ยง8.14, type 2).
2
3use crate::types::{self, ButtonStyle};
4
5/// Builds a [`types::Button`].
6#[derive(Debug, Clone)]
7pub struct Button(types::Button);
8
9impl Button {
10    fn styled(style: ButtonStyle, custom_id: Option<String>, label: String) -> Self {
11        Self(types::Button {
12            id: None,
13            style,
14            label: Some(label),
15            emoji: None,
16            custom_id,
17            url: None,
18            disabled: false,
19        })
20    }
21
22    /// A primary (blurple) button.
23    #[must_use]
24    pub fn primary(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
25        Self::styled(ButtonStyle::Primary, Some(custom_id.into()), label.into())
26    }
27
28    /// A secondary (grey) button.
29    #[must_use]
30    pub fn secondary(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
31        Self::styled(ButtonStyle::Secondary, Some(custom_id.into()), label.into())
32    }
33
34    /// A success (green) button.
35    #[must_use]
36    pub fn success(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
37        Self::styled(ButtonStyle::Success, Some(custom_id.into()), label.into())
38    }
39
40    /// A danger (red) button.
41    #[must_use]
42    pub fn danger(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
43        Self::styled(ButtonStyle::Danger, Some(custom_id.into()), label.into())
44    }
45
46    /// A link button: `https://` only; clients ask before opening it. Sends no interaction.
47    #[must_use]
48    pub fn link(url: impl Into<String>, label: impl Into<String>) -> Self {
49        let mut b = Self::styled(ButtonStyle::Link, None, label.into());
50        b.0.url = Some(url.into());
51        b
52    }
53
54    /// Adds a Unicode emoji.
55    #[must_use]
56    pub fn emoji(mut self, emoji: impl Into<String>) -> Self {
57        self.0.emoji = Some(emoji.into());
58        self
59    }
60
61    /// Greys it out.
62    #[must_use]
63    pub fn disabled(mut self, disabled: bool) -> Self {
64        self.0.disabled = disabled;
65        self
66    }
67
68    /// The finished button.
69    #[must_use]
70    pub fn build(self) -> types::Button {
71        self.0
72    }
73}