Updates per review feedback

This commit is contained in:
James Nylen
2019-01-08 22:46:35 -05:00
parent 009b63a78c
commit aabf5dfed8

View File

@@ -25,6 +25,7 @@ type Bdiscord struct {
guildID string guildID string
webhookID string webhookID string
webhookToken string webhookToken string
canEditWebhooks bool
channelsMutex sync.RWMutex channelsMutex sync.RWMutex
channels []*discordgo.Channel channels []*discordgo.Channel
@@ -110,9 +111,27 @@ func (b *Bdiscord) Connect() error {
return err return err
} }
b.channelsMutex.RLock() b.channelsMutex.RLock()
if b.GetString("WebhookURL") == "" {
for _, channel := range b.channels { for _, channel := range b.channels {
b.Log.Debugf("found channel %#v", channel) 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, err := b.c.State.UserChannelPermissions(userinfo.ID, channel.ID)
manageWebhooks := discordgo.PermissionManageWebhooks
if err != 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() b.channelsMutex.RUnlock()
// Obtaining guild members and initializing nickname mapping. // Obtaining guild members and initializing nickname mapping.
@@ -211,11 +230,12 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
if len(msg.Username) > 32 { if len(msg.Username) > 32 {
msg.Username = msg.Username[0:32] msg.Username = msg.Username[0:32]
} }
// if we have a global webhook for this Discord account, use it, but // if we have a global webhook for this Discord account, and permission
// first set its channel to the message channel // 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 // TODO: this isn't necessary if the last message from this webhook was
// sent to the current channel // sent to the current channel
if isGlobalWebhook { if isGlobalWebhook && b.canEditWebhooks {
b.Log.Debugf("Setting webhook channel to \"%s\"", msg.Channel) b.Log.Debugf("Setting webhook channel to \"%s\"", msg.Channel)
_, err := b.c.WebhookEdit(wID, "", "", channelID) _, err := b.c.WebhookEdit(wID, "", "", channelID)
if err != nil { if err != nil {