Skip to content

Commit 377b724

Browse files
committed
fix possibly too long message in decode command
1 parent 630445d commit 377b724

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

commands/decode.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"time"
@@ -42,13 +43,16 @@ func (c *Commands) Decode(data discord.SlashCommandInteractionData, e *handler.C
4243
if version > 0 {
4344
content += fmt.Sprintf("track was encoded with version: `%d`\n", version)
4445
}
46+
var decodedData []byte
4547
if decoded != nil {
46-
decodedData, _ := json.MarshalIndent(decoded, "", " ")
47-
content += fmt.Sprintf("```json\n%s\n```", decodedData)
48+
decodedData, _ = json.MarshalIndent(decoded, "", " ")
4849
}
4950

51+
msg := jsonMessage(content, decodedData)
52+
5053
return e.CreateMessage(discord.MessageCreate{
51-
Content: content,
54+
Content: msg.Content,
55+
Files: msg.Files,
5256
})
5357
}
5458

@@ -66,16 +70,36 @@ func (c *Commands) Decode(data discord.SlashCommandInteractionData, e *handler.C
6670
return err
6771
}
6872

69-
decodedData, err := json.MarshalIndent(decoded, "", " ")
70-
if err != nil {
71-
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
72-
Content: json.Ptr(fmt.Sprintf("failed to decode track: %s", err)),
73-
})
74-
return err
75-
}
73+
decodedData, _ := json.MarshalIndent(decoded, "", " ")
74+
75+
msg := jsonMessage("", decodedData)
7676

7777
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
78-
Content: json.Ptr(fmt.Sprintf("```json\n%s\n```", decodedData)),
78+
Content: &msg.Content,
79+
Files: msg.Files,
7980
})
8081
return err
8182
}
83+
84+
type message struct {
85+
Content string
86+
Files []*discord.File
87+
}
88+
89+
func jsonMessage(msg string, jsonData []byte) message {
90+
var (
91+
content string
92+
files []*discord.File
93+
)
94+
95+
if len([]rune(msg))+len(jsonData) > 2020 {
96+
content = msg
97+
files = append(files, discord.NewFile("track.json", "", bytes.NewReader(jsonData)))
98+
} else {
99+
content = fmt.Sprintf("%s\n\n```json\n%s\n```", msg, jsonData)
100+
}
101+
return message{
102+
Content: content,
103+
Files: files,
104+
}
105+
}

0 commit comments

Comments
 (0)