Skip to content

Commit f69d71e

Browse files
Mopchowxiaoguang
authored andcommitted
Fix discord webhook 400 status code when description limit is exceeded (go-gitea#34084)
Fixes [go-gitea#34027](go-gitea#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 0e64893 commit f69d71e

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
@@ -307,7 +314,7 @@ func (d discordConvertor) createPayload(s *api.User, title, text, url string, co
307314
Embeds: []DiscordEmbed{
308315
{
309316
Title: title,
310-
Description: text,
317+
Description: util.TruncateRunes(text, discordDescriptionCharactersLimit),
311318
URL: url,
312319
Color: color,
313320
Author: DiscordEmbedAuthor{

0 commit comments

Comments
 (0)