Skip to content

Commit 013b268

Browse files
Mopchowxiaoguang
andauthored
Fix discord webhook 400 status code when description limit is exceeded (#34084)
Fixes [#34027](#34027) Discord does not allow for description bigger than 2048 bytes. If the description is bigger than that it will throw 400 and the event won't appear in discord. To fix that, in the createPayload method we now slice the description to ensure it doesn’t exceed the limit. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 6cee3bf commit 013b268

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

services/webhook/discord.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ var (
101101
redColor = color("ff3232")
102102
)
103103

104+
// https://discord.com/developers/docs/resources/message#embed-object-embed-limits
105+
// Discord has some limits in place for the embeds.
106+
// According to some tests, there is no consistent limit for different character sets.
107+
// For example: 4096 ASCII letters are allowed, but only 2490 emoji characters are allowed.
108+
// To keep it simple, we currently truncate at 2000.
109+
const discordDescriptionCharactersLimit = 2000
110+
104111
type discordConvertor struct {
105112
Username string
106113
AvatarURL string
@@ -313,7 +320,7 @@ func (d discordConvertor) createPayload(s *api.User, title, text, url string, co
313320
Embeds: []DiscordEmbed{
314321
{
315322
Title: title,
316-
Description: text,
323+
Description: util.TruncateRunes(text, discordDescriptionCharactersLimit),
317324
URL: url,
318325
Color: color,
319326
Author: DiscordEmbedAuthor{

0 commit comments

Comments
 (0)