summaryrefslogtreecommitdiffstats
path: root/bridge/discord/webhook.go
diff options
context:
space:
mode:
authorQais Patankar <qaisjp@gmail.com>2020-12-06 17:47:10 +0000
committerWim <wim@42.be>2020-12-13 23:19:48 +0100
commit03dc51ffa2c07c3bb35e834971650bbf1110e88e (patch)
treef1128641696dfb05cd5ff97923896699faa1b418 /bridge/discord/webhook.go
parentaef2dcdfddd1a5b0a06e918cc878f3b52f7babf6 (diff)
downloadmatterbridge-msglm-03dc51ffa2c07c3bb35e834971650bbf1110e88e.tar.gz
matterbridge-msglm-03dc51ffa2c07c3bb35e834971650bbf1110e88e.tar.bz2
matterbridge-msglm-03dc51ffa2c07c3bb35e834971650bbf1110e88e.zip
Split Bdiscord.Send into handleEventWebhook and handleEventBotUser (discord)
Diffstat (limited to 'bridge/discord/webhook.go')
-rw-r--r--bridge/discord/webhook.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/bridge/discord/webhook.go b/bridge/discord/webhook.go
index 5c6f63a1..16938bc3 100644
--- a/bridge/discord/webhook.go
+++ b/bridge/discord/webhook.go
@@ -4,6 +4,7 @@ import (
"bytes"
"github.com/42wim/matterbridge/bridge/config"
+ "github.com/42wim/matterbridge/bridge/helper"
"github.com/matterbridge/discordgo"
)
@@ -100,3 +101,47 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
}
return res, err
}
+
+func (b *Bdiscord) handleEventWebhook(msg *config.Message, channelID string) (string, error) {
+ // skip events
+ if msg.Event != "" && msg.Event != config.EventUserAction && msg.Event != config.EventJoinLeave && msg.Event != config.EventTopicChange {
+ return "", nil
+ }
+
+ // skip empty messages
+ if msg.Text == "" && (msg.Extra == nil || len(msg.Extra["file"]) == 0) {
+ b.Log.Debugf("Skipping empty message %#v", msg)
+ return "", nil
+ }
+
+ msg.Text = helper.ClipMessage(msg.Text, MessageLength)
+ msg.Text = b.replaceUserMentions(msg.Text)
+ // discord username must be [0..32] max
+ if len(msg.Username) > 32 {
+ msg.Username = msg.Username[0:32]
+ }
+
+ if msg.ID != "" {
+ b.Log.Debugf("Editing webhook message")
+ err := b.transmitter.Edit(channelID, msg.ID, &discordgo.WebhookParams{
+ Content: msg.Text,
+ Username: msg.Username,
+ })
+ if err == nil {
+ return msg.ID, nil
+ }
+ b.Log.Errorf("Could not edit webhook message: %s", err)
+ }
+
+ b.Log.Debugf("Processing webhook sending for message %#v", msg)
+ discordMsg, err := b.webhookSend(msg, channelID)
+ if err != nil {
+ b.Log.Errorf("Could not broadcast via webook for message %#v: %s", msg, err)
+ return "", err
+ }
+ if discordMsg == nil {
+ return "", nil
+ }
+
+ return discordMsg.ID, nil
+}