use std::collections::HashMap;
use super::{CreateAllowedMentions, CreateEmbed};
use crate::builder::CreateComponents;
use crate::internal::prelude::*;
use crate::json::{self, from_number};
use crate::model::channel::{AttachmentType, MessageFlags};
use crate::model::id::AttachmentId;
#[derive(Clone, Debug, Default)]
pub struct EditMessage<'a>(pub HashMap<&'static str, Value>, pub Vec<AttachmentType<'a>>);
impl<'a> EditMessage<'a> {
#[inline]
pub fn content<D: ToString>(&mut self, content: D) -> &mut Self {
self.0.insert("content", Value::from(content.to_string()));
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 suppress_embeds(&mut self, suppress: bool) -> &mut Self {
let flags = if suppress { 1 << 2 } else { 0 };
self.0.insert("flags", from_number(flags));
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::from(components.0));
self
}
pub fn flags(&mut self, flags: MessageFlags) -> &mut Self {
self.0.insert("flags", from_number(flags.bits()));
self
}
pub fn attachment(&mut self, attachment: impl Into<AttachmentType<'a>>) -> &mut Self {
self.1.push(attachment.into());
self
}
pub fn add_existing_attachment(&mut self, attachment: AttachmentId) -> &mut Self {
let attachments =
self.0.entry("attachments").or_insert_with(|| Value::from(Vec::<Value>::new()));
let attachments_array = attachments.as_array_mut().expect("Attachments must be an array");
let mut map = HashMap::new();
map.insert("id", Value::from(attachment.to_string()));
attachments_array.push(Value::from(json::hashmap_to_json_map(map)));
self
}
pub fn remove_existing_attachment(&mut self, attachment: AttachmentId) -> &mut Self {
let attachments =
self.0.entry("attachments").or_insert_with(|| Value::from(Vec::<Value>::new()));
let attachments_array = attachments.as_array_mut().expect("Attachments must be an array");
let attachment_string = attachment.to_string();
let mut found_at = None;
for (index, value) in attachments_array.iter().enumerate() {
if attachment_string
== value
.as_object()
.expect("Attachments must be an array of objects")
.get("id")
.expect("Attachments must be an array of objects containing ids")
.as_str()
.expect("Attachments must be an array of objects containing string ids")
{
found_at = Some(index);
}
}
if let Some(index) = found_at {
attachments_array.remove(index);
}
self
}
pub fn remove_all_attachments(&mut self) -> &mut Self {
let attachments =
self.0.entry("attachments").or_insert_with(|| Value::from(Vec::<Value>::new()));
let attachments_array = attachments.as_array_mut().expect("Attachments must be an array");
attachments_array.clear();
self
}
}