Skip to content

Commit c46772d

Browse files
authored
Add default avatar in discord notifications (#5890)
1 parent 4bdb616 commit c46772d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

server/notification-providers/discord.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,28 @@ class Discord extends NotificationProvider {
1818
webhookUrl.searchParams.append("thread_id", notification.threadId);
1919
}
2020

21+
// Check if the webhook has an avatar
22+
let webhookHasAvatar = true;
23+
try {
24+
const webhookInfo = await axios.get(webhookUrl.toString());
25+
webhookHasAvatar = !!webhookInfo.data.avatar;
26+
} catch (e) {
27+
// If we can't verify, we assume he has an avatar to avoid forcing the default avatar
28+
webhookHasAvatar = true;
29+
}
30+
2131
// If heartbeatJSON is null, assume we're testing.
2232
if (heartbeatJSON == null) {
2333
let discordtestdata = {
2434
username: discordDisplayName,
2535
content: msg,
2636
};
27-
37+
if (!webhookHasAvatar) {
38+
discordtestdata.avatar_url = "https://github.com/louislam/uptime-kuma/raw/master/public/icon.png";
39+
}
2840
if (notification.discordChannelType === "createNewForumPost") {
2941
discordtestdata.thread_name = notification.postName;
3042
}
31-
3243
await axios.post(webhookUrl.toString(), discordtestdata);
3344
return okMsg;
3445
}
@@ -61,6 +72,9 @@ class Discord extends NotificationProvider {
6172
],
6273
}],
6374
};
75+
if (!webhookHasAvatar) {
76+
discorddowndata.avatar_url = "https://github.com/louislam/uptime-kuma/raw/master/public/icon.png";
77+
}
6478
if (notification.discordChannelType === "createNewForumPost") {
6579
discorddowndata.thread_name = notification.postName;
6680
}
@@ -98,6 +112,9 @@ class Discord extends NotificationProvider {
98112
],
99113
}],
100114
};
115+
if (!webhookHasAvatar) {
116+
discordupdata.avatar_url = "https://github.com/louislam/uptime-kuma/raw/master/public/icon.png";
117+
}
101118

102119
if (notification.discordChannelType === "createNewForumPost") {
103120
discordupdata.thread_name = notification.postName;

0 commit comments

Comments
 (0)