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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;

type OString = Option<String>;
type OInt32 = Option<i32>;

pub struct Webhook {
    url: String,
    client: Client
}

#[derive(Deserialize, Debug)]
pub struct WebhookModel {
    pub name: String,
    #[serde(rename = "type")]
    pub webhook_type: i8,
    pub channel_id: String,
    pub token: Option<String>,
    pub avatar: Option<String>,
    pub guild_id: Option<String>,
    pub user: Option<WebhookUser>
}

#[derive(Deserialize, Debug)]
pub struct WebhookUser {
    pub username: String,
    pub discriminator: String,
    pub id: String,
    pub avatar: String
}

#[derive(Debug, Serialize)]
pub struct EmbedBuilder {
    title: OString,
    #[serde(rename = "type")]
    type_: OString,
    description: OString,
    url: OString,
    timestamp: OString,
    color: OInt32,
    fields: Vec<EmbedField>,
    footer: Option<EmbedFooter>,
    image: Option<EmbedImage>,
    author: Option<EmbedAuthor>,
    video: Option<EmbedVideo>
}

#[derive(Debug, Serialize)]
pub struct Embed {
    title: OString,
    #[serde(rename = "type")]
    type_: OString,
    description: OString,
    url: OString,
    timestamp: OString,
    color: OInt32,
    fields: Vec<EmbedField>,
    footer: Option<EmbedFooter>,
    image: Option<EmbedImage>,
    author: Option<EmbedAuthor>,
    video: Option<EmbedVideo>
}

impl EmbedBuilder {

    pub fn new() -> Self {
        Self {
            title: None,
            type_: None,
            description: None,
            url: None,
            timestamp: None,
            color: None,
            fields: vec![],
            footer: None,
            image: None,
            author: None,
            video: None
        }
    }

    pub fn title(&mut self, title: &str) -> &mut EmbedBuilder {
        self.title = Some(title.to_owned());
        self
    }

    pub fn type_(&mut self, type_: &str) -> &mut EmbedBuilder {
        self.type_ = Some(type_.to_owned());
        self
    }

    pub fn description(&mut self, description: &str) -> &mut EmbedBuilder {
        self.description = Some(description.to_owned());
        self
    }

    pub fn url(&mut self, url: &str) -> &mut EmbedBuilder {
        self.url = Some(url.to_owned());
        self
    }

    pub fn color(&mut self, color: i32) -> &mut EmbedBuilder {
        self.color = Some(color);
        self
    }

    pub fn field(&mut self, name: &str, value: &str, inline: bool) -> &mut EmbedBuilder {
        self.fields.push(EmbedField::new(name, value, inline));
        self
    }

    pub fn timestamp(&mut self, timestamp: &str) -> &mut EmbedBuilder {
        self.timestamp = Some(timestamp.to_owned());
        self
    }

    pub fn footer(&mut self, text: &str, url: OString, proxy_url: OString) -> &mut EmbedBuilder {
        self.footer = Some(EmbedFooter::new(text, url, proxy_url));
        self
    }

    pub fn image(&mut self, url: &str, proxy_url: OString, height: OInt32, width: OInt32) -> &mut EmbedBuilder {
        self.image = Some(EmbedImage::new(url, proxy_url, height, width));
        self
    }

    pub fn author(&mut self, name: &str, url: &str, icon_url: OString, proxy_icon_url: OString) -> &mut EmbedBuilder {
        self.author = Some(EmbedAuthor::new(name, url, icon_url, proxy_icon_url));
        self
    }

    pub fn video(&mut self, url: &str, height: OInt32, width: OInt32) -> &mut EmbedBuilder {
        self.video = Some(EmbedVideo::new(url, height, width));
        self
    }

    pub fn build(&mut self) -> Embed {
        Embed {
            title: self.title.clone(),
            type_: self.type_.clone(),
            description: self.description.clone(),
            url: self.url.clone(),
            timestamp: self.timestamp.clone(),
            color: self.color.clone(),
            fields: self.fields.clone(),
            footer: self.footer.clone(),
            image: self.image.clone(),
            author: self.author.clone(),
            video: self.video.clone()
        }
    }

}

#[derive(Debug, Serialize, Clone)]
pub struct EmbedField {
    name: String,
    value: String,
    inline: bool
}

impl EmbedField {
    pub fn new(name: &str, value: &str, inline: bool) -> Self {
        Self {
            name: name.to_owned(),
            value: value.to_owned(),
            inline
        }
    }
}

#[derive(Debug, Serialize, Clone)]
pub struct EmbedFooter {
    text: String,
    icon_url: OString,
    proxy_icon_url: OString
}

impl EmbedFooter {
    pub fn new(text: &str, icon_url: Option<String>, proxy_icon_url: Option<String>) -> Self {
        Self {
            text: text.to_owned(),
            icon_url,
            proxy_icon_url
        }
    }
}

#[derive(Debug, Serialize, Clone)]
pub struct EmbedImage {
    url: OString,
    proxy_url: OString,
    height: OInt32,
    width: OInt32
}

impl EmbedImage {
    pub fn new(url: &str, proxy_url: OString, height: OInt32, width: OInt32) -> Self {
        Self {
            url: Some(url.to_owned()),
            proxy_url,
            height,
            width
        }
    }
}

#[derive(Debug, Serialize, Clone)]
pub struct EmbedAuthor {
    name: String,
    url: String,
    icon_url: OString,
    proxy_icon_url: OString
}

impl EmbedAuthor {
    pub fn new(name: &str, url: &str, icon_url: OString, proxy_icon_url: OString) -> Self {
        Self {
            name: name.to_owned(),
            url: url.to_owned(),
            icon_url,
            proxy_icon_url
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct EmbedVideo {
    url: String,
    height: OInt32,
    width: OInt32
}

impl EmbedVideo {
    pub fn new(url: &str, height: OInt32, width: OInt32) -> Self {
        Self {
            url: url.to_owned(),
            height,
            width
        }
    }
}

#[derive(Debug, Serialize)]
pub struct Message {
    content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    username: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    avatar_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tts: Option<bool>,
    embeds: Vec<Embed>
}

impl Message {

    pub fn new() -> Self {
        Self {
            content: None,
            username: None,
            avatar_url: None,
            tts: None,
            embeds: vec![]
        }
    }

    pub fn content(&mut self, content: &str) -> &mut Message {
        self.content = Some(content.to_owned());
        self
    }

    pub fn username(&mut self, name: &str) -> &mut Message {
        self.username = Some(name.to_owned());
        self
    }

    pub fn avatar_url(&mut self, url: &str) -> &mut Message {
        self.avatar_url = Some(url.to_owned());
        self
    }

    pub fn tts(&mut self, tts: bool) -> &mut Message {
        self.tts = Some(tts.to_owned());
        self
    }

    pub fn embed<F>(&mut self, embed: F) -> &mut Message
    where F: Fn(&mut EmbedBuilder) -> &mut EmbedBuilder {
        let mut em = EmbedBuilder::new();
        let embed = embed(&mut em);
        self.embeds.push(embed.build());
        self
    }

}

impl Webhook {

    pub fn from_url(url: &str) -> Self {
        Self {
            url: url.to_owned(),
            client: Client::new()
        }
    }

    pub async fn get_info(&self) -> Result<WebhookModel, Box<dyn Error>> {
        let request = self.client.get(&self.url).send().await?;
        let content = request.text().await?;
        Ok(serde_json::from_str(&content)?)
    }

    pub async fn send<F>(&self, t: F) -> Result<(), Box<dyn Error>>
    where F: Fn(&mut Message) -> &mut Message {
        let mut msg = Message::new();
        let message = t(&mut msg);
        self.client.post(&self.url)
            .json(&message).send().await?;
        Ok(())
    }

}