|
- use serde::{Deserialize, Serialize};
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct InteractionRaw {
- #[serde(rename = "type")]
- pub type_: usize,
- pub token: Option<String>,
- pub member: Option<GuildMember>,
- pub id: Option<String>,
- pub guild_id: Option<String>,
- pub data: Option<ApplicationCommandInteractionData>,
- pub channel_id: Option<String>,
- pub version: Option<usize>,
- }
-
- impl InteractionRaw {
- pub fn interaction(&self) -> Interaction {
- match self.type_ {
- 1 => Interaction::Ping {},
- 2 => Interaction::Command {
- token: self.token.clone().unwrap(),
- member: self.member.clone().unwrap(),
- id: self.id.clone().unwrap(),
- guild_id: self.guild_id.clone().unwrap(),
- data: self.data.clone().unwrap(),
- channel_id: self.channel_id.clone().unwrap(),
- version: self.version.unwrap_or(1),
- },
- _ => Interaction::Invalid {},
- }
- }
- }
-
- pub enum Interaction {
- //#[serde(rename = "1")]
- Ping {},
- //#[serde(rename = "2")]
- Command {
- token: String,
- member: GuildMember,
- id: String,
- guild_id: String,
- data: ApplicationCommandInteractionData,
- channel_id: String,
- version: usize,
- },
- Invalid {},
- }
-
- impl Interaction {
- pub fn is_ping(&self) -> bool {
- match self {
- Self::Ping {..} => true,
- Self::Command {..} => false,
- Self::Invalid {..} => false,
- }
- }
-
- pub fn is_command(&self) -> bool {
- match self {
- Self::Ping {..} => false,
- Self::Command {..} => true,
- Self::Invalid {..} => false,
- }
- }
-
- pub fn is_invalid(&self) -> bool {
- match self {
- Self::Ping {..} => false,
- Self::Command {..} => false,
- Self::Invalid {..} => true,
- }
- }
-
- pub fn cmd(&self) -> Option<InteractionCommand> {
- match self {
- Self::Ping {..} => None,
- Self::Command { token, member, id, guild_id, data, channel_id, version } => Some(InteractionCommand {
- token: token.to_string(),
- member: member.clone(),
- id: id.to_string(),
- guild_id: guild_id.to_string(),
- data: data.clone(),
- channel_id: channel_id.to_string(),
- version: *version,
- }),
- Self::Invalid {..} => None,
- }
- }
- }
-
- pub struct InteractionCommand {
- pub token: String,
- pub member: GuildMember,
- pub id: String,
- pub guild_id: String,
- pub data: ApplicationCommandInteractionData,
- pub channel_id: String,
- pub version: usize,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct GuildMember {
- pub user: Option<User>,
- pub nick: Option<String>,
- pub role: Option<Vec<String>>,
- pub joined_at: String,
- pub premium_since: Option<String>,
- pub deaf: bool,
- pub mute: bool,
- pub pending: Option<bool>,
- pub permissions: Option<String>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct User {
- pub id: String,
- pub username: String,
- pub discriminator: String,
- pub avatar: Option<String>,
- pub bot: Option<bool>,
- pub system: Option<bool>,
- pub mfa_enabled: Option<bool>,
- pub locale: Option<String>,
- pub verified: Option<String>,
- pub email: Option<String>,
- pub flags: Option<usize>,
- pub premium_type: Option<usize>,
- pub public_flags: Option<usize>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct ApplicationCommandInteractionData {
- pub id: String,
- pub name: String,
- pub options: Option<Vec<ApplicationCommandInteractionDataOption>>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct ApplicationCommandInteractionDataOption {
- pub name: String,
- pub value: Option<CommandValue>, // FIXME this could be bool, integer, or sub-command as well
- pub options: Option<Vec<ApplicationCommandInteractionDataOption>>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- #[serde(untagged)]
- pub enum CommandValue {
- BoolVal(bool),
- IntVal(usize),
- StrVal(String),
- }
-
- impl ToString for CommandValue {
- fn to_string(&self) -> String {
- match self {
- Self::StrVal(v) => v.to_string(),
- Self::BoolVal(v) => v.to_string(),
- Self::IntVal(v) => v.to_string(),
- }
- }
- }
-
- pub enum InteractionResponse {
- //#[serde(rename = "1")]
- Pong {},
- //#[serde(rename = "2")]
- Acknowledge {},
- //#[serde(rename = "3")]
- ChannelMessage {
- data: Option<InteractionApplicationCommandCallbackData>,
- },
- //#[serde(rename = "4")]
- ChannelMessageWithSource {
- data: Option<InteractionApplicationCommandCallbackData>,
- },
- //#[serde(rename = "5")]
- AcknowledgeWithSource {},
- }
-
- impl InteractionResponse {
- pub fn raw(&self) -> InteractionResponseRaw {
- match self {
- InteractionResponse::Pong { .. } => InteractionResponseRaw {type_: 1, data:None},
- InteractionResponse::Acknowledge { .. } => InteractionResponseRaw {type_: 2, data:None},
- InteractionResponse::ChannelMessage { data, .. } => InteractionResponseRaw {type_: 3, data: data.clone()},
- InteractionResponse::ChannelMessageWithSource { data, .. } => InteractionResponseRaw {type_: 4, data: data.clone()},
- InteractionResponse::AcknowledgeWithSource { .. } => InteractionResponseRaw {type_: 5, data:None},
- }
- }
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct InteractionResponseRaw {
- #[serde(rename = "type")]
- pub type_: usize,
- pub data: Option<InteractionApplicationCommandCallbackData>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct InteractionApplicationCommandCallbackData {
- pub tts: bool,
- pub content: String,
- pub embeds: Option<Vec<Embed>>,
- pub allowed_mentions: Option<String>,
- pub flags: Option<usize>,
- }
-
- // embed and sub-objects
- #[derive(Serialize, Deserialize, Clone)]
- pub struct Embed {
- pub title: Option<String>,
- #[serde(rename = "type")]
- pub type_: Option<String>,
- pub description: Option<String>,
- pub url: Option<String>,
- pub timestamp: Option<String>,
- pub color: Option<usize>,
- pub footer: Option<EmbedFooter>,
- pub image: Option<EmbedImage>,
- pub thumbnail: Option<EmbedThumbnail>,
- pub video: Option<EmbedVideo>,
- pub provider: Option<EmbedProvider>,
- pub author: Option<EmbedAuthor>,
- pub fields: Option<Vec<EmbedField>>,
- }
-
- pub const EMBED_TYPE_RICH: &str = "rich";
- pub const EMBED_TYPE_IMAGE: &str = "image";
- pub const EMBED_TYPE_VIDEO: &str = "video";
- pub const EMBED_TYPE_GIFV: &str = "gifv";
- pub const EMBED_TYPE_ARTICLE: &str = "article";
- pub const EMBED_TYPE_LINK: &str = "link";
-
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedFooter {
- pub text: String,
- pub icon_url: Option<String>,
- pub proxy_icon_url: Option<String>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedImage {
- pub url: Option<String>,
- pub proxy_url: Option<String>,
- pub height: Option<usize>,
- pub width: Option<usize>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedThumbnail {
- pub url: Option<String>,
- pub proxy_url: Option<String>,
- pub height: Option<usize>,
- pub width: Option<usize>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedVideo {
- pub url: Option<String>,
- pub proxy_url: Option<String>,
- pub height: Option<usize>,
- pub width: Option<usize>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedProvider {
- pub name: Option<String>,
- pub url: Option<String>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedAuthor {
- pub name: Option<String>,
- pub url: Option<String>,
- pub icon_url: Option<String>,
- pub proxy_icon_url: Option<String>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EmbedField {
- pub name: String,
- pub value: String,
- pub inline: Option<bool>,
- }
-
- // slash command management structures
- #[derive(Clone)]
- pub struct ApplicationCommand {
- pub id: Option<String>,
- pub application_id: Option<String>,
- pub name: String,
- pub description: String,
- pub options: Option<Vec<ApplicationCommandOption>>,
- }
-
- impl ApplicationCommand {
- pub fn raw(&self) -> ApplicationCommandRaw {
- let mut options = None;
- if let Some(opts) = &self.options {
- let mut options_raw = Vec::with_capacity(opts.len());
- for i in opts {
- options_raw.push(i.raw());
- }
- if !options_raw.is_empty() {
- options = Some(options_raw);
- }
- }
- ApplicationCommandRaw {
- id: self.id.clone(),
- application_id: self.application_id.clone(),
- name: self.name.to_string(),
- description: self.description.to_string(),
- options,
- }
- }
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct ApplicationCommandRaw {
- pub id: Option<String>,
- pub application_id: Option<String>,
- pub name: String,
- pub description: String,
- pub options: Option<Vec<ApplicationCommandOptionRaw>>,
- }
-
- #[derive(Clone)]
- pub enum ApplicationCommandOption {
- //#[serde(rename = "1")]
- SubCommand {
- name: String,
- description: String,
- required: bool,
- options: Option<Vec<ApplicationCommandOption>>,
- },
- //#[serde(rename = "2")]
- SubCommandGroup {
- name: String,
- description: String,
- required: bool,
- options: Option<Vec<ApplicationCommandOption>>,
- },
- //#[serde(rename = "3")]
- String {
- name: String,
- description: String,
- required: bool,
- choices: Option<Vec<ApplicationCommandOptionChoice>>,
- },
- //#[serde(rename = "4")]
- Integer {
- name: String,
- description: String,
- required: bool,
- choices: Option<Vec<ApplicationCommandOptionChoice>>,
- },
- //#[serde(rename = "5")]
- Boolean {
- name: String,
- description: String,
- required: bool,
- },
- //#[serde(rename = "6")]
- User {
- name: String,
- description: String,
- required: bool,
- },
- //#[serde(rename = "7")]
- Channel {
- name: String,
- description: String,
- required: bool,
- },
- //#[serde(rename = "8")]
- Role {
- name: String,
- description: String,
- required: bool,
- },
- }
-
- impl ApplicationCommandOption {
- pub fn raw(&self) -> ApplicationCommandOptionRaw {
- match self {
- ApplicationCommandOption::SubCommand { name, description, required, options} => {
- let mut options_opt = None;
- if let Some(opts) = options {
- let mut options_raw = Vec::with_capacity(opts.len());
- for i in opts {
- options_raw.push(i.raw());
- }
- options_opt = Some(options_raw);
- }
- ApplicationCommandOptionRaw {
- type_: 1,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: options_opt,
- choices: None,
- }
- },
- ApplicationCommandOption::SubCommandGroup { name, description, required, options} => {
- let mut options_opt = None;
- if let Some(opts) = options {
- let mut options_raw = Vec::with_capacity(opts.len());
- for i in opts {
- options_raw.push(i.raw());
- }
- options_opt = Some(options_raw);
- }
- ApplicationCommandOptionRaw {
- type_: 2,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: options_opt,
- choices: None,
- }
- },
- ApplicationCommandOption::String { name, description, required, choices} => ApplicationCommandOptionRaw {
- type_: 3,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: None,
- choices: choices.clone(),
- },
- ApplicationCommandOption::Integer { name, description, required, choices} => ApplicationCommandOptionRaw {
- type_: 4,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: None,
- choices: choices.clone(),
- },
- ApplicationCommandOption::Boolean { name, description, required} => ApplicationCommandOptionRaw {
- type_: 5,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: None,
- choices: None
- },
- ApplicationCommandOption::User { name, description, required} => ApplicationCommandOptionRaw {
- type_: 6,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: None,
- choices: None
- },
- ApplicationCommandOption::Channel { name, description, required} => ApplicationCommandOptionRaw {
- type_: 7,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: None,
- choices: None
- },
- ApplicationCommandOption::Role { name, description, required} => ApplicationCommandOptionRaw {
- type_: 8,
- name: name.to_string(),
- description: description.to_string(),
- required: *required,
- options: None,
- choices: None
- },
- }
- }
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct ApplicationCommandOptionRaw {
- #[serde(rename = "type")]
- pub type_: usize,
- pub name: String,
- pub description: String,
- pub required: bool,
- pub options: Option<Vec<ApplicationCommandOptionRaw>>,
- pub choices: Option<Vec<ApplicationCommandOptionChoice>>,
- }
-
- #[derive(Serialize, Deserialize, Clone)]
- pub struct ApplicationCommandOptionChoice {
- pub name: String,
- pub value: String,
- }
|