Extract maybeGetLocalAvatar into its own function (discord)

This commit is contained in:
Qais Patankar 2020-12-05 15:56:43 +00:00 committed by Wim
parent 0a17e21119
commit 0494119bf4

View File

@ -382,17 +382,9 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
err error err error
) )
// If avatar is unset, check if UseLocalAvatar contains the message's // If avatar is unset, mutate the message to include the local avatar (but only if settings say we should do this)
// account or protocol, and if so, try to find a local avatar
if msg.Avatar == "" { if msg.Avatar == "" {
for _, val := range b.GetStringSlice("UseLocalAvatar") { msg.Avatar = b.maybeGetLocalAvatar(msg)
if msg.Protocol == val || msg.Account == val {
if avatar := b.findAvatar(msg); avatar != "" {
msg.Avatar = avatar
}
break
}
}
} }
// WebhookParams can have either `Content` or `File`. // WebhookParams can have either `Content` or `File`.
@ -441,10 +433,20 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
return res, err return res, err
} }
func (b *Bdiscord) findAvatar(m *config.Message) string { // maybeGetLocalAvatar checks if UseLocalAvatar contains the message's
member, err := b.getGuildMemberByNick(m.Username) // account or protocol, and if so, returns the Discord avatar (if exists)
if err != nil { func (b *Bdiscord) maybeGetLocalAvatar(msg *config.Message) string {
return "" for _, val := range b.GetStringSlice("UseLocalAvatar") {
if msg.Protocol != val && msg.Account != val {
continue
}
member, err := b.getGuildMemberByNick(msg.Username)
if err != nil {
return ""
}
return member.User.AvatarURL("")
} }
return member.User.AvatarURL("") return ""
} }