diff options
Diffstat (limited to 'bridge/discord/webhook.go')
-rw-r--r-- | bridge/discord/webhook.go | 45 |
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 +} |