use crate::builders::CreateEmbed;
use crate::http::{HttpClient, HttpError};
use crate::model::channel::ChannelId;
use crate::model::message::Message;
use crate::model::message::Replies;
use serde::Serialize;
use crate::http::routing::Route;
#[derive(Debug, Default, Serialize)]
pub struct CreateMessage {
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub nonce: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub attachments: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub replies: Option<Vec<Replies>>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub embeds: Vec<CreateEmbed>,
}
impl CreateMessage {
pub fn new() -> Self {
Default::default()
}
pub fn content(mut self, content: impl Into<String>) -> Self {
self.content = Some(content.into());
self
}
pub fn nonce(mut self, nonce: Option<String>) -> Self {
self.nonce = nonce;
self
}
pub fn attachments(mut self, attachments: Vec<String>) -> Self {
self.attachments = attachments;
self
}
pub fn replies(mut self, replies: Replies) -> Self {
self.replies = Some(vec![replies]);
self
}
pub fn add_embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
self.embeds.extend(embeds);
self
}
pub fn embed(self, embed: CreateEmbed) -> Self {
self.embeds(vec![embed])
}
pub fn embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
self.embeds = embeds;
self
}
pub(crate) async fn execute(self, http: &HttpClient, channel_id: &ChannelId) -> Result<Message, HttpError> {
let route = Route::SendMessage {channel_id: &channel_id.0 };
let response = http.request::<Self, (), Message>(route, Some(self), None).await?;
Ok(response)
}
}