1use std::collections::BTreeMap;
8
9use compact_str::CompactString;
10use serde::{
11 Deserialize, Deserializer, Serialize, Serializer,
12 de::{Error as DeError, Unexpected, Visitor},
13};
14
15use crate::{
16 application::IntegrationType,
17 channel::ChannelType,
18 id::{ApplicationId, CommandId, CommandVersionId, GenericId, GuildId},
19 interaction::InteractionContextType,
20 macros::int_enum,
21 permissions::Permissions,
22 util::Locale,
23};
24
25#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
32#[non_exhaustive]
33pub struct ApplicationCommand {
34 pub id: CommandId,
36 #[serde(rename = "type", default)]
38 pub kind: CommandType,
39 pub application_id: ApplicationId,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub guild_id: Option<GuildId>,
44 pub name: CompactString,
46 #[serde(
48 default,
49 skip_serializing_if = "BTreeMap::is_empty",
50 deserialize_with = "crate::serde_util::null_as_default"
51 )]
52 pub name_localizations: BTreeMap<Locale, CompactString>,
53 #[serde(default)]
55 pub description: CompactString,
56 #[serde(
58 default,
59 skip_serializing_if = "BTreeMap::is_empty",
60 deserialize_with = "crate::serde_util::null_as_default"
61 )]
62 pub description_localizations: BTreeMap<Locale, CompactString>,
63 #[serde(default, skip_serializing_if = "Option::is_none")]
67 pub name_localized: Option<CompactString>,
68 #[serde(default, skip_serializing_if = "Option::is_none")]
70 pub description_localized: Option<CompactString>,
71 #[serde(
73 default,
74 skip_serializing_if = "Vec::is_empty",
75 deserialize_with = "crate::serde_util::null_as_default"
76 )]
77 pub options: Vec<CommandOption>,
78 #[serde(default)]
81 pub default_member_permissions: Option<Permissions>,
82 #[serde(default, skip_serializing_if = "Option::is_none")]
85 pub dm_permission: Option<bool>,
86 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub default_permission: Option<bool>,
90 #[serde(default, skip_serializing_if = "core::ops::Not::not")]
92 pub nsfw: bool,
93 #[serde(
95 default,
96 skip_serializing_if = "Vec::is_empty",
97 deserialize_with = "crate::serde_util::null_as_default"
98 )]
99 pub integration_types: Vec<IntegrationType>,
100 #[serde(
102 default,
103 skip_serializing_if = "Vec::is_empty",
104 deserialize_with = "crate::serde_util::null_as_default"
105 )]
106 pub contexts: Vec<InteractionContextType>,
107 pub version: CommandVersionId,
109 #[serde(default, skip_serializing_if = "Option::is_none")]
111 pub handler: Option<EntryPointHandlerType>,
112}
113
114int_enum! {
115 #[doc(alias = "ApplicationCommandType")]
119 #[derive(Default)]
120 pub enum CommandType: u8 {
121 #[default]
123 ChatInput = 1,
124 User = 2,
126 Message = 3,
128 PrimaryEntryPoint = 4,
130 }
131}
132
133int_enum! {
134 #[doc(alias = "EntryPointCommandHandlerType")]
138 pub enum EntryPointHandlerType: u8 {
139 AppHandler = 1,
141 DiscordLaunchActivity = 2,
143 }
144}
145
146#[derive(Clone, Debug, PartialEq)]
151#[non_exhaustive]
152#[doc(alias = "ApplicationCommandOption")]
153pub enum CommandOption {
154 SubCommand(SubCommandOption),
156 SubCommandGroup(SubCommandOption),
158 String(StringOption),
160 Integer(IntegerOption),
162 Boolean(BasicOption),
164 User(BasicOption),
166 Channel(ChannelOption),
168 Role(BasicOption),
170 Mentionable(BasicOption),
172 Number(NumberOption),
174 Attachment(AttachmentOption),
176 Unknown(UnknownOption),
178}
179
180#[derive(Clone, Copy, Debug)]
184#[non_exhaustive]
185pub struct OptionInfo<'a> {
186 pub name: &'a str,
188 pub name_localizations: &'a BTreeMap<Locale, CompactString>,
190 pub description: &'a str,
192 pub description_localizations: &'a BTreeMap<Locale, CompactString>,
194 pub name_localized: Option<&'a str>,
196 pub description_localized: Option<&'a str>,
198}
199
200macro_rules! common_fields {
201 ($self:ident) => {
202 match $self {
203 Self::SubCommand(option) | Self::SubCommandGroup(option) => OptionInfo {
204 name: &option.name,
205 name_localizations: &option.name_localizations,
206 description: &option.description,
207 description_localizations: &option.description_localizations,
208 name_localized: option.name_localized.as_deref(),
209 description_localized: option.description_localized.as_deref(),
210 },
211 Self::String(option) => OptionInfo {
212 name: &option.name,
213 name_localizations: &option.name_localizations,
214 description: &option.description,
215 description_localizations: &option.description_localizations,
216 name_localized: option.name_localized.as_deref(),
217 description_localized: option.description_localized.as_deref(),
218 },
219 Self::Integer(option) => OptionInfo {
220 name: &option.name,
221 name_localizations: &option.name_localizations,
222 description: &option.description,
223 description_localizations: &option.description_localizations,
224 name_localized: option.name_localized.as_deref(),
225 description_localized: option.description_localized.as_deref(),
226 },
227 Self::Boolean(option)
228 | Self::User(option)
229 | Self::Role(option)
230 | Self::Mentionable(option) => OptionInfo {
231 name: &option.name,
232 name_localizations: &option.name_localizations,
233 description: &option.description,
234 description_localizations: &option.description_localizations,
235 name_localized: option.name_localized.as_deref(),
236 description_localized: option.description_localized.as_deref(),
237 },
238 Self::Channel(option) => OptionInfo {
239 name: &option.name,
240 name_localizations: &option.name_localizations,
241 description: &option.description,
242 description_localizations: &option.description_localizations,
243 name_localized: option.name_localized.as_deref(),
244 description_localized: option.description_localized.as_deref(),
245 },
246 Self::Number(option) => OptionInfo {
247 name: &option.name,
248 name_localizations: &option.name_localizations,
249 description: &option.description,
250 description_localizations: &option.description_localizations,
251 name_localized: option.name_localized.as_deref(),
252 description_localized: option.description_localized.as_deref(),
253 },
254 Self::Attachment(option) => OptionInfo {
255 name: &option.name,
256 name_localizations: &option.name_localizations,
257 description: &option.description,
258 description_localizations: &option.description_localizations,
259 name_localized: option.name_localized.as_deref(),
260 description_localized: option.description_localized.as_deref(),
261 },
262 Self::Unknown(option) => OptionInfo {
263 name: &option.name,
264 name_localizations: &option.name_localizations,
265 description: &option.description,
266 description_localizations: &option.description_localizations,
267 name_localized: option.name_localized.as_deref(),
268 description_localized: option.description_localized.as_deref(),
269 },
270 }
271 };
272}
273
274impl CommandOption {
275 #[must_use]
279 pub const fn kind(&self) -> CommandOptionType {
280 match self {
281 Self::SubCommand(_) => CommandOptionType::SubCommand,
282 Self::SubCommandGroup(_) => CommandOptionType::SubCommandGroup,
283 Self::String(_) => CommandOptionType::String,
284 Self::Integer(_) => CommandOptionType::Integer,
285 Self::Boolean(_) => CommandOptionType::Boolean,
286 Self::User(_) => CommandOptionType::User,
287 Self::Channel(_) => CommandOptionType::Channel,
288 Self::Role(_) => CommandOptionType::Role,
289 Self::Mentionable(_) => CommandOptionType::Mentionable,
290 Self::Number(_) => CommandOptionType::Number,
291 Self::Attachment(_) => CommandOptionType::Attachment,
292 Self::Unknown(option) => option.kind,
293 }
294 }
295
296 #[must_use]
300 pub fn common(&self) -> OptionInfo<'_> {
301 common_fields!(self)
302 }
303
304 #[must_use]
308 pub fn name(&self) -> &str {
309 self.common().name
310 }
311
312 #[must_use]
316 pub fn options(&self) -> &[Self] {
317 match self {
318 Self::SubCommand(option) | Self::SubCommandGroup(option) => &option.options,
319 _ => &[],
320 }
321 }
322}
323
324int_enum! {
325 #[doc(alias = "ApplicationCommandOptionType")]
329 pub enum CommandOptionType: u8 {
330 SubCommand = 1,
332 SubCommandGroup = 2,
334 String = 3,
336 Integer = 4,
338 Boolean = 5,
340 User = 6,
342 Channel = 7,
344 Role = 8,
346 Mentionable = 9,
348 Number = 10,
350 Attachment = 11,
352 }
353}
354
355#[derive(Clone, Debug, PartialEq, Serialize)]
359#[non_exhaustive]
360pub struct SubCommandOption {
361 pub name: CompactString,
363 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
365 pub name_localizations: BTreeMap<Locale, CompactString>,
366 pub description: CompactString,
368 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
370 pub description_localizations: BTreeMap<Locale, CompactString>,
371 #[serde(skip_serializing_if = "Option::is_none")]
374 pub name_localized: Option<CompactString>,
375 #[serde(skip_serializing_if = "Option::is_none")]
377 pub description_localized: Option<CompactString>,
378 #[serde(skip_serializing_if = "Vec::is_empty")]
380 pub options: Vec<CommandOption>,
381}
382
383impl SubCommandOption {
384 #[must_use]
390 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
391 Self {
392 name: name.into(),
393 name_localizations: BTreeMap::new(),
394 description: description.into(),
395 description_localizations: BTreeMap::new(),
396 name_localized: None,
397 description_localized: None,
398 options: Vec::new(),
399 }
400 }
401}
402
403#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
407#[non_exhaustive]
408pub struct StringOption {
409 pub name: CompactString,
411 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
413 pub name_localizations: BTreeMap<Locale, CompactString>,
414 pub description: CompactString,
416 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
418 pub description_localizations: BTreeMap<Locale, CompactString>,
419 #[serde(skip_serializing_if = "Option::is_none")]
422 pub name_localized: Option<CompactString>,
423 #[serde(skip_serializing_if = "Option::is_none")]
425 pub description_localized: Option<CompactString>,
426 #[serde(skip_serializing_if = "core::ops::Not::not")]
428 pub required: bool,
429 #[serde(skip_serializing_if = "Vec::is_empty")]
431 pub choices: Vec<Choice<CompactString>>,
432 #[serde(skip_serializing_if = "Option::is_none")]
434 pub min_length: Option<u16>,
435 #[serde(skip_serializing_if = "Option::is_none")]
437 pub max_length: Option<u16>,
438 #[serde(skip_serializing_if = "core::ops::Not::not")]
441 pub autocomplete: bool,
442}
443
444impl StringOption {
445 #[must_use]
451 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
452 Self {
453 name: name.into(),
454 name_localizations: BTreeMap::new(),
455 description: description.into(),
456 description_localizations: BTreeMap::new(),
457 name_localized: None,
458 description_localized: None,
459 required: false,
460 choices: Vec::new(),
461 min_length: None,
462 max_length: None,
463 autocomplete: false,
464 }
465 }
466}
467
468#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
472#[non_exhaustive]
473pub struct IntegerOption {
474 pub name: CompactString,
476 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
478 pub name_localizations: BTreeMap<Locale, CompactString>,
479 pub description: CompactString,
481 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
483 pub description_localizations: BTreeMap<Locale, CompactString>,
484 #[serde(skip_serializing_if = "Option::is_none")]
487 pub name_localized: Option<CompactString>,
488 #[serde(skip_serializing_if = "Option::is_none")]
490 pub description_localized: Option<CompactString>,
491 #[serde(skip_serializing_if = "core::ops::Not::not")]
493 pub required: bool,
494 #[serde(skip_serializing_if = "Vec::is_empty")]
496 pub choices: Vec<Choice<i64>>,
497 #[serde(skip_serializing_if = "Option::is_none")]
499 pub min_value: Option<i64>,
500 #[serde(skip_serializing_if = "Option::is_none")]
502 pub max_value: Option<i64>,
503 #[serde(skip_serializing_if = "core::ops::Not::not")]
505 pub autocomplete: bool,
506}
507
508impl IntegerOption {
509 #[must_use]
515 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
516 Self {
517 name: name.into(),
518 name_localizations: BTreeMap::new(),
519 description: description.into(),
520 description_localizations: BTreeMap::new(),
521 name_localized: None,
522 description_localized: None,
523 required: false,
524 choices: Vec::new(),
525 min_value: None,
526 max_value: None,
527 autocomplete: false,
528 }
529 }
530}
531
532#[derive(Clone, Debug, PartialEq, Serialize)]
536#[non_exhaustive]
537pub struct NumberOption {
538 pub name: CompactString,
540 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
542 pub name_localizations: BTreeMap<Locale, CompactString>,
543 pub description: CompactString,
545 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
547 pub description_localizations: BTreeMap<Locale, CompactString>,
548 #[serde(skip_serializing_if = "Option::is_none")]
551 pub name_localized: Option<CompactString>,
552 #[serde(skip_serializing_if = "Option::is_none")]
554 pub description_localized: Option<CompactString>,
555 #[serde(skip_serializing_if = "core::ops::Not::not")]
557 pub required: bool,
558 #[serde(skip_serializing_if = "Vec::is_empty")]
560 pub choices: Vec<Choice<f64>>,
561 #[serde(skip_serializing_if = "Option::is_none")]
563 pub min_value: Option<f64>,
564 #[serde(skip_serializing_if = "Option::is_none")]
566 pub max_value: Option<f64>,
567 #[serde(skip_serializing_if = "core::ops::Not::not")]
569 pub autocomplete: bool,
570}
571
572impl NumberOption {
573 #[must_use]
579 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
580 Self {
581 name: name.into(),
582 name_localizations: BTreeMap::new(),
583 description: description.into(),
584 description_localizations: BTreeMap::new(),
585 name_localized: None,
586 description_localized: None,
587 required: false,
588 choices: Vec::new(),
589 min_value: None,
590 max_value: None,
591 autocomplete: false,
592 }
593 }
594}
595
596#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
600#[non_exhaustive]
601pub struct BasicOption {
602 pub name: CompactString,
604 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
606 pub name_localizations: BTreeMap<Locale, CompactString>,
607 pub description: CompactString,
609 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
611 pub description_localizations: BTreeMap<Locale, CompactString>,
612 #[serde(skip_serializing_if = "Option::is_none")]
615 pub name_localized: Option<CompactString>,
616 #[serde(skip_serializing_if = "Option::is_none")]
618 pub description_localized: Option<CompactString>,
619 #[serde(skip_serializing_if = "core::ops::Not::not")]
621 pub required: bool,
622}
623
624impl BasicOption {
625 #[must_use]
631 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
632 Self {
633 name: name.into(),
634 name_localizations: BTreeMap::new(),
635 description: description.into(),
636 description_localizations: BTreeMap::new(),
637 name_localized: None,
638 description_localized: None,
639 required: false,
640 }
641 }
642}
643
644#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
648#[non_exhaustive]
649pub struct ChannelOption {
650 pub name: CompactString,
652 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
654 pub name_localizations: BTreeMap<Locale, CompactString>,
655 pub description: CompactString,
657 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
659 pub description_localizations: BTreeMap<Locale, CompactString>,
660 #[serde(skip_serializing_if = "Option::is_none")]
663 pub name_localized: Option<CompactString>,
664 #[serde(skip_serializing_if = "Option::is_none")]
666 pub description_localized: Option<CompactString>,
667 #[serde(skip_serializing_if = "core::ops::Not::not")]
669 pub required: bool,
670 #[serde(skip_serializing_if = "Vec::is_empty")]
672 pub channel_types: Vec<ChannelType>,
673}
674
675impl ChannelOption {
676 #[must_use]
682 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
683 Self {
684 name: name.into(),
685 name_localizations: BTreeMap::new(),
686 description: description.into(),
687 description_localizations: BTreeMap::new(),
688 name_localized: None,
689 description_localized: None,
690 required: false,
691 channel_types: Vec::new(),
692 }
693 }
694}
695
696#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
700#[non_exhaustive]
701pub struct AttachmentOption {
702 pub name: CompactString,
704 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
706 pub name_localizations: BTreeMap<Locale, CompactString>,
707 pub description: CompactString,
709 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
711 pub description_localizations: BTreeMap<Locale, CompactString>,
712 #[serde(skip_serializing_if = "Option::is_none")]
715 pub name_localized: Option<CompactString>,
716 #[serde(skip_serializing_if = "Option::is_none")]
718 pub description_localized: Option<CompactString>,
719 #[serde(skip_serializing_if = "core::ops::Not::not")]
721 pub required: bool,
722 #[serde(skip_serializing_if = "Vec::is_empty")]
725 pub file_types: Vec<CompactString>,
726}
727
728impl AttachmentOption {
729 #[must_use]
735 pub fn new(name: impl Into<CompactString>, description: impl Into<CompactString>) -> Self {
736 Self {
737 name: name.into(),
738 name_localizations: BTreeMap::new(),
739 description: description.into(),
740 description_localizations: BTreeMap::new(),
741 name_localized: None,
742 description_localized: None,
743 required: false,
744 file_types: Vec::new(),
745 }
746 }
747}
748
749#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
754#[non_exhaustive]
755pub struct UnknownOption {
756 #[serde(skip)]
758 pub kind: CommandOptionType,
759 pub name: CompactString,
761 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
763 pub name_localizations: BTreeMap<Locale, CompactString>,
764 pub description: CompactString,
766 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
768 pub description_localizations: BTreeMap<Locale, CompactString>,
769 #[serde(skip_serializing_if = "Option::is_none")]
772 pub name_localized: Option<CompactString>,
773 #[serde(skip_serializing_if = "Option::is_none")]
775 pub description_localized: Option<CompactString>,
776 #[serde(skip_serializing_if = "core::ops::Not::not")]
778 pub required: bool,
779}
780
781#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
786#[non_exhaustive]
787#[doc(alias = "ApplicationCommandOptionChoice")]
788pub struct Choice<T> {
789 pub name: CompactString,
791 #[serde(
793 default,
794 skip_serializing_if = "BTreeMap::is_empty",
795 deserialize_with = "crate::serde_util::null_as_default"
796 )]
797 pub name_localizations: BTreeMap<Locale, CompactString>,
798 pub value: T,
800}
801
802impl<T> Choice<T> {
803 #[must_use]
807 pub fn new(name: impl Into<CompactString>, value: T) -> Self {
808 Self {
809 name: name.into(),
810 name_localizations: BTreeMap::new(),
811 value,
812 }
813 }
814}
815
816#[derive(Clone, Debug, PartialEq)]
819pub(crate) enum RawValue {
820 String(CompactString),
821 Integer(i64),
822 Float(f64),
823}
824
825impl<'de> Deserialize<'de> for RawValue {
826 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
827 struct RawValueVisitor;
828
829 impl Visitor<'_> for RawValueVisitor {
830 type Value = RawValue;
831
832 fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
833 f.write_str("a string or a number")
834 }
835
836 fn visit_i64<E: DeError>(self, value: i64) -> Result<RawValue, E> {
837 Ok(RawValue::Integer(value))
838 }
839
840 fn visit_u64<E: DeError>(self, value: u64) -> Result<RawValue, E> {
841 i64::try_from(value)
842 .map(RawValue::Integer)
843 .map_err(|_| E::invalid_value(Unexpected::Unsigned(value), &self))
844 }
845
846 fn visit_f64<E: DeError>(self, value: f64) -> Result<RawValue, E> {
847 Ok(RawValue::Float(value))
848 }
849
850 fn visit_str<E: DeError>(self, value: &str) -> Result<RawValue, E> {
851 Ok(RawValue::String(value.into()))
852 }
853 }
854
855 deserializer.deserialize_any(RawValueVisitor)
856 }
857}
858
859impl RawValue {
860 pub(crate) fn into_string<E: DeError>(self) -> Result<CompactString, E> {
861 match self {
862 Self::String(value) => Ok(value),
863 Self::Integer(value) => Err(E::invalid_type(Unexpected::Signed(value), &"a string")),
864 Self::Float(value) => Err(E::invalid_type(Unexpected::Float(value), &"a string")),
865 }
866 }
867
868 pub(crate) fn into_integer<E: DeError>(self) -> Result<i64, E> {
869 match self {
870 Self::Integer(value) => Ok(value),
871 Self::String(value) => Err(E::invalid_type(Unexpected::Str(&value), &"an integer")),
872 Self::Float(value) => Err(E::invalid_type(Unexpected::Float(value), &"an integer")),
873 }
874 }
875
876 #[expect(
877 clippy::cast_precision_loss,
878 reason = "Discord limits command numbers to ±2^53, which f64 represents exactly"
879 )]
880 pub(crate) fn into_number<E: DeError>(self) -> Result<f64, E> {
881 match self {
882 Self::Integer(value) => Ok(value as f64),
883 Self::Float(value) => Ok(value),
884 Self::String(value) => Err(E::invalid_type(Unexpected::Str(&value), &"a number")),
885 }
886 }
887}
888
889pub(crate) fn convert_choices<T, E: DeError>(
890 choices: Vec<Choice<RawValue>>,
891 convert: impl Fn(RawValue) -> Result<T, E>,
892) -> Result<Vec<Choice<T>>, E> {
893 choices
894 .into_iter()
895 .map(|choice| {
896 Ok(Choice {
897 name: choice.name,
898 name_localizations: choice.name_localizations,
899 value: convert(choice.value)?,
900 })
901 })
902 .collect()
903}
904
905#[derive(Deserialize)]
907struct RawOption {
908 #[serde(rename = "type")]
909 kind: CommandOptionType,
910 name: CompactString,
911 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
912 name_localizations: BTreeMap<Locale, CompactString>,
913 #[serde(default)]
914 description: CompactString,
915 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
916 description_localizations: BTreeMap<Locale, CompactString>,
917 #[serde(default)]
918 name_localized: Option<CompactString>,
919 #[serde(default)]
920 description_localized: Option<CompactString>,
921 #[serde(default)]
922 required: bool,
923 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
924 choices: Vec<Choice<RawValue>>,
925 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
926 options: Vec<CommandOption>,
927 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
928 channel_types: Vec<ChannelType>,
929 #[serde(default)]
930 min_value: Option<RawValue>,
931 #[serde(default)]
932 max_value: Option<RawValue>,
933 #[serde(default)]
934 min_length: Option<u16>,
935 #[serde(default)]
936 max_length: Option<u16>,
937 #[serde(default)]
938 autocomplete: bool,
939 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
940 file_types: Vec<CompactString>,
941}
942
943impl RawOption {
944 fn into_option<E: DeError>(self) -> Result<CommandOption, E> {
945 let basic = |raw: Self| BasicOption {
946 name: raw.name,
947 name_localizations: raw.name_localizations,
948 description: raw.description,
949 description_localizations: raw.description_localizations,
950 name_localized: raw.name_localized,
951 description_localized: raw.description_localized,
952 required: raw.required,
953 };
954 Ok(match self.kind {
955 kind @ (CommandOptionType::SubCommand | CommandOptionType::SubCommandGroup) => {
956 let option = SubCommandOption {
957 name: self.name,
958 name_localizations: self.name_localizations,
959 description: self.description,
960 description_localizations: self.description_localizations,
961 name_localized: self.name_localized,
962 description_localized: self.description_localized,
963 options: self.options,
964 };
965 if kind == CommandOptionType::SubCommand {
966 CommandOption::SubCommand(option)
967 } else {
968 CommandOption::SubCommandGroup(option)
969 }
970 }
971 CommandOptionType::String => CommandOption::String(StringOption {
972 choices: convert_choices(self.choices, RawValue::into_string)?,
973 name: self.name,
974 name_localizations: self.name_localizations,
975 description: self.description,
976 description_localizations: self.description_localizations,
977 name_localized: self.name_localized,
978 description_localized: self.description_localized,
979 required: self.required,
980 min_length: self.min_length,
981 max_length: self.max_length,
982 autocomplete: self.autocomplete,
983 }),
984 CommandOptionType::Integer => CommandOption::Integer(IntegerOption {
985 choices: convert_choices(self.choices, RawValue::into_integer)?,
986 min_value: self.min_value.map(RawValue::into_integer).transpose()?,
987 max_value: self.max_value.map(RawValue::into_integer).transpose()?,
988 name: self.name,
989 name_localizations: self.name_localizations,
990 description: self.description,
991 description_localizations: self.description_localizations,
992 name_localized: self.name_localized,
993 description_localized: self.description_localized,
994 required: self.required,
995 autocomplete: self.autocomplete,
996 }),
997 CommandOptionType::Number => CommandOption::Number(NumberOption {
998 choices: convert_choices(self.choices, RawValue::into_number)?,
999 min_value: self.min_value.map(RawValue::into_number).transpose()?,
1000 max_value: self.max_value.map(RawValue::into_number).transpose()?,
1001 name: self.name,
1002 name_localizations: self.name_localizations,
1003 description: self.description,
1004 description_localizations: self.description_localizations,
1005 name_localized: self.name_localized,
1006 description_localized: self.description_localized,
1007 required: self.required,
1008 autocomplete: self.autocomplete,
1009 }),
1010 CommandOptionType::Boolean => CommandOption::Boolean(basic(self)),
1011 CommandOptionType::User => CommandOption::User(basic(self)),
1012 CommandOptionType::Role => CommandOption::Role(basic(self)),
1013 CommandOptionType::Mentionable => CommandOption::Mentionable(basic(self)),
1014 CommandOptionType::Channel => CommandOption::Channel(ChannelOption {
1015 name: self.name,
1016 name_localizations: self.name_localizations,
1017 description: self.description,
1018 description_localizations: self.description_localizations,
1019 name_localized: self.name_localized,
1020 description_localized: self.description_localized,
1021 required: self.required,
1022 channel_types: self.channel_types,
1023 }),
1024 CommandOptionType::Attachment => CommandOption::Attachment(AttachmentOption {
1025 name: self.name,
1026 name_localizations: self.name_localizations,
1027 description: self.description,
1028 description_localizations: self.description_localizations,
1029 name_localized: self.name_localized,
1030 description_localized: self.description_localized,
1031 required: self.required,
1032 file_types: self.file_types,
1033 }),
1034 kind @ CommandOptionType::Unknown(_) => CommandOption::Unknown(UnknownOption {
1035 kind,
1036 name: self.name,
1037 name_localizations: self.name_localizations,
1038 description: self.description,
1039 description_localizations: self.description_localizations,
1040 name_localized: self.name_localized,
1041 description_localized: self.description_localized,
1042 required: self.required,
1043 }),
1044 })
1045 }
1046}
1047
1048impl<'de> Deserialize<'de> for CommandOption {
1049 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1050 RawOption::deserialize(deserializer)?.into_option::<D::Error>()
1051 }
1052}
1053
1054#[derive(Serialize)]
1056struct Tagged<'a, T> {
1057 #[serde(rename = "type")]
1058 kind: CommandOptionType,
1059 #[serde(flatten)]
1060 option: &'a T,
1061}
1062
1063impl Serialize for CommandOption {
1064 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1065 let kind = self.kind();
1066 match self {
1067 Self::SubCommand(option) | Self::SubCommandGroup(option) => {
1068 Tagged { kind, option }.serialize(serializer)
1069 }
1070 Self::String(option) => Tagged { kind, option }.serialize(serializer),
1071 Self::Integer(option) => Tagged { kind, option }.serialize(serializer),
1072 Self::Boolean(option)
1073 | Self::User(option)
1074 | Self::Role(option)
1075 | Self::Mentionable(option) => Tagged { kind, option }.serialize(serializer),
1076 Self::Channel(option) => Tagged { kind, option }.serialize(serializer),
1077 Self::Number(option) => Tagged { kind, option }.serialize(serializer),
1078 Self::Attachment(option) => Tagged { kind, option }.serialize(serializer),
1079 Self::Unknown(option) => Tagged { kind, option }.serialize(serializer),
1080 }
1081 }
1082}
1083
1084#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1088#[non_exhaustive]
1089#[doc(alias = "GuildApplicationCommandPermissions")]
1090pub struct GuildCommandPermissions {
1091 pub id: GenericId,
1094 pub application_id: ApplicationId,
1096 pub guild_id: GuildId,
1098 pub permissions: Vec<CommandPermission>,
1100}
1101
1102#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1107#[non_exhaustive]
1108#[doc(alias = "ApplicationCommandPermissions")]
1109pub struct CommandPermission {
1110 pub id: GenericId,
1113 #[serde(rename = "type")]
1115 pub kind: CommandPermissionType,
1116 pub permission: bool,
1118}
1119
1120impl CommandPermission {
1121 #[must_use]
1125 pub const fn new(id: GenericId, kind: CommandPermissionType, permission: bool) -> Self {
1126 Self {
1127 id,
1128 kind,
1129 permission,
1130 }
1131 }
1132
1133 #[must_use]
1137 pub const fn everyone(guild_id: GuildId) -> GenericId {
1138 guild_id.cast()
1139 }
1140
1141 #[must_use]
1146 pub const fn all_channels(guild_id: GuildId) -> Option<GenericId> {
1147 GenericId::new_checked(guild_id.get() - 1)
1148 }
1149}
1150
1151int_enum! {
1152 #[doc(alias = "ApplicationCommandPermissionType")]
1156 pub enum CommandPermissionType: u8 {
1157 Role = 1,
1159 User = 2,
1161 Channel = 3,
1163 }
1164}