summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWim <wim@42.be>2018-07-22 00:27:49 +0200
committerWim <wim@42.be>2018-07-22 00:28:17 +0200
commit2597c9bfacfbc04d1d77162465039eb9b45d0e4d (patch)
treee3a802f1e2cf35f7cd7f3a7aa1c570910a9fdc5d
parent93307b57aa830b0bfbf052264243f9e047b77089 (diff)
downloadmatterbridge-msglm-2597c9bfacfbc04d1d77162465039eb9b45d0e4d.tar.gz
matterbridge-msglm-2597c9bfacfbc04d1d77162465039eb9b45d0e4d.tar.bz2
matterbridge-msglm-2597c9bfacfbc04d1d77162465039eb9b45d0e4d.zip
Clip too long messages sent to discord (discord). Closes #440
-rw-r--r--bridge/discord/discord.go6
-rw-r--r--bridge/helper/helper.go13
2 files changed, 19 insertions, 0 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go
index cd06eeaf..db3fb68a 100644
--- a/bridge/discord/discord.go
+++ b/bridge/discord/discord.go
@@ -13,6 +13,8 @@ import (
"github.com/bwmarrin/discordgo"
)
+const MessageLength = 1950
+
type Bdiscord struct {
c *discordgo.Session
Channels []*discordgo.Channel
@@ -141,6 +143,8 @@ 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,
@@ -167,6 +171,7 @@ 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)
@@ -175,6 +180,7 @@ 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 d0c1062c..a13e02af 100644
--- a/bridge/helper/helper.go
+++ b/bridge/helper/helper.go
@@ -8,6 +8,7 @@ import (
"regexp"
"strings"
"time"
+ "unicode/utf8"
"github.com/42wim/matterbridge/bridge/config"
log "github.com/sirupsen/logrus"
@@ -115,3 +116,15 @@ 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
+}