Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 14d9ff4b1eefe07f5cddf947540f57c7c886eed5 Author: James Nylen <jnylen@gmail.com> Date: Wed Jan 09 15:28:47 2019 +0000 Use only one webhook if possible (discord) (#681) diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index b6c2ae1..a4eb2bf 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -2011 +2012 @@ type Bdiscord struct {     c *discordgo.Session   - nick string - useChannelID bool - guildID string - webhookID string - webhookToken string + nick string + useChannelID bool + guildID string + webhookID string + webhookToken string + canEditWebhooks bool     channelsMutex sync.RWMutex   channels []*discordgo.Channel @@ -1108 +11126 @@ func (b *Bdiscord) Connect() error {   return err   }   b.channelsMutex.RLock() - for _, channel := range b.channels { - b.Log.Debugf("found channel %#v", channel) + if b.GetString("WebhookURL") == "" { + for _, channel := range b.channels { + b.Log.Debugf("found channel %#v", channel) + } + } else { + b.canEditWebhooks = true + for _, channel := range b.channels { + b.Log.Debugf("found channel %#v; verifying PermissionManageWebhooks", channel) + perms, permsErr := b.c.State.UserChannelPermissions(userinfo.ID, channel.ID) + manageWebhooks := discordgo.PermissionManageWebhooks + if permsErr != nil || perms&manageWebhooks != manageWebhooks { + b.Log.Warnf("Can't manage webhooks in channel \"%s\"", channel.Name) + b.canEditWebhooks = false + } + } + if b.canEditWebhooks { + b.Log.Info("Can manage webhooks; will edit channel for global webhook on send") + } else { + b.Log.Warn("Can't manage webhooks; won't edit channel for global webhook on send") + }   }   b.channelsMutex.RUnlock()   @@ -1667 +1858 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   msg.Text = "_" + msg.Text + "_"   }   - // use initial webhook + // use initial webhook configured for the entire Discord account + isGlobalWebhook := true   wID := b.webhookID   wToken := b.webhookToken   @@ -1756 +1957 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   if ci, ok := b.channelInfoMap[msg.Channel+b.Account]; ok {   if ci.Options.WebhookURL != "" {   wID, wToken = b.splitURL(ci.Options.WebhookURL) + isGlobalWebhook = false   }   }   b.channelsMutex.RUnlock() @@ -2096 +23019 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   if len(msg.Username) > 32 {   msg.Username = msg.Username[0:32]   } + // if we have a global webhook for this Discord account, and permission + // to modify webhooks (previously verified), then set its channel to + // the message channel before using it + // TODO: this isn't necessary if the last message from this webhook was + // sent to the current channel + if isGlobalWebhook && b.canEditWebhooks { + b.Log.Debugf("Setting webhook channel to \"%s\"", msg.Channel) + _, err := b.c.WebhookEdit(wID, "", "", channelID) + if err != nil { + b.Log.Errorf("Could not set webhook channel: %v", err) + return "", err + } + }   err := b.c.WebhookExecute(   wID,   wToken,