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
//! [Webhooks](https://docs.discord.com/developers/resources/webhook): post messages to a
//! channel without a bot user.
use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use crate::{
id::{ApplicationId, ChannelId, GuildId, WebhookId},
macros::int_enum,
user::User,
util::ImageHash,
};
/// A [webhook](https://docs.discord.com/developers/resources/webhook#webhook-object).
///
/// The `Debug` output hides the [`token`](Self::token) (C-011).
///
/// discord.js counterpart: `Webhook`.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Webhook {
/// The webhook's ID.
pub id: WebhookId,
/// What kind of webhook this is.
#[serde(rename = "type")]
pub kind: WebhookType,
/// The guild the webhook belongs to.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub guild_id: Option<GuildId>,
/// The channel the webhook posts to.
pub channel_id: Option<ChannelId>,
/// Who created the webhook (absent when the webhook is fetched with its token).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub user: Option<Box<User>>,
/// The default name of the webhook's messages.
pub name: Option<CompactString>,
/// The default [avatar hash](https://docs.discord.com/developers/reference#image-formatting).
pub avatar: Option<ImageHash>,
/// The secret token used to execute the webhook (incoming webhooks only).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub token: Option<CompactString>,
/// The application that created the webhook.
pub application_id: Option<ApplicationId>,
/// The guild of the followed channel (channel follower webhooks only).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_guild: Option<SourceGuild>,
/// The followed channel (channel follower webhooks only).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_channel: Option<SourceChannel>,
/// The URL to execute the webhook (from the `webhook.incoming` `OAuth2` flow).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<CompactString>,
}
impl core::fmt::Debug for Webhook {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Webhook")
.field("id", &self.id)
.field("kind", &self.kind)
.field("guild_id", &self.guild_id)
.field("channel_id", &self.channel_id)
.field("user", &self.user)
.field("name", &self.name)
.field("avatar", &self.avatar)
.field("token", &self.token.as_ref().map(|_| "<redacted>"))
.field("application_id", &self.application_id)
.field("source_guild", &self.source_guild)
.field("source_channel", &self.source_channel)
.field("url", &self.url.as_ref().map(|_| "<redacted>"))
.finish()
}
}
int_enum! {
/// The [kind of a webhook](https://docs.discord.com/developers/resources/webhook#webhook-object-webhook-types).
///
/// discord.js counterpart: `WebhookType`.
pub enum WebhookType: u8 {
/// Posts messages with a generated token.
Incoming = 1,
/// Internal webhook that posts messages of a followed announcement channel.
ChannelFollower = 2,
/// Used by interactions.
Application = 3,
}
}
/// The guild of the channel a channel follower webhook follows.
///
/// discord.js counterpart: `APIWebhookSourceGuild` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SourceGuild {
/// The guild's ID.
pub id: GuildId,
/// The guild's name.
pub name: CompactString,
/// The guild's [icon hash](https://docs.discord.com/developers/reference#image-formatting).
pub icon: Option<ImageHash>,
}
/// The channel a channel follower webhook follows.
///
/// discord.js counterpart: `APIWebhookSourceChannel` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SourceChannel {
/// The channel's ID.
pub id: ChannelId,
/// The channel's name.
pub name: CompactString,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn token_is_redacted_in_debug() {
let webhook: Webhook = serde_json::from_str(
r#"{"id":"1","type":1,"channel_id":"2","name":"hook","avatar":null,
"token":"secret-token","application_id":null}"#,
)
.unwrap();
let debug = format!("{webhook:?}");
assert!(!debug.contains("secret-token"));
assert!(debug.contains("<redacted>"));
assert_eq!(webhook.kind, WebhookType::Incoming);
}
}