zaun_community_protocol/command/object/
app_command.rs1mod 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#[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 #[default]
24 ChatInput,
25 User,
27 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
51#[serde(try_from = "u8", into = "u8")]
52pub enum OptionType {
53 SubCommand,
55 SubCommandGroup,
57 String,
59 Integer,
61 Boolean,
63 User,
65 Channel,
67 Role,
69 Mentionable,
71 Number,
73 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
112#[serde(untagged)]
113pub enum ChoiceValue {
114 String(String),
116 Number(f64),
118}
119
120#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
122pub struct Choice {
123 pub name: String,
125 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
127 pub name_localizations: BTreeMap<String, String>,
128 pub value: ChoiceValue,
130}
131
132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct CommandOption {
135 #[serde(rename = "type")]
137 pub kind: OptionType,
138 pub name: String,
140 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
142 pub name_localizations: BTreeMap<String, String>,
143 pub description: String,
145 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
147 pub description_localizations: BTreeMap<String, String>,
148 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
150 pub required: bool,
151 #[serde(default, skip_serializing_if = "Vec::is_empty")]
153 pub choices: Vec<Choice>,
154 #[serde(default, skip_serializing_if = "Vec::is_empty")]
156 pub options: Vec<CommandOption>,
157 #[serde(default, skip_serializing_if = "Vec::is_empty")]
159 pub channel_types: Vec<ChannelKind>,
160 #[serde(default, skip_serializing_if = "Option::is_none")]
162 pub min_value: Option<f64>,
163 #[serde(default, skip_serializing_if = "Option::is_none")]
165 pub max_value: Option<f64>,
166 #[serde(default, skip_serializing_if = "Option::is_none")]
168 pub min_length: Option<u32>,
169 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub max_length: Option<u32>,
172 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
174 pub autocomplete: bool,
175}
176
177#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
179pub struct ApplicationCommand {
180 #[serde(default, skip_serializing_if = "Option::is_none")]
182 pub command_id: Option<CommandId>,
183 #[serde(default, skip_serializing_if = "Option::is_none")]
185 pub bot_id: Option<BotId>,
186 #[serde(rename = "type", default)]
188 pub kind: CommandType,
189 pub name: String,
191 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
193 pub name_localizations: BTreeMap<String, String>,
194 #[serde(default)]
196 pub description: String,
197 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
199 pub description_localizations: BTreeMap<String, String>,
200 #[serde(default, skip_serializing_if = "Vec::is_empty")]
202 pub options: Vec<CommandOption>,
203 #[serde(default, skip_serializing_if = "Option::is_none")]
205 pub default_member_permissions: Option<Permissions>,
206}