From a3a1a84172b596a43678ff1659410fa05a4a35dd Mon Sep 17 00:00:00 2001 From: Dmitry Kozlov Date: Tue, 16 Jun 2020 00:22:00 +0300 Subject: [PATCH] Split long discord output message into the chunks by 2000 characters (#1704) Signed-off-by: Dmitry Kozlov --- agent/input/discord/conn.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/agent/input/discord/conn.go b/agent/input/discord/conn.go index ac9af037..4766d442 100644 --- a/agent/input/discord/conn.go +++ b/agent/input/discord/conn.go @@ -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