Skip to content

Commit

Permalink
Split long discord output message into the chunks by 2000 characters (m…
Browse files Browse the repository at this point in the history
…icro#1704)

Signed-off-by: Dmitry Kozlov <[email protected]>
  • Loading branch information
dkozlov authored Jun 15, 2020
1 parent 1179d7e commit a3a1a84
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions agent/input/discord/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,32 @@ func (dc *discordConn) Recv(event *input.Event) error {
}
}

func ChunkString(s string, chunkSize int) []string {
var chunks []string
runes := []rune(s)

if len(runes) == 0 {
return []string{s}
}

for i := 0; i < len(runes); i += chunkSize {
nn := i + chunkSize
if nn > len(runes) {
nn = len(runes)
}
chunks = append(chunks, string(runes[i:nn]))
}
return chunks
}

func (dc *discordConn) Send(e *input.Event) error {
fields := strings.Split(e.To, ":")
_, err := dc.master.session.ChannelMessageSend(fields[0], string(e.Data))
if err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error("[bot][loop][send]", err)
for _, chunk := range ChunkString(string(e.Data), 2000) {
_, err := dc.master.session.ChannelMessageSend(fields[0], chunk)
if err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error("[bot][loop][send]", err)
}
}
}
return nil
Expand Down

0 comments on commit a3a1a84

Please sign in to comment.