Skip to main content

zaun_bot/
select.rs

1//! [`Select`] menus (types 3, 5–8) and [`ActionRow`] (type 1).
2
3use crate::Button;
4use crate::types::{self, EntityKind, RowItem, SelectOption};
5
6/// Builds a select menu: string options or members, roles, mentionables or channels.
7#[derive(Debug, Clone)]
8pub enum Select {
9    /// Type 3.
10    Strings(types::StringSelect),
11    /// Types 5–8.
12    Entities(types::EntitySelect),
13}
14
15impl Select {
16    /// A string select with these `(label, value)` options (≤ 25).
17    #[must_use]
18    pub fn strings(custom_id: impl Into<String>, options: &[(&str, &str)]) -> Self {
19        let options = options
20            .iter()
21            .map(|(label, value)| SelectOption {
22                label: (*label).to_owned(),
23                value: (*value).to_owned(),
24                description: None,
25                emoji: None,
26                is_default: false,
27            })
28            .collect();
29        Self::Strings(types::StringSelect {
30            id: None,
31            custom_id: custom_id.into(),
32            options,
33            placeholder: None,
34            min_values: None,
35            max_values: None,
36            required: None,
37            disabled: false,
38        })
39    }
40
41    /// A member, role, mentionable or channel select.
42    #[must_use]
43    pub fn entities(kind: EntityKind, custom_id: impl Into<String>) -> Self {
44        Self::Entities(types::EntitySelect {
45            id: None,
46            kind,
47            custom_id: custom_id.into(),
48            placeholder: None,
49            min_values: None,
50            max_values: None,
51            default_values: Vec::new(),
52            channel_types: Vec::new(),
53            required: None,
54            disabled: false,
55        })
56    }
57
58    /// Placeholder text (≤ 150).
59    #[must_use]
60    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
61        match &mut self {
62            Self::Strings(s) => s.placeholder = Some(text.into()),
63            Self::Entities(s) => s.placeholder = Some(text.into()),
64        }
65        self
66    }
67
68    /// How many values may be picked (0–25).
69    #[must_use]
70    pub fn range(mut self, min: u8, max: u8) -> Self {
71        match &mut self {
72            Self::Strings(s) => (s.min_values, s.max_values) = (Some(min), Some(max)),
73            Self::Entities(s) => (s.min_values, s.max_values) = (Some(min), Some(max)),
74        }
75        self
76    }
77
78    pub(crate) fn row_item(self) -> RowItem {
79        match self {
80            Self::Strings(s) => RowItem::StringSelect(s),
81            Self::Entities(s) => RowItem::EntitySelect(s),
82        }
83    }
84}
85
86/// Builds a [`types::ActionRow`]: up to 5 buttons, or one select.
87#[derive(Debug, Clone, Default)]
88pub struct ActionRow(Vec<RowItem>);
89
90impl ActionRow {
91    /// An empty row.
92    #[must_use]
93    pub fn new() -> Self {
94        Self::default()
95    }
96
97    /// A row of buttons.
98    #[must_use]
99    pub fn buttons(buttons: impl IntoIterator<Item = Button>) -> Self {
100        Self(
101            buttons
102                .into_iter()
103                .map(|b| RowItem::Button(b.build()))
104                .collect(),
105        )
106    }
107
108    /// A row holding one select.
109    #[must_use]
110    pub fn select(select: Select) -> Self {
111        Self(vec![select.row_item()])
112    }
113
114    /// Adds a button.
115    #[must_use]
116    pub fn button(mut self, button: Button) -> Self {
117        self.0.push(RowItem::Button(button.build()));
118        self
119    }
120
121    /// The finished row.
122    #[must_use]
123    pub fn build(self) -> types::ActionRow {
124        types::ActionRow {
125            id: None,
126            components: self.0,
127        }
128    }
129}