Skip to main content

zaun_bot/
command.rs

1//! Application command builders (§8.14).
2
3use crate::CommandOptionBuilder;
4use crate::types::{ApplicationCommand, CommandEntry, CommandKind, SubCommand as WitSub};
5
6/// Builds an [`ApplicationCommand`]: a slash command, or a user or message context command.
7#[derive(Debug, Clone)]
8pub struct SlashCommand(ApplicationCommand);
9
10impl SlashCommand {
11    fn of(kind: CommandKind, name: String, description: String) -> Self {
12        Self(ApplicationCommand {
13            kind,
14            name,
15            name_localizations: Vec::new(),
16            description,
17            description_localizations: Vec::new(),
18            options: Vec::new(),
19            default_member_permissions: None,
20        })
21    }
22
23    /// A slash command. `name`: 1–32 lowercase letters, digits, `-` or `_`; `description`:
24    /// 1–100.
25    #[must_use]
26    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
27        Self::of(CommandKind::ChatInput, name.into(), description.into())
28    }
29
30    /// A user context-menu command ("Apps → name" on a member).
31    #[must_use]
32    pub fn user(name: impl Into<String>) -> Self {
33        Self::of(CommandKind::User, name.into(), String::new())
34    }
35
36    /// A message context-menu command.
37    #[must_use]
38    pub fn message(name: impl Into<String>) -> Self {
39        Self::of(CommandKind::Message, name.into(), String::new())
40    }
41
42    /// Adds an option (≤ 25; required ones first).
43    #[must_use]
44    pub fn option(mut self, option: CommandOptionBuilder) -> Self {
45        self.0
46            .options
47            .push(CommandEntry::ValueOption(option.build()));
48        self
49    }
50
51    /// Adds a sub-command.
52    #[must_use]
53    pub fn sub_command(mut self, sub: SubCommand) -> Self {
54        self.0.options.push(CommandEntry::SubCommand(sub.0));
55        self
56    }
57
58    /// Adds a group of sub-commands.
59    #[must_use]
60    pub fn group(mut self, group: SubCommandGroup) -> Self {
61        self.0.options.push(CommandEntry::Group(group.0));
62        self
63    }
64
65    /// Bits the invoker needs in the channel; `0` makes it owner-only.
66    #[must_use]
67    pub fn default_member_permissions(mut self, bits: u64) -> Self {
68        self.0.default_member_permissions = Some(bits);
69        self
70    }
71
72    /// Adds a translated name (BCP 47 locale).
73    #[must_use]
74    pub fn localized_name(mut self, locale: &str, name: &str) -> Self {
75        let l = crate::types::Localized {
76            locale: locale.to_owned(),
77            text: name.to_owned(),
78        };
79        self.0.name_localizations.push(l);
80        self
81    }
82
83    /// The finished command.
84    #[must_use]
85    pub fn build(self) -> ApplicationCommand {
86        self.0
87    }
88}
89
90/// Builds a sub-command.
91#[derive(Debug, Clone)]
92pub struct SubCommand(WitSub);
93
94impl SubCommand {
95    /// A sub-command (`/name sub`).
96    #[must_use]
97    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
98        Self(WitSub {
99            name: name.into(),
100            name_localizations: Vec::new(),
101            description: description.into(),
102            description_localizations: Vec::new(),
103            options: Vec::new(),
104        })
105    }
106
107    /// Adds an option.
108    #[must_use]
109    pub fn option(mut self, option: CommandOptionBuilder) -> Self {
110        self.0.options.push(option.build());
111        self
112    }
113}
114
115/// Builds a group of sub-commands (`/name group sub`).
116#[derive(Debug, Clone)]
117pub struct SubCommandGroup(crate::types::SubCommandGroup);
118
119impl SubCommandGroup {
120    /// A group.
121    #[must_use]
122    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
123        Self(crate::types::SubCommandGroup {
124            name: name.into(),
125            name_localizations: Vec::new(),
126            description: description.into(),
127            description_localizations: Vec::new(),
128            sub_commands: Vec::new(),
129        })
130    }
131
132    /// Adds a sub-command.
133    #[must_use]
134    pub fn sub_command(mut self, sub: SubCommand) -> Self {
135        self.0.sub_commands.push(sub.0);
136        self
137    }
138}