Skip to main content

zaun_community_protocol/command/object/
app_command.rs

1//! Application commands of bots (specification §8.14), in Discord's shapes.
2
3mod check;
4
5pub use check::check_commands;
6
7use std::collections::BTreeMap;
8
9use serde::{Deserialize, Serialize};
10
11use crate::bits::Permissions;
12use crate::ids::{BotId, CommandId};
13use crate::model::ChannelKind;
14
15/// `type` of a command: 1 `CHAT_INPUT` (a slash command), 2 `USER`, 3 `MESSAGE` (context
16/// menus).
17#[derive(
18    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize,
19)]
20#[serde(try_from = "u8", into = "u8")]
21pub enum CommandType {
22    /// 1.
23    #[default]
24    ChatInput,
25    /// 2: on a member.
26    User,
27    /// 3: on a message.
28    Message,
29}
30
31impl TryFrom<u8> for CommandType {
32    type Error = String;
33    fn try_from(v: u8) -> Result<Self, String> {
34        match v {
35            1 => Ok(Self::ChatInput),
36            2 => Ok(Self::User),
37            3 => Ok(Self::Message),
38            _ => Err(format!("unsupported command type {v}")),
39        }
40    }
41}
42
43impl From<CommandType> for u8 {
44    fn from(t: CommandType) -> Self {
45        t as Self + 1
46    }
47}
48
49/// `type` of an option (Discord's numbers 1–11).
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
51#[serde(try_from = "u8", into = "u8")]
52pub enum OptionType {
53    /// 1.
54    SubCommand,
55    /// 2.
56    SubCommandGroup,
57    /// 3.
58    String,
59    /// 4: |x| < 2^53.
60    Integer,
61    /// 5.
62    Boolean,
63    /// 6: a member.
64    User,
65    /// 7.
66    Channel,
67    /// 8.
68    Role,
69    /// 9: a member or a role.
70    Mentionable,
71    /// 10: a double.
72    Number,
73    /// 11: an attachment the invoker uploaded.
74    Attachment,
75}
76
77impl TryFrom<u8> for OptionType {
78    type Error = String;
79    fn try_from(v: u8) -> Result<Self, String> {
80        use OptionType::{
81            Attachment, Boolean, Channel, Integer, Mentionable, Number, Role, String as S,
82            SubCommand, SubCommandGroup, User,
83        };
84        const ALL: [OptionType; 11] = [
85            SubCommand,
86            SubCommandGroup,
87            S,
88            Integer,
89            Boolean,
90            User,
91            Channel,
92            Role,
93            Mentionable,
94            Number,
95            Attachment,
96        ];
97        usize::from(v)
98            .checked_sub(1)
99            .and_then(|i| ALL.get(i).copied())
100            .ok_or_else(|| format!("unsupported option type {v}"))
101    }
102}
103
104impl From<OptionType> for u8 {
105    fn from(t: OptionType) -> Self {
106        t as Self + 1
107    }
108}
109
110/// A choice `value`: a string or a number.
111#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
112#[serde(untagged)]
113pub enum ChoiceValue {
114    /// For `STRING` options.
115    String(String),
116    /// For `INTEGER` and `NUMBER` options.
117    Number(f64),
118}
119
120/// A predefined choice of an option, or an autocomplete result.
121#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
122pub struct Choice {
123    /// 1–100 scalars.
124    pub name: String,
125    /// Localized names by BCP 47 tag.
126    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
127    pub name_localizations: BTreeMap<String, String>,
128    /// What the bot receives.
129    pub value: ChoiceValue,
130}
131
132/// An option of a command or subcommand.
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct CommandOption {
135    /// The kind of value.
136    #[serde(rename = "type")]
137    pub kind: OptionType,
138    /// 1–32, Discord's name pattern.
139    pub name: String,
140    /// Localized names.
141    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
142    pub name_localizations: BTreeMap<String, String>,
143    /// 1–100 scalars.
144    pub description: String,
145    /// Localized descriptions.
146    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
147    pub description_localizations: BTreeMap<String, String>,
148    /// Required options come before optional ones; never on subcommands.
149    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
150    pub required: bool,
151    /// ≤ 25 fixed choices (string, integer and number options).
152    #[serde(default, skip_serializing_if = "Vec::is_empty")]
153    pub choices: Vec<Choice>,
154    /// Subcommand and group children.
155    #[serde(default, skip_serializing_if = "Vec::is_empty")]
156    pub options: Vec<CommandOption>,
157    /// Channel options: the kinds offered.
158    #[serde(default, skip_serializing_if = "Vec::is_empty")]
159    pub channel_types: Vec<ChannelKind>,
160    /// Integer and number bounds.
161    #[serde(default, skip_serializing_if = "Option::is_none")]
162    pub min_value: Option<f64>,
163    /// Integer and number bounds.
164    #[serde(default, skip_serializing_if = "Option::is_none")]
165    pub max_value: Option<f64>,
166    /// String length bounds, 0–6 000.
167    #[serde(default, skip_serializing_if = "Option::is_none")]
168    pub min_length: Option<u32>,
169    /// String length bounds, 1–6 000.
170    #[serde(default, skip_serializing_if = "Option::is_none")]
171    pub max_length: Option<u32>,
172    /// The bot suggests values as the invoker types (not with `choices`).
173    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
174    pub autocomplete: bool,
175}
176
177/// An application command (§8.14).
178#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
179pub struct ApplicationCommand {
180    /// Host-assigned; absent in `command.set`.
181    #[serde(default, skip_serializing_if = "Option::is_none")]
182    pub command_id: Option<CommandId>,
183    /// The bot that owns it: in `command.list` and `command.updated`, absent elsewhere.
184    #[serde(default, skip_serializing_if = "Option::is_none")]
185    pub bot_id: Option<BotId>,
186    /// Slash, user or message command.
187    #[serde(rename = "type", default)]
188    pub kind: CommandType,
189    /// The name.
190    pub name: String,
191    /// Localized names.
192    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
193    pub name_localizations: BTreeMap<String, String>,
194    /// 1–100 scalars for slash commands; `""` for user and message commands.
195    #[serde(default)]
196    pub description: String,
197    /// Localized descriptions.
198    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
199    pub description_localizations: BTreeMap<String, String>,
200    /// Slash commands only, ≤ 25.
201    #[serde(default, skip_serializing_if = "Vec::is_empty")]
202    pub options: Vec<CommandOption>,
203    /// Bits the invoker needs in the channel; `"0"` is owner only; absent is everyone.
204    #[serde(default, skip_serializing_if = "Option::is_none")]
205    pub default_member_permissions: Option<Permissions>,
206}