use crate::{
client::Client,
error::Error as HttpError,
request::{validate, Pending, Request},
routing::Route,
};
use serde::Serialize;
use std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::{
channel::{
embed::Embed,
message::{AllowedMentions, MessageFlags},
Attachment, Message,
},
id::{ChannelId, MessageId},
};
#[derive(Debug)]
pub struct UpdateMessageError {
kind: UpdateMessageErrorType,
source: Option<Box<dyn Error + Send + Sync>>,
}
impl UpdateMessageError {
#[must_use = "retrieving the type has no effect if left unused"]
pub const fn kind(&self) -> &UpdateMessageErrorType {
&self.kind
}
#[must_use = "consuming the error and retrieving the source has no effect if left unused"]
pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
self.source
}
#[must_use = "consuming the error into its parts has no effect if left unused"]
pub fn into_parts(self) -> (UpdateMessageErrorType, Option<Box<dyn Error + Send + Sync>>) {
(self.kind, self.source)
}
}
impl Display for UpdateMessageError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match &self.kind {
UpdateMessageErrorType::ContentInvalid { .. } => {
f.write_str("the message content is invalid")
}
UpdateMessageErrorType::EmbedTooLarge { .. } => {
f.write_str("the embed's contents are too long")
}
}
}
}
impl Error for UpdateMessageError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source
.as_ref()
.map(|source| &**source as &(dyn Error + 'static))
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum UpdateMessageErrorType {
ContentInvalid {
content: String,
},
EmbedTooLarge {
embed: Box<Embed>,
},
}
#[derive(Default, Serialize)]
struct UpdateMessageFields {
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) allowed_mentions: Option<AllowedMentions>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub attachments: Vec<Attachment>,
#[allow(clippy::option_option)]
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<Option<String>>,
#[allow(clippy::option_option)]
#[serde(skip_serializing_if = "Option::is_none")]
embed: Option<Option<Embed>>,
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<MessageFlags>,
}
pub struct UpdateMessage<'a> {
channel_id: ChannelId,
fields: UpdateMessageFields,
fut: Option<Pending<'a, Message>>,
http: &'a Client,
message_id: MessageId,
}
impl<'a> UpdateMessage<'a> {
pub(crate) fn new(http: &'a Client, channel_id: ChannelId, message_id: MessageId) -> Self {
Self {
channel_id,
fields: UpdateMessageFields::default(),
fut: None,
http,
message_id,
}
}
pub fn attachment(mut self, attachment: Attachment) -> Self {
self.fields.attachments.push(attachment);
self
}
pub fn attachments(mut self, attachments: impl IntoIterator<Item = Attachment>) -> Self {
self.fields
.attachments
.extend(attachments.into_iter().collect::<Vec<Attachment>>());
self
}
pub fn content(self, content: impl Into<Option<String>>) -> Result<Self, UpdateMessageError> {
self._content(content.into())
}
fn _content(mut self, content: Option<String>) -> Result<Self, UpdateMessageError> {
if let Some(content_ref) = content.as_ref() {
if !validate::content_limit(content_ref) {
return Err(UpdateMessageError {
kind: UpdateMessageErrorType::ContentInvalid {
content: content.expect("content is known to be some"),
},
source: None,
});
}
}
self.fields.content.replace(content);
Ok(self)
}
pub fn embed(self, embed: impl Into<Option<Embed>>) -> Result<Self, UpdateMessageError> {
self._embed(embed.into())
}
fn _embed(mut self, embed: Option<Embed>) -> Result<Self, UpdateMessageError> {
if let Some(embed_ref) = embed.as_ref() {
if let Err(source) = validate::embed(&embed_ref) {
return Err(UpdateMessageError {
kind: UpdateMessageErrorType::EmbedTooLarge {
embed: Box::new(embed.expect("embed is known to be some")),
},
source: Some(Box::new(source)),
});
}
}
self.fields.embed.replace(embed);
Ok(self)
}
pub fn suppress_embeds(mut self, suppress: bool) -> Self {
let mut flags = self.fields.flags.unwrap_or_else(MessageFlags::empty);
if suppress {
flags |= MessageFlags::SUPPRESS_EMBEDS;
} else {
flags &= !MessageFlags::SUPPRESS_EMBEDS;
}
self.fields.flags.replace(flags);
self
}
pub fn allowed_mentions(mut self, allowed: AllowedMentions) -> Self {
self.fields.allowed_mentions.replace(allowed);
self
}
fn start(&mut self) -> Result<(), HttpError> {
let request = Request::builder(Route::UpdateMessage {
channel_id: self.channel_id.0,
message_id: self.message_id.0,
})
.json(&self.fields)?
.build();
self.fut.replace(Box::pin(self.http.request(request)));
Ok(())
}
}
poll_req!(UpdateMessage<'_>, Message);