Skip to main content

zaun_client/ui/
command.rs

1//! Application commands for `set_commands` (ยง8.14).
2
3use 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/// A slash, user or message command.
11#[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    /// `/name`, lower case.
30    #[must_use]
31    pub fn slash(name: &str, description: &str) -> Self {
32        Self::new(CommandType::ChatInput, name, description)
33    }
34
35    /// An entry in a member's menu.
36    #[must_use]
37    pub fn user(name: &str) -> Self {
38        Self::new(CommandType::User, name, "")
39    }
40
41    /// An entry in a message's menu.
42    #[must_use]
43    pub fn message(name: &str) -> Self {
44        Self::new(CommandType::Message, name, "")
45    }
46
47    /// Adds an option (slash commands).
48    #[must_use]
49    pub fn option(mut self, option: OptionBuilder) -> Self {
50        self.0.options.push(option.0);
51        self
52    }
53
54    /// Bits the invoker needs; empty means the owner only.
55    #[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    /// The command.
62    #[must_use]
63    pub fn build(self) -> ApplicationCommand {
64        self.0
65    }
66}
67
68/// An option of a slash command.
69#[derive(Debug, Clone)]
70pub struct OptionBuilder(CommandOption);
71
72impl OptionBuilder {
73    /// An option of `kind`.
74    #[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    /// Text.
95    #[must_use]
96    pub fn string(name: &str, description: &str) -> Self {
97        Self::new(OptionType::String, name, description)
98    }
99
100    /// A whole number.
101    #[must_use]
102    pub fn integer(name: &str, description: &str) -> Self {
103        Self::new(OptionType::Integer, name, description)
104    }
105
106    /// A member.
107    #[must_use]
108    pub fn user(name: &str, description: &str) -> Self {
109        Self::new(OptionType::User, name, description)
110    }
111
112    /// Must be given.
113    #[must_use]
114    pub const fn required(mut self) -> Self {
115        self.0.required = true;
116        self
117    }
118
119    /// The bot suggests values as the member types.
120    #[must_use]
121    pub const fn autocomplete(mut self) -> Self {
122        self.0.autocomplete = true;
123        self
124    }
125
126    /// A fixed string choice.
127    #[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}