Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 8ba0441ef3b45d59734c108458c8ae60cf81f7ab Author: Wim <wim@42.be> Date: Sun Jul 22 00:27:49 2018 +0000 Clip too long messages sent to discord (discord). Closes #440 diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index cd06eea..db3fb68 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -136 +138 @@ import (   "github.com/bwmarrin/discordgo"  )   +const MessageLength = 1950 +  type Bdiscord struct {   c *discordgo.Session   Channels []*discordgo.Channel @@ -1416 +1438 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   if msg.Text == "" {   return "", nil   } + + msg.Text = helper.ClipMessage(msg.Text, MessageLength)   err := b.c.WebhookExecute(   wID,   wToken, @@ -1676 +1717 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   // Upload a file if it exists   if msg.Extra != nil {   for _, rmsg := range helper.HandleExtra(&msg, b.General) { + rmsg.Text = helper.ClipMessage(rmsg.Text, MessageLength)   b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text)   }   // check if we have files to upload (from slack, telegram or mattermost) @@ -1756 +1807 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   }   }   + msg.Text = helper.ClipMessage(msg.Text, MessageLength)   // Edit message   if msg.ID != "" {   _, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text) diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go index d0c1062..a13e02a 100644 --- a/bridge/helper/helper.go +++ b/bridge/helper/helper.go @@ -86 +87 @@ import (   "regexp"   "strings"   "time" + "unicode/utf8"     "github.com/42wim/matterbridge/bridge/config"   log "github.com/sirupsen/logrus" @@ -1153 +11615 @@ func RemoveEmptyNewLines(msg string) string {   lines = strings.TrimRight(lines, "\n")   return lines  } + +func ClipMessage(text string, length int) string { + // clip too long messages + if len(text) > length { + text = text[:length-len(" *message clipped*")] + if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError { + text = text[:len(text)-size] + } + text += " *message clipped*" + } + return text +}