Skip to main content

chipa_webhooks/platform/
discord.rs

1use serde_json::{Map, Value, json};
2
3use super::Platform;
4
5pub struct Discord {
6    webhook_url: String,
7    username: Option<String>,
8}
9
10impl Discord {
11    pub fn new(webhook_url: impl Into<String>) -> Self {
12        Self {
13            webhook_url: webhook_url.into(),
14            username: None,
15        }
16    }
17
18    pub fn with_username(mut self, username: impl Into<String>) -> Self {
19        self.username = Some(username.into());
20        self
21    }
22}
23
24impl Platform for Discord {
25    fn build_payload(&self, rendered: &str, hints: &Map<String, Value>) -> Value {
26        let mut embed = json!({ "description": rendered });
27
28        if let Some(color) = hints.get("__d_color") {
29            embed["color"] = color.clone();
30        }
31
32        if let Some(title) = hints.get("__d_title") {
33            embed["title"] = title.clone();
34        }
35
36        let mut payload = json!({ "embeds": [embed] });
37
38        if let Some(username) = &self.username {
39            payload["username"] = Value::String(username.clone());
40        }
41
42        payload
43    }
44
45    fn endpoint(&self) -> &str {
46        &self.webhook_url
47    }
48}