5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 16:20:26 +00:00

Clip too long messages sent to discord (discord). Closes #440

This commit is contained in:
Wim 2018-07-22 00:27:49 +02:00
parent 93307b57aa
commit 2597c9bfac
2 changed files with 19 additions and 0 deletions

View File

@ -13,6 +13,8 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
const MessageLength = 1950
type Bdiscord struct { type Bdiscord struct {
c *discordgo.Session c *discordgo.Session
Channels []*discordgo.Channel Channels []*discordgo.Channel
@ -141,6 +143,8 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
if msg.Text == "" { if msg.Text == "" {
return "", nil return "", nil
} }
msg.Text = helper.ClipMessage(msg.Text, MessageLength)
err := b.c.WebhookExecute( err := b.c.WebhookExecute(
wID, wID,
wToken, wToken,
@ -167,6 +171,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
// Upload a file if it exists // Upload a file if it exists
if msg.Extra != nil { if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) { for _, rmsg := range helper.HandleExtra(&msg, b.General) {
rmsg.Text = helper.ClipMessage(rmsg.Text, MessageLength)
b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text) b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text)
} }
// check if we have files to upload (from slack, telegram or mattermost) // 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 // Edit message
if msg.ID != "" { if msg.ID != "" {
_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text) _, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)

View File

@ -8,6 +8,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"time" "time"
"unicode/utf8"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -115,3 +116,15 @@ func RemoveEmptyNewLines(msg string) string {
lines = strings.TrimRight(lines, "\n") lines = strings.TrimRight(lines, "\n")
return lines 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
}