use super::create_poll::Ready;
#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::{
CreateActionRow,
CreateAllowedMentions,
CreateAttachment,
CreateEmbed,
CreatePoll,
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct CreateInteractionResponseFollowup {
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
tts: Option<bool>,
embeds: Vec<CreateEmbed>,
#[serde(skip_serializing_if = "Option::is_none")]
allowed_mentions: Option<CreateAllowedMentions>,
#[serde(skip_serializing_if = "Option::is_none")]
components: Option<Vec<CreateActionRow>>,
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<MessageFlags>,
#[serde(skip_serializing_if = "Option::is_none")]
poll: Option<CreatePoll<Ready>>,
attachments: EditAttachments,
}
impl CreateInteractionResponseFollowup {
pub fn new() -> Self {
Self::default()
}
#[cfg(feature = "http")]
fn check_length(&self) -> Result<()> {
if let Some(content) = &self.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}
check_overflow(self.embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in &self.embeds {
embed.check_length()?;
}
Ok(())
}
#[inline]
pub fn content(mut self, content: impl Into<String>) -> Self {
self.content = Some(content.into());
self
}
pub fn tts(mut self, tts: bool) -> Self {
self.tts = Some(tts);
self
}
pub fn add_file(mut self, file: CreateAttachment) -> Self {
self.attachments = self.attachments.add(file);
self
}
pub fn add_files(mut self, files: impl IntoIterator<Item = CreateAttachment>) -> Self {
for file in files {
self.attachments = self.attachments.add(file);
}
self
}
pub fn files(mut self, files: impl IntoIterator<Item = CreateAttachment>) -> Self {
self.attachments = EditAttachments::new();
self.add_files(files)
}
pub fn add_embed(mut self, embed: CreateEmbed) -> Self {
self.embeds.push(embed);
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 fn allowed_mentions(mut self, allowed_mentions: CreateAllowedMentions) -> Self {
self.allowed_mentions = Some(allowed_mentions);
self
}
pub fn flags(mut self, flags: MessageFlags) -> Self {
self.flags = Some(flags);
self
}
pub fn ephemeral(mut self, ephemeral: bool) -> Self {
let mut flags = self.flags.unwrap_or_else(MessageFlags::empty);
if ephemeral {
flags |= MessageFlags::EPHEMERAL;
} else {
flags &= !MessageFlags::EPHEMERAL;
}
self.flags = Some(flags);
self
}
pub fn poll(mut self, poll: CreatePoll<Ready>) -> Self {
self.poll = Some(poll);
self
}
pub fn components(mut self, components: Vec<CreateActionRow>) -> Self {
self.components = Some(components);
self
}
super::button_and_select_menu_convenience_methods!(self.components);
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for CreateInteractionResponseFollowup {
type Context<'ctx> = (Option<MessageId>, &'ctx str);
type Built = Message;
async fn execute(
mut self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
self.check_length()?;
let files = self.attachments.take_files();
let http = cache_http.http();
if self.allowed_mentions.is_none() {
self.allowed_mentions.clone_from(&http.default_allowed_mentions);
}
match ctx.0 {
Some(id) => http.as_ref().edit_followup_message(ctx.1, id, &self, files).await,
None => http.as_ref().create_followup_message(ctx.1, &self, files).await,
}
}
}