Split long discord output message into the chunks by 2000 characters (#1704)

Signed-off-by: Dmitry Kozlov <dmitry.f.kozlov@gmail.com>
This commit is contained in:
Dmitry Kozlov 2020-06-16 00:22:00 +03:00 committed by Dominic Wong
parent cca8826a1f
commit 5b161b88f7

View File

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