use std::collections::HashMap;
#[cfg(not(feature = "model"))]
use std::marker::PhantomData;
use super::CreateAllowedMentions;
use crate::builder::CreateComponents;
use crate::json::{self, from_number, Value};
#[cfg(feature = "model")]
use crate::model::channel::AttachmentType;
use crate::model::channel::MessageFlags;
#[derive(Clone, Debug)]
pub struct ExecuteWebhook<'a>(
pub HashMap<&'static str, Value>,
#[cfg(feature = "model")] pub Vec<AttachmentType<'a>>,
#[cfg(not(feature = "model"))] PhantomData<&'a ()>,
);
impl<'a> ExecuteWebhook<'a> {
pub fn avatar_url<S: ToString>(&mut self, avatar_url: S) -> &mut Self {
self.0.insert("avatar_url", Value::from(avatar_url.to_string()));
self
}
pub fn content<S: ToString>(&mut self, content: S) -> &mut Self {
self.0.insert("content", Value::from(content.to_string()));
self
}
#[cfg(feature = "model")]
pub fn add_file<T: Into<AttachmentType<'a>>>(&mut self, file: T) -> &mut Self {
self.1.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.1.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.1 = 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
}
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::Array(components.0));
self
}
pub fn embeds(&mut self, embeds: Vec<Value>) -> &mut Self {
self.0.insert("embeds", Value::from(embeds));
self
}
pub fn tts(&mut self, tts: bool) -> &mut Self {
self.0.insert("tts", Value::from(tts));
self
}
pub fn username<S: ToString>(&mut self, username: S) -> &mut Self {
self.0.insert("username", Value::from(username.to_string()));
self
}
pub fn flags(&mut self, flags: MessageFlags) -> &mut Self {
self.0.insert("flags", from_number(flags.bits()));
self
}
}
impl<'a> Default for ExecuteWebhook<'a> {
fn default() -> ExecuteWebhook<'a> {
let mut map = HashMap::new();
map.insert("tts", Value::from(false));
#[allow(clippy::default_trait_access)]
ExecuteWebhook(map, Default::default())
}
}