use std::collections::HashMap;
#[cfg(not(feature = "model"))]
use std::marker::PhantomData;
use super::{CreateAllowedMentions, CreateEmbed};
use crate::builder::CreateComponents;
use crate::internal::prelude::*;
use crate::json::{self, from_number, to_value};
#[cfg(feature = "model")]
use crate::model::channel::AttachmentType;
use crate::model::channel::{MessageFlags, MessageReference, ReactionType};
use crate::model::id::StickerId;
#[derive(Clone, Debug)]
pub struct CreateMessage<'a>(
pub HashMap<&'static str, Value>,
pub Option<Vec<ReactionType>>,
#[cfg(feature = "model")] pub Vec<AttachmentType<'a>>,
#[cfg(not(feature = "model"))] PhantomData<&'a ()>,
);
impl<'a> CreateMessage<'a> {
#[inline]
pub fn content<D: ToString>(&mut self, content: D) -> &mut Self {
self._content(content.to_string())
}
fn _content(&mut self, content: String) -> &mut Self {
self.0.insert("content", Value::from(content));
self
}
fn _add_embed(&mut self, embed: CreateEmbed) -> &mut Self {
let map = json::hashmap_to_json_map(embed.0);
let embed = Value::from(map);
let embeds = self.0.entry("embeds").or_insert_with(|| Value::from(Vec::<Value>::new()));
let embeds_array = embeds.as_array_mut().expect("Embeds must be an array");
embeds_array.push(embed);
self
}
pub fn add_embed<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed,
{
let mut embed = CreateEmbed::default();
f(&mut embed);
self._add_embed(embed)
}
pub fn add_embeds(&mut self, embeds: Vec<CreateEmbed>) -> &mut Self {
for embed in embeds {
self._add_embed(embed);
}
self
}
pub fn embed<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed,
{
let mut embed = CreateEmbed::default();
f(&mut embed);
self.0.insert("embeds", Value::from(Vec::<Value>::new()));
self._add_embed(embed)
}
pub fn set_embed(&mut self, embed: CreateEmbed) -> &mut Self {
self.0.insert("embeds", Value::from(Vec::<Value>::new()));
self._add_embed(embed)
}
pub fn set_embeds(&mut self, embeds: Vec<CreateEmbed>) -> &mut Self {
self.0.insert("embeds", Value::from(Vec::<Value>::new()));
for embed in embeds {
self._add_embed(embed);
}
self
}
pub fn tts(&mut self, tts: bool) -> &mut Self {
self.0.insert("tts", Value::from(tts));
self
}
#[inline]
pub fn reactions<R: Into<ReactionType>, It: IntoIterator<Item = R>>(
&mut self,
reactions: It,
) -> &mut Self {
self._reactions(reactions.into_iter().map(Into::into).collect());
self
}
fn _reactions(&mut self, reactions: Vec<ReactionType>) {
self.1 = Some(reactions);
}
#[cfg(feature = "model")]
pub fn add_file<T: Into<AttachmentType<'a>>>(&mut self, file: T) -> &mut Self {
self.2.push(file.into());
self
}
#[cfg(feature = "model")]
pub fn add_files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item = T>>(
&mut self,
files: It,
) -> &mut Self {
self.2.extend(files.into_iter().map(Into::into));
self
}
#[cfg(feature = "model")]
pub fn files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item = T>>(
&mut self,
files: It,
) -> &mut Self {
self.2 = files.into_iter().map(Into::into).collect();
self
}
pub fn allowed_mentions<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateAllowedMentions) -> &mut CreateAllowedMentions,
{
let mut allowed_mentions = CreateAllowedMentions::default();
f(&mut allowed_mentions);
let map = json::hashmap_to_json_map(allowed_mentions.0);
let allowed_mentions = Value::from(map);
self.0.insert("allowed_mentions", allowed_mentions);
self
}
#[allow(clippy::unwrap_used)] pub fn reference_message(&mut self, reference: impl Into<MessageReference>) -> &mut Self {
self.0.insert("message_reference", to_value(reference.into()).unwrap());
self
}
pub fn components<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateComponents) -> &mut CreateComponents,
{
let mut components = CreateComponents::default();
f(&mut components);
self.0.insert("components", Value::from(components.0));
self
}
pub fn set_components(&mut self, components: CreateComponents) -> &mut Self {
self.0.insert("components", Value::from(components.0));
self
}
pub fn flags(&mut self, flags: MessageFlags) -> &mut Self {
self.0.insert("flags", from_number(flags.bits()));
self
}
pub fn sticker_id(&mut self, sticker_id: impl Into<StickerId>) -> &mut Self {
self.0.insert("sticker_ids", Value::from(Vec::<Value>::new()));
self.add_sticker_id(sticker_id)
}
pub fn add_sticker_id(&mut self, sticker_id: impl Into<StickerId>) -> &mut Self {
let sticker_ids =
self.0.entry("sticker_ids").or_insert_with(|| Value::from(Vec::<Value>::new()));
let sticker_ids_array = sticker_ids.as_array_mut().expect("Sticker_ids must be an array");
sticker_ids_array.push(Value::from(sticker_id.into().0));
self
}
pub fn add_sticker_ids<T: Into<StickerId>, It: IntoIterator<Item = T>>(
&mut self,
sticker_ids: It,
) -> &mut Self {
for sticker_id in sticker_ids {
self.add_sticker_id(sticker_id);
}
self
}
pub fn set_sticker_ids<T: Into<StickerId>, It: IntoIterator<Item = T>>(
&mut self,
sticker_ids: It,
) -> &mut Self {
self.0.insert("sticker_ids", Value::from(Vec::<Value>::new()));
self.add_sticker_ids(sticker_ids)
}
}
impl<'a> Default for CreateMessage<'a> {
fn default() -> CreateMessage<'a> {
let mut map = HashMap::new();
map.insert("tts", Value::from(false));
#[allow(clippy::default_trait_access)]
CreateMessage(map, None, Default::default())
}
}