use crate::{
client::Client,
error::Error,
request::{
Nullable, Request, TryIntoRequest,
attachment::{AttachmentManager, PartialAttachment},
},
response::{Response, ResponseFuture},
routing::Route,
};
use serde::Serialize;
use std::future::IntoFuture;
use twilight_model::{
channel::message::{
AllowedMentions, Component, Embed, Message, MessageFlags, MessageReference,
MessageReferenceType,
},
http::attachment::Attachment,
id::{
Id,
marker::{ChannelMarker, MessageMarker, StickerMarker},
},
poll::Poll,
};
use twilight_validate::message::{
MessageValidationError, attachment as validate_attachment, components as validate_components,
content as validate_content, embeds as validate_embeds, sticker_ids as validate_sticker_ids,
};
#[derive(Serialize)]
pub(crate) struct CreateMessageFields<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
allowed_mentions: Option<Nullable<&'a AllowedMentions>>,
#[serde(skip_serializing_if = "Option::is_none")]
attachments: Option<Vec<PartialAttachment<'a>>>,
#[serde(skip_serializing_if = "Option::is_none")]
components: Option<&'a [Component]>,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
embeds: Option<&'a [Embed]>,
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<MessageFlags>,
#[serde(skip_serializing_if = "Option::is_none")]
message_reference: Option<MessageReference>,
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
payload_json: Option<&'a [u8]>,
#[serde(skip_serializing_if = "Option::is_none")]
poll: Option<&'a Poll>,
#[serde(skip_serializing_if = "Option::is_none")]
sticker_ids: Option<&'a [Id<StickerMarker>]>,
#[serde(skip_serializing_if = "Option::is_none")]
tts: Option<bool>,
}
#[must_use = "requests must be configured and executed"]
pub struct CreateMessage<'a> {
attachment_manager: AttachmentManager<'a>,
channel_id: Id<ChannelMarker>,
fields: Result<CreateMessageFields<'a>, MessageValidationError>,
http: &'a Client,
}
impl<'a> CreateMessage<'a> {
pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
Self {
attachment_manager: AttachmentManager::new(),
channel_id,
fields: Ok(CreateMessageFields {
attachments: None,
components: None,
content: None,
embeds: None,
flags: None,
message_reference: None,
nonce: None,
payload_json: None,
poll: None,
allowed_mentions: None,
sticker_ids: None,
tts: None,
}),
http,
}
}
pub const fn allowed_mentions(mut self, allowed_mentions: Option<&'a AllowedMentions>) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.allowed_mentions = Some(Nullable(allowed_mentions));
}
self
}
pub fn attachments(mut self, attachments: &'a [Attachment]) -> Self {
if self.fields.is_ok() {
if let Err(source) = attachments.iter().try_for_each(validate_attachment) {
self.fields = Err(source);
} else {
self.attachment_manager = self
.attachment_manager
.set_files(attachments.iter().collect());
}
}
self
}
pub fn components(mut self, components: &'a [Component]) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_components(
components,
fields
.flags
.is_some_and(|flags| flags.contains(MessageFlags::IS_COMPONENTS_V2)),
)?;
fields.components = Some(components);
Ok(fields)
});
self
}
pub fn content(mut self, content: &'a str) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_content(content)?;
fields.content.replace(content);
Ok(fields)
});
self
}
pub fn embeds(mut self, embeds: &'a [Embed]) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_embeds(embeds)?;
fields.embeds = Some(embeds);
Ok(fields)
});
self
}
pub const fn poll(mut self, poll: &'a Poll) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.poll = Some(poll);
}
self
}
pub fn fail_if_not_exists(mut self, fail_if_not_exists: bool) -> Self {
if let Ok(fields) = self.fields.as_mut() {
if let Some(reference) = fields.message_reference.as_mut() {
reference.fail_if_not_exists = Some(fail_if_not_exists);
} else {
fields.message_reference = Some(MessageReference {
kind: MessageReferenceType::default(),
channel_id: None,
guild_id: None,
message_id: None,
fail_if_not_exists: Some(fail_if_not_exists),
});
}
}
self
}
pub const fn flags(mut self, flags: MessageFlags) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.flags = Some(flags);
}
self
}
pub const fn nonce(mut self, nonce: u64) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.nonce = Some(nonce);
}
self
}
pub const fn payload_json(mut self, payload_json: &'a [u8]) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.payload_json = Some(payload_json);
}
self
}
pub fn reply(mut self, other: Id<MessageMarker>) -> Self {
self.fields = self.fields.map(|mut fields| {
let channel_id = self.channel_id;
let reference = if let Some(reference) = fields.message_reference {
MessageReference {
channel_id: Some(channel_id),
message_id: Some(other),
..reference
}
} else {
MessageReference {
kind: MessageReferenceType::Default,
channel_id: Some(channel_id),
guild_id: None,
message_id: Some(other),
fail_if_not_exists: None,
}
};
fields.message_reference = Some(reference);
fields
});
self
}
pub fn forward(mut self, channel_id: Id<ChannelMarker>, message_id: Id<MessageMarker>) -> Self {
self.fields = self.fields.map(|mut fields| {
let reference = if let Some(reference) = fields.message_reference {
MessageReference {
channel_id: Some(channel_id),
message_id: Some(message_id),
..reference
}
} else {
MessageReference {
kind: MessageReferenceType::Forward,
channel_id: Some(channel_id),
guild_id: None,
message_id: Some(message_id),
fail_if_not_exists: None,
}
};
fields.message_reference = Some(reference);
fields
});
self
}
pub fn sticker_ids(mut self, sticker_ids: &'a [Id<StickerMarker>]) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_sticker_ids(sticker_ids)?;
fields.sticker_ids = Some(sticker_ids);
Ok(fields)
});
self
}
pub const fn tts(mut self, tts: bool) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.tts = Some(tts);
}
self
}
}
impl IntoFuture for CreateMessage<'_> {
type Output = Result<Response<Message>, Error>;
type IntoFuture = ResponseFuture<Message>;
fn into_future(self) -> Self::IntoFuture {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}
impl TryIntoRequest for CreateMessage<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let mut fields = self.fields.map_err(Error::validation)?;
let mut request = Request::builder(&Route::CreateMessage {
channel_id: self.channel_id.get(),
});
if fields.allowed_mentions.is_none()
&& let Some(allowed_mentions) = self.http.default_allowed_mentions()
{
fields.allowed_mentions = Some(Nullable(Some(allowed_mentions)));
}
if !self.attachment_manager.is_empty() {
let form = if let Some(payload_json) = fields.payload_json {
self.attachment_manager.build_form(payload_json)
} else {
fields.attachments = Some(self.attachment_manager.get_partial_attachments());
let fields = crate::json::to_vec(&fields).map_err(Error::json)?;
self.attachment_manager.build_form(fields.as_ref())
};
request = request.form(form);
} else if let Some(payload_json) = fields.payload_json {
request = request.body(payload_json.to_vec());
} else {
request = request.json(&fields);
}
request.build()
}
}