1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Report as IP address update to Discord.

#![warn(missing_docs)]

use async_trait::async_trait;
use netloc_core::reporter::{Data, Reporter};
pub use reqwest::Client;
use serde::Serialize;

#[derive(Debug, Serialize)]
struct Message {
    content: String,
}

/// A [`Reporter`] that sends the data to the Discord via a Webhook.
pub struct Discord {
    /// HTTP client.
    pub client: Client,

    /// Discord Webhook to use.
    pub webhook_url: String,
}

impl Discord {
    /// Create a new [`Discord`] reporter from a webhook url.
    pub fn from_url(webhook_url: impl Into<String>) -> Self {
        let client = Client::new();
        let webhook_url = webhook_url.into();
        Self {
            client,
            webhook_url,
        }
    }
}

#[async_trait]
impl Reporter for Discord {
    type Error = Box<dyn std::error::Error>;

    async fn report(&self, data: &Data) -> Result<(), Self::Error> {
        let message = Message {
            content: data.ip.clone(),
        };
        self.client
            .post(&self.webhook_url)
            .json(&message)
            .send()
            .await?;
        Ok(())
    }
}