diff --git a/Cargo.toml b/Cargo.toml index 22d8f0b..8e9652b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" tokio = {version = "1.27.0", features = ["rt", "macros", "rt-multi-thread"]} serde = {version = "1.0.159", features = ["derive"]} serde_json = "1.0.95" -reqwest = "0.11.16" +reqwest = {version = "0.11.16", features = ["multipart"]} url = "2.3.1" chrono = "0.4.24" regex = "1.7.3" diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 668e150..357e245 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1,11 +1,6 @@ pub mod messages { - use reqwest::{Client, Response}; - use serde_json::to_string; - use std::io::Read; - use crate::api::limits::Limits; - use crate::api::types::{DiscordFileAttachment, Message, User}; - use crate::errors::InstanceServerError; + use crate::api::types::{Message, PartialDiscordFileAttachment, User}; use crate::limit::LimitedRequester; impl Message { @@ -20,54 +15,21 @@ pub mod messages { # Errors * [`InstanceServerError`] - If the message cannot be sent. */ - pub async fn send( + + pub async fn send<'a>( url_api: &String, - token: &String, - message: &Message, - files: Option>, - limits_user: &mut Limits, + message: &mut Message, + files: Option>, + user: &mut User<'a>, limits_instance: &mut Limits, requester: &mut LimitedRequester, - ) -> Result { - let mut request = Client::new() - .post(format!( - "{}/channels/{}/messages", - url_api, message.channel_id - )) - .body(to_string(message).unwrap()) - .bearer_auth(token); - if files.is_some() {} - match requester - .send_request( - request, - crate::api::limits::LimitType::Channel, - limits_instance, - limits_user, - ) - .await - { - Ok(result) => Ok(result), - Err(e) => Err(e), - } + ) { + let token = user.token(); + let mut limits = &mut user.rate_limits; } } impl<'a> User<'a> { - pub async fn send_message( - &mut self, - message: &Message, - files: Option>, - ) -> Result { - Message::send( - &self.belongs_to().urls.get_api().to_string(), - &self.token(), - message, - files, - self.rate_limits.get_as_mut(), - &mut self.belongs_to.limits.get_as_mut(), - &mut LimitedRequester::new().await, - ) - .await - } + pub async fn send_message() {} } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index 08425a3..7672e55 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -1,8 +1,12 @@ +use std::{collections::HashMap, io::Bytes}; + use regex::Regex; -use serde::{Deserialize, Serialize}; +use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer}; use crate::errors::FieldFormatError; +use super::{DiscordFileAttachment, Embed}; + /** A struct that represents a well-formed email address. */ @@ -240,6 +244,24 @@ pub struct TotpSchema { login_source: Option, } +#[derive(Debug, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct MessageSendSchema { + #[serde(rename = "type")] + message_type: i32, + content: Option, + nonce: Option, + tts: Option, + embeds: Option>, + allowed_mentions: Option, + message_reference: Option, + components: Option>, + sticker_ids: Option>, + #[serde(flatten)] + files: Option>>, + attachments: Option>, +} + // I know that some of these tests are... really really basic and unneccessary, but sometimes, I // just feel like writing tests, so there you go :) -@bitfl0wer #[cfg(test)] diff --git a/src/api/types.rs b/src/api/types.rs index 0fa51f2..0cb7ba0 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -4,7 +4,7 @@ https://discord.com/developers/docs . I do not feel like re-documenting all of this, as everything is already perfectly explained there. */ -use std::fs::File; +use std::{collections::HashMap, fs::File}; use serde::{Deserialize, Serialize}; @@ -216,7 +216,7 @@ pub struct Message { mentions: Vec, mention_roles: Vec, mention_channels: Option>, - attachments: Vec, + pub attachments: Vec, embeds: Vec, reactions: Option>, nonce: Option, @@ -263,7 +263,7 @@ struct PartialMessage { mentions: Option>, mention_roles: Option>, mention_channels: Option>, - attachments: Option>, + attachments: Option>, embeds: Option>, reactions: Option>, nonce: Option, @@ -368,25 +368,11 @@ struct ChannelMention { name: String, } -#[derive(Debug, Serialize, Deserialize)] -struct Attachment { - id: String, - filename: String, - description: Option, - content_type: Option, - size: i64, - url: String, - proxy_url: String, - height: Option, - width: Option, - ephemeral: Option, -} - #[derive(Debug, Serialize, Deserialize)] /** Represents an Embed. [See the Discord Documentation](https://discord.com/developers/docs/resources/channel#embed-object). */ -struct Embed { +pub struct Embed { title: Option, #[serde(rename = "type")] embed_type: Option, @@ -539,9 +525,9 @@ struct InstallParams { } #[derive(Debug, Serialize, Deserialize)] -struct MessageReference { - message_id: Option, - channel_id: Option, +pub struct MessageReference { + message_id: String, + channel_id: String, guild_id: Option, fail_if_not_exists: Option, } @@ -655,7 +641,7 @@ struct DefaultReaction { } #[derive(Debug, Serialize, Deserialize)] -enum Component { +pub enum Component { ActionRow = 1, Button = 2, StringSelect = 3, @@ -842,27 +828,51 @@ pub struct GatewayPayload { impl WebSocketEvent for GatewayPayload {} +#[derive(Debug, Serialize, Deserialize)] pub struct DiscordFileAttachment { - pub name: String, + pub id: i16, pub filename: String, - pub file: File, + description: Option, + content_type: Option, + size: i64, + url: String, + proxy_url: String, + height: Option, + width: Option, + ephemeral: Option, + duration_secs: Option, + waveform: Option, } -impl DiscordFileAttachment { - pub fn new(filenames: &Vec, files: Vec) { - //-> Vec { - if filenames.len() != files.len() { - panic!("Your 'filenames' Vector has either more or less elements than your 'files' Vector.") - } - let mut return_vec: Vec = Vec::new(); - let mut counter = 0; - /*for _ in 0..files.len() { - return_vec.push(DiscordFileAttachment { - name: format!("files[{}]", counter.to_string()), - filename: filenames.iter().next().unwrap().to_string(), - file: files.get(0).unwrap(), - }); - } - return_vec*/ - } +#[derive(Debug, Serialize, Deserialize)] + +pub struct PartialDiscordFileAttachment { + pub id: Option, + pub filename: Option, + description: Option, + content_type: Option, + size: Option, + url: Option, + proxy_url: Option, + height: Option, + width: Option, + ephemeral: Option, + duration_secs: Option, + waveform: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct AllowedMention { + parse: Vec, + roles: Vec, + users: Vec, + replied_user: bool, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum AllowedMentionType { + Roles, + Users, + Everyone, }