zaun_client/ui/
command.rs1use std::collections::BTreeMap;
4
5use zaun_community_protocol::bits::Permissions;
6use zaun_community_protocol::command::object::{
7 ApplicationCommand, Choice, ChoiceValue, CommandOption, CommandType, OptionType,
8};
9
10#[derive(Debug, Clone)]
12pub struct CommandBuilder(ApplicationCommand);
13
14impl CommandBuilder {
15 fn new(kind: CommandType, name: &str, description: &str) -> Self {
16 Self(ApplicationCommand {
17 command_id: None,
18 bot_id: None,
19 kind,
20 name: name.to_owned(),
21 name_localizations: BTreeMap::new(),
22 description: description.to_owned(),
23 description_localizations: BTreeMap::new(),
24 options: Vec::new(),
25 default_member_permissions: None,
26 })
27 }
28
29 #[must_use]
31 pub fn slash(name: &str, description: &str) -> Self {
32 Self::new(CommandType::ChatInput, name, description)
33 }
34
35 #[must_use]
37 pub fn user(name: &str) -> Self {
38 Self::new(CommandType::User, name, "")
39 }
40
41 #[must_use]
43 pub fn message(name: &str) -> Self {
44 Self::new(CommandType::Message, name, "")
45 }
46
47 #[must_use]
49 pub fn option(mut self, option: OptionBuilder) -> Self {
50 self.0.options.push(option.0);
51 self
52 }
53
54 #[must_use]
56 pub const fn permissions(mut self, bits: Permissions) -> Self {
57 self.0.default_member_permissions = Some(bits);
58 self
59 }
60
61 #[must_use]
63 pub fn build(self) -> ApplicationCommand {
64 self.0
65 }
66}
67
68#[derive(Debug, Clone)]
70pub struct OptionBuilder(CommandOption);
71
72impl OptionBuilder {
73 #[must_use]
75 pub fn new(kind: OptionType, name: &str, description: &str) -> Self {
76 Self(CommandOption {
77 kind,
78 name: name.to_owned(),
79 name_localizations: BTreeMap::new(),
80 description: description.to_owned(),
81 description_localizations: BTreeMap::new(),
82 required: false,
83 choices: Vec::new(),
84 options: Vec::new(),
85 channel_types: Vec::new(),
86 min_value: None,
87 max_value: None,
88 min_length: None,
89 max_length: None,
90 autocomplete: false,
91 })
92 }
93
94 #[must_use]
96 pub fn string(name: &str, description: &str) -> Self {
97 Self::new(OptionType::String, name, description)
98 }
99
100 #[must_use]
102 pub fn integer(name: &str, description: &str) -> Self {
103 Self::new(OptionType::Integer, name, description)
104 }
105
106 #[must_use]
108 pub fn user(name: &str, description: &str) -> Self {
109 Self::new(OptionType::User, name, description)
110 }
111
112 #[must_use]
114 pub const fn required(mut self) -> Self {
115 self.0.required = true;
116 self
117 }
118
119 #[must_use]
121 pub const fn autocomplete(mut self) -> Self {
122 self.0.autocomplete = true;
123 self
124 }
125
126 #[must_use]
128 pub fn choice(mut self, name: &str, value: &str) -> Self {
129 self.0.choices.push(Choice {
130 name: name.to_owned(),
131 name_localizations: BTreeMap::new(),
132 value: ChoiceValue::String(value.to_owned()),
133 });
134 self
135 }
136}