From 5193634a5210857e1fe49fc1c2d4297912794935 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 9 Jan 2019 15:28:47 -0500 Subject: Use only one webhook if possible (discord) (#681) --- bridge/discord/discord.go | 50 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index b6c2ae11..a4eb2bfd 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -20,11 +20,12 @@ 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 @@ -110,8 +111,26 @@ 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() @@ -166,7 +185,8 @@ 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 @@ -175,6 +195,7 @@ 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() @@ -209,6 +230,19 @@ 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, -- cgit v1.2.3