1use crate::types::{
4 Choice, ChoiceValue, CommandData, CommandOption, ModalData, OptionKind, OptionValue,
5};
6
7#[derive(Debug, Clone)]
9pub struct CommandOptionBuilder(CommandOption);
10
11impl CommandOptionBuilder {
12 #[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 #[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 #[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 #[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_use]
52 pub fn required(mut self) -> Self {
53 self.0.required = true;
54 self
55 }
56
57 #[must_use]
59 pub fn autocomplete(mut self) -> Self {
60 self.0.autocomplete = true;
61 self
62 }
63
64 #[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 #[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 #[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 #[must_use]
87 pub fn build(self) -> CommandOption {
88 self.0
89 }
90}
91
92#[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
102pub trait Options {
104 fn value(&self, name: &str) -> Option<&OptionValue>;
106
107 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 fn int(&self, name: &str) -> Option<i64> {
117 match self.value(name)? {
118 OptionValue::Integer(i) => Some(*i),
119 _ => None,
120 }
121 }
122
123 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 #[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 #[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}