diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 49dfe4bc..b75692a9 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -208,7 +208,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { b.channelsMutex.RUnlock() // Use webhook to send the message - if wID != "" { + if wID != "" && msg.ID == "" { // skip events if msg.Event != "" && msg.Event != config.EventJoinLeave && msg.Event != config.EventTopicChange { return "", nil @@ -250,7 +250,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { return "", err } } - err := b.c.WebhookExecute( + msg, err := b.webhookExecute( wID, wToken, true, @@ -259,7 +259,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..0bab8063 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 + } + + err = json.Unmarshal(response, &st) + if err != nil { + return nil, discordgo.ErrJSONUnmarshal + } + + return +}