spectacles_model/message/
webhook.rs

1use crate::{Snowflake, User};
2use crate::message::embed::Embed;
3
4/// A simple solution to post messages in Discord channels from external sources.
5#[derive(Deserialize, Serialize, Clone, Debug, Default)]
6pub struct Webhook {
7    /// The webhook ID of this webhook.
8    pub id: Snowflake,
9    /// The guild ID of the guild which the webhook belongs to.
10    #[serde(default)]
11    pub guild_id: Option<String>,
12    /// The channel ID of the channel which the webhook belongs to.
13    pub channel_id: Snowflake,
14    /// The user who created this webhook.
15    #[serde(default)]
16    pub user: Option<User>,
17    /// The default name of this webhook.
18    pub name: Option<String>,
19    /// The default avatar hash of this webhook.
20    pub avatar: Option<String>,
21    /// The secure token of this webhook.
22    pub token: String
23}
24
25#[derive(Serialize, Clone, Debug, Default)]
26pub struct ModifyWebhookOptions {
27    #[serde(skip_serializing_if = "Option::is_none")]
28    name: Option<String>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    avatar: Option<String>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    channel_id: Option<Snowflake>,
33}
34
35impl ModifyWebhookOptions {
36    /// Sets a new name for the webhook.
37    pub fn name(mut self, name: &str) -> Self {
38        self.name = Some(name.to_string());
39        self
40    }
41
42    /// Sets a new avatar for this webhook.
43    /// This url must be base64 encodes according to Discord specifications, which can be found [here.](https://discordapp.com/developers/docs/resources/user#avatar-data)
44    pub fn avatar(mut self, url: &str) -> Self {
45        self.avatar = Some(url.to_string());
46        self
47    }
48
49    /// Sets the new channel ID for this webhook.
50    pub fn channel_id(mut self, id: Snowflake) -> Self {
51        self.channel_id = Some(id);
52        self
53    }
54}
55
56#[derive(Serialize, Debug, Default)]
57pub struct ExecuteWebhookOptions {
58    #[serde(skip_serializing_if = "Option::is_none")]
59    content: Option<String>,
60    #[serde(skip_serializing_if = "Option::is_none")]
61    username: Option<String>,
62    #[serde(skip_serializing_if = "Option::is_none")]
63    avatar_url: Option<String>,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    tts: Option<bool>,
66    #[serde(skip_serializing)]
67    pub file: Option<(String, Vec<u8>)>,
68    embeds: Vec<Embed>,
69}
70
71impl ExecuteWebhookOptions {
72    /// Adds a content for this webhook's message.
73    pub fn content(mut self, text: &str) -> Self {
74        self.content = Some(text.to_string());
75        self
76    }
77
78    /// Overrides the default username of this webhook.
79    pub fn username(mut self, name: &str) -> Self {
80        self.username = Some(name.to_string());
81        self
82    }
83
84    /// Sets the avatar url for this webhook.
85    pub fn avatar_url(mut self, url: &str) -> Self {
86        self.avatar_url = Some(url.to_string());
87        self
88    }
89
90    /// Sets the TTS flag for this message.
91    pub fn tts(mut self, opt: bool) -> Self {
92        self.tts = Some(opt);
93        self
94    }
95
96    /// Adds a file to be sent with this webhook's message.
97    pub fn file(mut self, name: &str, file: Vec<u8>) -> Self {
98        self.file = Some((name.to_string(), file));
99        self
100    }
101
102
103    /// Adds an embed to the collection of embeds being sent with this embed.
104    pub fn embed(mut self, embe: Embed) -> Self {
105        self.embeds.push(embe);
106        self
107    }
108}