Add basic AllowedMentions behavior to discord webhooks

This commit is contained in:
Reach
2021-05-05 01:24:43 +02:00
parent 4a27dd0591
commit fd68eb89bc

View File

@@ -47,8 +47,9 @@ func (b *Bdiscord) maybeGetLocalAvatar(msg *config.Message) string {
// Returns messageID and error. // Returns messageID and error.
func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordgo.Message, error) { func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordgo.Message, error) {
var ( var (
res *discordgo.Message res *discordgo.Message
err error err error
allowedMentionTypes []discordgo.AllowedMentionType
) )
// If avatar is unset, mutate the message to include the local avatar (but only if settings say we should do this) // If avatar is unset, mutate the message to include the local avatar (but only if settings say we should do this)
@@ -56,6 +57,18 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
msg.Avatar = b.maybeGetLocalAvatar(msg) msg.Avatar = b.maybeGetLocalAvatar(msg)
} }
// Allow only the mention types that are not disabled by the config
allowedMentionTypes = make([]discordgo.AllowedMentionType, 0, 3)
if !b.GetBool("DisablePingEveryoneHere") {
allowedMentionTypes = append(allowedMentionTypes, discordgo.AllowedMentionTypeEveryone)
}
if !b.GetBool("DisablePingRoles") {
allowedMentionTypes = append(allowedMentionTypes, discordgo.AllowedMentionTypeRoles)
}
if !b.GetBool("DisablePingUsers") {
allowedMentionTypes = append(allowedMentionTypes, discordgo.AllowedMentionTypeUsers)
}
// WebhookParams can have either `Content` or `File`. // WebhookParams can have either `Content` or `File`.
// We can't send empty messages. // We can't send empty messages.
@@ -66,6 +79,9 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
Content: msg.Text, Content: msg.Text,
Username: msg.Username, Username: msg.Username,
AvatarURL: msg.Avatar, AvatarURL: msg.Avatar,
AllowedMentions: &discordgo.MessageAllowedMentions{
Parse: allowedMentionTypes,
},
}, },
) )
if err != nil { if err != nil {