Slash commands are cool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.9KB

  1. use serde::{Deserialize, Serialize};
  2. #[derive(Serialize, Deserialize, Clone)]
  3. pub struct InteractionRaw {
  4. #[serde(rename = "type")]
  5. pub type_: usize,
  6. pub token: Option<String>,
  7. pub member: Option<GuildMember>,
  8. pub id: Option<String>,
  9. pub guild_id: Option<String>,
  10. pub data: Option<ApplicationCommandInteractionData>,
  11. pub channel_id: Option<String>,
  12. pub version: Option<usize>,
  13. }
  14. impl InteractionRaw {
  15. pub fn interaction(&self) -> Interaction {
  16. match self.type_ {
  17. 1 => Interaction::Ping {},
  18. 2 => Interaction::Command {
  19. token: self.token.clone().unwrap(),
  20. member: self.member.clone().unwrap(),
  21. id: self.id.clone().unwrap(),
  22. guild_id: self.guild_id.clone().unwrap(),
  23. data: self.data.clone().unwrap(),
  24. channel_id: self.channel_id.clone().unwrap(),
  25. version: self.version.unwrap_or(1),
  26. },
  27. _ => Interaction::Invalid {},
  28. }
  29. }
  30. }
  31. pub enum Interaction {
  32. //#[serde(rename = "1")]
  33. Ping {},
  34. //#[serde(rename = "2")]
  35. Command {
  36. token: String,
  37. member: GuildMember,
  38. id: String,
  39. guild_id: String,
  40. data: ApplicationCommandInteractionData,
  41. channel_id: String,
  42. version: usize,
  43. },
  44. Invalid {},
  45. }
  46. impl Interaction {
  47. pub fn is_ping(&self) -> bool {
  48. match self {
  49. Self::Ping {..} => true,
  50. Self::Command {..} => false,
  51. Self::Invalid {..} => false,
  52. }
  53. }
  54. pub fn is_command(&self) -> bool {
  55. match self {
  56. Self::Ping {..} => false,
  57. Self::Command {..} => true,
  58. Self::Invalid {..} => false,
  59. }
  60. }
  61. pub fn is_invalid(&self) -> bool {
  62. match self {
  63. Self::Ping {..} => false,
  64. Self::Command {..} => false,
  65. Self::Invalid {..} => true,
  66. }
  67. }
  68. pub fn cmd(&self) -> Option<InteractionCommand> {
  69. match self {
  70. Self::Ping {..} => None,
  71. Self::Command { token, member, id, guild_id, data, channel_id, version } => Some(InteractionCommand {
  72. token: token.to_string(),
  73. member: member.clone(),
  74. id: id.to_string(),
  75. guild_id: guild_id.to_string(),
  76. data: data.clone(),
  77. channel_id: channel_id.to_string(),
  78. version: *version,
  79. }),
  80. Self::Invalid {..} => None,
  81. }
  82. }
  83. }
  84. pub struct InteractionCommand {
  85. pub token: String,
  86. pub member: GuildMember,
  87. pub id: String,
  88. pub guild_id: String,
  89. pub data: ApplicationCommandInteractionData,
  90. pub channel_id: String,
  91. pub version: usize,
  92. }
  93. #[derive(Serialize, Deserialize, Clone)]
  94. pub struct GuildMember {
  95. pub user: Option<User>,
  96. pub nick: Option<String>,
  97. pub role: Vec<String>,
  98. pub joined_at: String,
  99. pub premium_since: Option<String>,
  100. pub deaf: bool,
  101. pub mute: bool,
  102. pub pending: Option<bool>,
  103. pub permissions: Option<String>,
  104. }
  105. #[derive(Serialize, Deserialize, Clone)]
  106. pub struct User {
  107. pub id: String,
  108. pub username: String,
  109. pub discriminator: String,
  110. pub avatar: Option<String>,
  111. pub bot: Option<bool>,
  112. pub system: Option<bool>,
  113. pub mfa_enabled: Option<bool>,
  114. pub locale: Option<String>,
  115. pub verified: Option<String>,
  116. pub email: Option<String>,
  117. pub flags: Option<usize>,
  118. pub premium_type: Option<usize>,
  119. pub public_flags: Option<usize>,
  120. }
  121. #[derive(Serialize, Deserialize, Clone)]
  122. pub struct ApplicationCommandInteractionData {
  123. pub id: String,
  124. pub name: String,
  125. pub options: Option<Vec<ApplicationCommandInteractionData>>,
  126. }
  127. pub enum InteractionResponse {
  128. //#[serde(rename = "1")]
  129. Pong {},
  130. //#[serde(rename = "2")]
  131. Acknowledge {},
  132. //#[serde(rename = "3")]
  133. ChannelMessage {
  134. data: Option<InteractionApplicationCommandCallbackData>,
  135. },
  136. //#[serde(rename = "4")]
  137. ChannelMessageWithSource {
  138. data: Option<InteractionApplicationCommandCallbackData>,
  139. },
  140. //#[serde(rename = "5")]
  141. AcknowledgeWithSource {},
  142. }
  143. impl InteractionResponse {
  144. pub fn raw(&self) -> InteractionResponseRaw {
  145. match self {
  146. InteractionResponse::Pong { .. } => InteractionResponseRaw {type_: 1, data:None},
  147. InteractionResponse::Acknowledge { .. } => InteractionResponseRaw {type_: 2, data:None},
  148. InteractionResponse::ChannelMessage { data, .. } => InteractionResponseRaw {type_: 3, data: data.clone()},
  149. InteractionResponse::ChannelMessageWithSource { data, .. } => InteractionResponseRaw {type_: 4, data: data.clone()},
  150. InteractionResponse::AcknowledgeWithSource { .. } => InteractionResponseRaw {type_: 5, data:None},
  151. }
  152. }
  153. }
  154. #[derive(Serialize, Deserialize, Clone)]
  155. pub struct InteractionResponseRaw {
  156. #[serde(rename = "type")]
  157. pub type_: usize,
  158. pub data: Option<InteractionApplicationCommandCallbackData>,
  159. }
  160. #[derive(Serialize, Deserialize, Clone)]
  161. pub struct InteractionApplicationCommandCallbackData {
  162. pub tts: bool,
  163. pub content: String,
  164. //pub embeds: Option<Vec<Embed>>,
  165. pub allowed_mentions: Option<String>,
  166. }
  167. // slash command management structures
  168. #[derive(Serialize, Deserialize, Clone)]
  169. pub struct ApplicationCommand {
  170. pub id: Option<String>,
  171. pub application_id: Option<String>,
  172. pub name: String,
  173. pub description: String,
  174. pub options: Option<Vec<ApplicationCommandOption>>,
  175. }
  176. #[derive(Serialize, Deserialize, Clone)]
  177. #[serde(tag = "type")]
  178. pub enum ApplicationCommandOption {
  179. #[serde(rename = "1")]
  180. SubCommand {
  181. name: String,
  182. description: String,
  183. required: bool,
  184. options: Option<Vec<ApplicationCommandOption>>,
  185. },
  186. #[serde(rename = "2")]
  187. SubCommandGroup {
  188. name: String,
  189. description: String,
  190. required: bool,
  191. options: Option<Vec<ApplicationCommandOption>>,
  192. },
  193. #[serde(rename = "3")]
  194. String {
  195. name: String,
  196. description: String,
  197. required: bool,
  198. choices: Option<Vec<ApplicationCommandOptionChoice>>,
  199. },
  200. #[serde(rename = "4")]
  201. Integer {
  202. name: String,
  203. description: String,
  204. required: bool,
  205. choices: Option<Vec<ApplicationCommandOptionChoice>>,
  206. },
  207. #[serde(rename = "5")]
  208. Boolean {
  209. name: String,
  210. description: String,
  211. required: bool,
  212. },
  213. #[serde(rename = "6")]
  214. User {
  215. name: String,
  216. description: String,
  217. required: bool,
  218. },
  219. #[serde(rename = "7")]
  220. Channel {
  221. name: String,
  222. description: String,
  223. required: bool,
  224. },
  225. #[serde(rename = "8")]
  226. Role {
  227. name: String,
  228. description: String,
  229. required: bool,
  230. },
  231. }
  232. #[derive(Serialize, Deserialize, Clone)]
  233. pub struct ApplicationCommandOptionChoice {
  234. pub name: String,
  235. pub value: String,
  236. }