netloc_discord/
lib.rs

1//! Report as IP address update to Discord.
2
3#![warn(missing_docs)]
4
5use async_trait::async_trait;
6use netloc_core::reporter::{Data, Reporter};
7pub use reqwest::Client;
8use serde::Serialize;
9
10#[derive(Debug, Serialize)]
11struct Message {
12    content: String,
13}
14
15/// A [`Reporter`] that sends the data to the Discord via a Webhook.
16pub struct Discord {
17    /// HTTP client.
18    pub client: Client,
19
20    /// Discord Webhook to use.
21    pub webhook_url: String,
22}
23
24impl Discord {
25    /// Create a new [`Discord`] reporter from a webhook url.
26    pub fn from_url(webhook_url: impl Into<String>) -> Self {
27        let client = Client::new();
28        let webhook_url = webhook_url.into();
29        Self {
30            client,
31            webhook_url,
32        }
33    }
34}
35
36#[async_trait]
37impl Reporter for Discord {
38    type Error = Box<dyn std::error::Error>;
39
40    async fn report(&self, data: &Data) -> Result<(), Self::Error> {
41        let message = Message {
42            content: data.ip.clone(),
43        };
44        self.client
45            .post(&self.webhook_url)
46            .json(&message)
47            .send()
48            .await?;
49        Ok(())
50    }
51}