diff options
author | Qais Patankar <qaisjp@gmail.com> | 2019-07-09 05:18:37 +0900 |
---|---|---|
committer | Wim <wim@42.be> | 2019-07-08 22:18:37 +0200 |
commit | e60949ff3f84e899d25103f85ed948d26a1b44c3 (patch) | |
tree | f008599b74b2c8d6de54af66c02c2886904c4987 /bridge/discord | |
parent | 278a3c68905605da73435f4173cca655b46721e4 (diff) | |
download | matterbridge-msglm-e60949ff3f84e899d25103f85ed948d26a1b44c3.tar.gz matterbridge-msglm-e60949ff3f84e899d25103f85ed948d26a1b44c3.tar.bz2 matterbridge-msglm-e60949ff3f84e899d25103f85ed948d26a1b44c3.zip |
Support webhook message deletions (discord) (#853)
* Support webhook message deletions (discord)
Messages sent via webhook can now be deleted. It seems it can do this
without any special permissions.
This copies discordgo.WebhookExecute and makes it support the returning
of discordgo.Message.
A pull request has been sent upstream, so we should use that if
@bwmariin accepts the pull request:
https://github.com/bwmarrin/discordgo/pull/663
Changes in behaviour (webhook mode only):
- Previously messages *edited* on other platforms would just be
retransmitted as a brand new message to Discord.
- Message *edits* will now be ignored.
- Debug: message edits will now print out a "permission error".
In the future it may be good to send an "message edited" react to those
webhook messages, so at least people know that the message was edited on
other platforms. (Even though it can't actually show the new message.)
Alternatively, message edits could just send a brand new message with a
link back to the old one. This is a little ugly but it would ensure that
Discord users are able to see the edited message. These "message edit
notifications" would be sent from the bot user (not from a webhook), so
we could edit the "edit notification" if subsequent edits to the
original message are made.
Diffstat (limited to 'bridge/discord')
-rw-r--r-- | bridge/discord/discord.go | 16 | ||||
-rw-r--r-- | bridge/discord/helpers.go | 24 |
2 files changed, 37 insertions, 3 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index ea43bd20..4c1c0f46 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -209,11 +209,21 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { b.channelsMutex.RUnlock() // Use webhook to send the message - if wID != "" { + if wID != "" && msg.Event != config.EventMsgDelete { // skip events if msg.Event != "" && msg.Event != config.EventJoinLeave && msg.Event != config.EventTopicChange { return "", nil } + + // If we are editing a message, delete the old message + if msg.ID != "" { + b.Log.Debugf("Deleting edited webhook message") + err := b.c.ChannelMessageDelete(channelID, msg.ID) + if err != nil { + b.Log.Errorf("Could not delete edited webhook message: %s", err) + } + } + b.Log.Debugf("Broadcasting using Webhook") for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) @@ -251,7 +261,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { return "", err } } - err := b.c.WebhookExecute( + msg, err := b.webhookExecute( wID, wToken, true, @@ -260,7 +270,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { Username: msg.Username, AvatarURL: msg.Avatar, }) - return "", err + return msg.ID, err } b.Log.Debugf("Broadcasting using token (API)") diff --git a/bridge/discord/helpers.go b/bridge/discord/helpers.go index 7b060817..e150d192 100644 --- a/bridge/discord/helpers.go +++ b/bridge/discord/helpers.go @@ -1,6 +1,7 @@ package bdiscord import ( + "encoding/json" "errors" "regexp" "strings" @@ -187,3 +188,26 @@ func enumerateUsernames(s string) []string { } return usernames } + +// webhookExecute executes a webhook. +// webhookID: The ID of a webhook. +// token : The auth token for the webhook +// wait : Waits for server confirmation of message send and ensures that the return struct is populated (it is nil otherwise) +func (b *Bdiscord) webhookExecute(webhookID, token string, wait bool, data *discordgo.WebhookParams) (st *discordgo.Message, err error) { + uri := discordgo.EndpointWebhookToken(webhookID, token) + + if wait { + uri += "?wait=true" + } + response, err := b.c.RequestWithBucketID("POST", uri, data, discordgo.EndpointWebhookToken("", "")) + if !wait || err != nil { + return nil, err + } + + err = json.Unmarshal(response, &st) + if err != nil { + return nil, discordgo.ErrJSONUnmarshal + } + + return st, nil +} |