#![allow(clippy::too_many_arguments)]
use crate::api::{Author, Embed, Field, Footer, Image, Provider, Thumbnail, Video, WebhookClient};
use crate::error::WeboxideResult;
use serde_json;
impl WebhookClient {
pub fn new(
client: reqwest::Client,
hook_url: String,
avatar_url: Option<String>,
username: Option<String>,
embeds: Vec<Embed>,
) -> WebhookClient {
WebhookClient {
client,
hook_url,
avatar_url,
username,
embeds,
}
}
pub async fn send_message(&self, message: String) -> WeboxideResult<()> {
let body = serde_json::json!({
"avatar_url": self.avatar_url,
"username": self.username,
"embeds": self.embeds,
"content": message
});
self.client.post(&self.hook_url).json(&body).send().await?;
Ok(())
}
pub async fn delete_hook(&self) -> WeboxideResult<()> {
self.client.delete(&self.hook_url).send().await?;
Ok(())
}
pub fn add_embed(&mut self, title: String, description: Option<String>, fields: Vec<Field>) {
self.embeds.push(Embed {
title,
description,
fields,
..Default::default()
});
}
pub fn add_embed_from_parts(
&mut self,
title: String,
description: Option<String>,
fields: Vec<Field>,
timestamp: Option<bool>,
color: Option<u32>,
footer: Option<Footer>,
image: Option<Image>,
thumbnail: Option<Thumbnail>,
video: Option<Video>,
provider: Option<Provider>,
author: Option<Author>,
) {
self.embeds.push(Embed {
title,
description,
fields,
timestamp,
color,
footer,
image,
thumbnail,
video,
provider,
author,
});
}
}