Skip to content

Commit 66ccae8

Browse files
GiteaBotMopchowxiaoguanglunny
authored
Fix discord webhook 400 status code when description limit is exceeded (#34084) (#34124)
Backport #34084 by @Mopcho 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: Mopcho <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 0e64893 commit 66ccae8

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

services/webhook/discord.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"unicode/utf8"
1515

1616
webhook_model "code.gitea.io/gitea/models/webhook"
17+
"code.gitea.io/gitea/modules/base"
1718
"code.gitea.io/gitea/modules/git"
1819
"code.gitea.io/gitea/modules/json"
1920
"code.gitea.io/gitea/modules/log"
@@ -101,6 +102,13 @@ var (
101102
redColor = color("ff3232")
102103
)
103104

105+
// https://discord.com/developers/docs/resources/message#embed-object-embed-limits
106+
// Discord has some limits in place for the embeds.
107+
// According to some tests, there is no consistent limit for different character sets.
108+
// For example: 4096 ASCII letters are allowed, but only 2490 emoji characters are allowed.
109+
// To keep it simple, we currently truncate at 2000.
110+
const discordDescriptionCharactersLimit = 2000
111+
104112
type discordConvertor struct {
105113
Username string
106114
AvatarURL string
@@ -307,7 +315,7 @@ func (d discordConvertor) createPayload(s *api.User, title, text, url string, co
307315
Embeds: []DiscordEmbed{
308316
{
309317
Title: title,
310-
Description: text,
318+
Description: base.TruncateString(text, discordDescriptionCharactersLimit),
311319
URL: url,
312320
Color: color,
313321
Author: DiscordEmbedAuthor{

0 commit comments

Comments
 (0)