Skip to main content

zaun_bot/
options.rs

1//! Command options: [`CommandOptionBuilder`] to declare them, [`Options`] to read them.
2
3use crate::types::{
4    Choice, ChoiceValue, CommandData, CommandOption, ModalData, OptionKind, OptionValue,
5};
6
7/// Builds a [`CommandOption`].
8#[derive(Debug, Clone)]
9pub struct CommandOptionBuilder(CommandOption);
10
11impl CommandOptionBuilder {
12    /// An option of `kind`. `name`: 1–32 lowercase; `description`: 1–100.
13    #[must_use]
14    pub fn new(kind: OptionKind, name: impl Into<String>, description: impl Into<String>) -> Self {
15        Self(CommandOption {
16            kind,
17            name: name.into(),
18            name_localizations: Vec::new(),
19            description: description.into(),
20            description_localizations: Vec::new(),
21            required: false,
22            choices: Vec::new(),
23            channel_types: Vec::new(),
24            min_value: None,
25            max_value: None,
26            min_length: None,
27            max_length: None,
28            autocomplete: false,
29        })
30    }
31
32    /// A text option.
33    #[must_use]
34    pub fn string(name: impl Into<String>, description: impl Into<String>) -> Self {
35        Self::new(OptionKind::String, name, description)
36    }
37
38    /// An integer option.
39    #[must_use]
40    pub fn integer(name: impl Into<String>, description: impl Into<String>) -> Self {
41        Self::new(OptionKind::Integer, name, description)
42    }
43
44    /// A member option.
45    #[must_use]
46    pub fn user(name: impl Into<String>, description: impl Into<String>) -> Self {
47        Self::new(OptionKind::User, name, description)
48    }
49
50    /// Must be filled.
51    #[must_use]
52    pub fn required(mut self) -> Self {
53        self.0.required = true;
54        self
55    }
56
57    /// Ask the bot for suggestions as the invoker types (`Bot::on_autocomplete`).
58    #[must_use]
59    pub fn autocomplete(mut self) -> Self {
60        self.0.autocomplete = true;
61        self
62    }
63
64    /// Adds a fixed text choice (≤ 25; not with autocomplete).
65    #[must_use]
66    pub fn choice(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
67        self.0.choices.push(text_choice(name, value));
68        self
69    }
70
71    /// Numeric bounds (integer and number options).
72    #[must_use]
73    pub fn range(mut self, min: f64, max: f64) -> Self {
74        (self.0.min_value, self.0.max_value) = (Some(min), Some(max));
75        self
76    }
77
78    /// Length bounds (text options, 0–6 000).
79    #[must_use]
80    pub fn length(mut self, min: u16, max: u16) -> Self {
81        (self.0.min_length, self.0.max_length) = (Some(min), Some(max));
82        self
83    }
84
85    /// The finished option.
86    #[must_use]
87    pub fn build(self) -> CommandOption {
88        self.0
89    }
90}
91
92/// A text [`Choice`] (autocomplete results and fixed choices).
93#[must_use]
94pub fn text_choice(name: impl Into<String>, value: impl Into<String>) -> Choice {
95    Choice {
96        name: name.into(),
97        name_localizations: Vec::new(),
98        value: ChoiceValue::Text(value.into()),
99    }
100}
101
102/// Reads option and modal values by name.
103pub trait Options {
104    /// The value named `name`.
105    fn value(&self, name: &str) -> Option<&OptionValue>;
106
107    /// A text value (string options, modal text inputs).
108    fn str(&self, name: &str) -> Option<&str> {
109        match self.value(name)? {
110            OptionValue::Text(s) | OptionValue::Id(s) => Some(s),
111            _ => None,
112        }
113    }
114
115    /// An integer value.
116    fn int(&self, name: &str) -> Option<i64> {
117        match self.value(name)? {
118            OptionValue::Integer(i) => Some(*i),
119            _ => None,
120        }
121    }
122
123    /// A boolean value.
124    fn bool(&self, name: &str) -> Option<bool> {
125        match self.value(name)? {
126            OptionValue::Boolean(b) => Some(*b),
127            _ => None,
128        }
129    }
130}
131
132impl Options for CommandData {
133    fn value(&self, name: &str) -> Option<&OptionValue> {
134        self.options
135            .iter()
136            .find(|o| o.name == name)
137            .map(|o| &o.value)
138    }
139}
140
141impl ModalData {
142    /// The text a modal input with `custom_id` held.
143    #[must_use]
144    pub fn text(&self, custom_id: &str) -> Option<&str> {
145        let input = self.components.iter().find(|c| c.custom_id == custom_id)?;
146        input.value.as_deref()
147    }
148}
149
150impl CommandData {
151    /// The option the invoker is typing in (autocomplete).
152    #[must_use]
153    pub fn focused(&self) -> Option<(&str, &OptionValue)> {
154        self.options
155            .iter()
156            .find(|o| o.focused)
157            .map(|o| (o.name.as_str(), &o.value))
158    }
159}