use serde::{Deserialize, Serialize};
use serde_with::*;
#[cfg(feature = "builder")]
use crate::Builder;
use super::components::ComponentType;
use super::user::*;
use super::Snowflake;
use serde_repr::*;
#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommand {
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
pub id: Option<Snowflake>,
pub r#type: Option<ApplicationCommandType>,
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
application_id: Option<Snowflake>,
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
pub guild_id: Option<Snowflake>,
pub name: String,
description: String,
options: Option<Vec<ApplicationCommandOption>>,
default_permission: Option<bool>,
}
#[derive(Clone, Serialize_repr, Deserialize_repr, Debug, PartialEq)]
#[repr(u8)]
#[non_exhaustive]
pub enum ApplicationCommandType {
ChatInput = 1,
User = 2,
Message = 3,
}
impl Default for ApplicationCommand {
fn default() -> Self {
Self {
id: None,
r#type: Some(ApplicationCommandType::ChatInput),
application_id: None,
guild_id: None,
name: String::new(),
description: String::new(),
options: None,
default_permission: Some(true),
}
}
}
#[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, PartialEq)]
#[repr(u8)]
#[non_exhaustive]
pub enum ApplicationCommandPermissionType {
Role = 1,
User = 2,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommandPermissionBatch {
pub id: Snowflake,
pub permissions: Vec<ApplicationCommandPermission>,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommandPermission {
pub id: Snowflake,
pub r#type: ApplicationCommandPermissionType,
pub permission: bool,
}
#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommandOption {
r#type: ApplicationCommandOptionType,
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
name: Option<String>,
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
description: Option<String>,
#[serde_as(as = "Option<_>")]
#[serde(default)]
required: Option<bool>,
#[serde_as(as = "Option<Vec<_>>")]
#[serde(default)]
choices: Option<Vec<ApplicationCommandOptionChoice>>,
#[serde_as(as = "Option<Vec<_>>")]
#[serde(default)]
options: Option<Vec<ApplicationCommandOption>>,
}
impl Default for ApplicationCommandOption {
fn default() -> Self {
Self {
r#type: ApplicationCommandOptionType::String,
name: None,
description: None,
required: None,
choices: None,
options: None,
}
}
}
impl ApplicationCommandOption {
pub fn option_type(mut self, ty: &ApplicationCommandOptionType) -> Self {
self.r#type = ty.clone();
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
pub fn required(mut self, req: &bool) -> Self {
self.required = Some(*req);
self
}
pub fn add_choice(mut self, choice: &ApplicationCommandOptionChoice) -> Self {
match self.choices.as_mut() {
None => {
self.choices = Some(vec![choice.clone()]);
}
Some(o) => {
o.push(choice.clone());
}
}
self
}
pub fn add_option(mut self, opt: &ApplicationCommandOption) -> Self {
match self.options.as_mut() {
None => {
self.options = Some(vec![opt.clone()]);
}
Some(o) => {
o.push(opt.clone());
}
}
self
}
}
#[derive(Clone, Serialize_repr, Deserialize_repr, Debug, PartialEq)]
#[repr(u8)]
#[non_exhaustive]
pub enum ApplicationCommandOptionType {
SubCommand = 1,
SubCommandGroup = 2,
String = 3,
Integer = 4,
Boolean = 5,
User = 6,
Channel = 7,
Role = 8,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommandOptionChoice {
pub name: String,
pub value: String,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ResolvedData {
pub users: Option<User>,
pub members: Option<Member>,
}
#[serde_as]
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommandInteractionData {
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
pub id: Option<Snowflake>,
pub name: Option<String>,
pub r#type: Option<ApplicationCommandType>,
pub options: Option<Vec<ApplicationCommandInteractionDataOption>>,
pub component_type: Option<ComponentType>,
pub custom_id: Option<String>,
pub values: Option<Vec<ApplicationCommandOption>>,
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default)]
pub target_id: Option<Snowflake>,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ApplicationCommandInteractionDataOption {
pub name: String,
pub value: String,
pub options: Option<Vec<ApplicationCommandInteractionDataOption>>,
}
#[derive(Clone, Debug, Default)]
#[cfg(feature = "builder")]
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
pub struct SlashCommandDefinitionBuilder {
obj: ApplicationCommand,
}
#[cfg(feature = "builder")]
impl SlashCommandDefinitionBuilder {
pub fn name(mut self, name: impl ToString) -> Self {
let n = name.to_string();
self.obj.name = n;
self
}
pub fn command_type(mut self, c_type: ApplicationCommandType) -> Self {
self.obj.r#type = Some(c_type);
self
}
pub fn description(mut self, desc: impl ToString) -> Self {
let d = desc.to_string();
self.obj.description = d;
self
}
pub fn add_option(mut self, opt: ApplicationCommandOption) -> Self {
match self.obj.options.as_mut() {
None => {
self.obj.options = Some(vec![opt]);
}
Some(o) => {
o.push(opt);
}
}
self
}
pub fn default_permission(mut self, permission: bool) -> Self {
self.obj.default_permission = Some(permission);
self
}
#[deprecated(since = "0.1.9", note = "Use the `build()` function instead")]
pub fn finish(self) -> ApplicationCommand {
self.obj
}
}
#[cfg(feature = "builder")]
impl Builder<ApplicationCommand> for SlashCommandDefinitionBuilder {
type Error = std::convert::Infallible;
fn build(self) -> Result<ApplicationCommand, Self::Error> {
Ok(self.obj)
}
}