Initialize b.AllowedMentions on Discord Bridger init

This commit is contained in:
Reach
2021-05-05 01:55:33 +02:00
parent fd68eb89bc
commit b8876523d6
3 changed files with 28 additions and 21 deletions

View File

@@ -32,6 +32,8 @@ type Bdiscord struct {
userMemberMap map[string]*discordgo.Member
nickMemberMap map[string]*discordgo.Member
allowedMentions *discordgo.MessageAllowedMentions
// Webhook specific logic
useAutoWebhooks bool
transmitter *transmitter.Transmitter
@@ -43,6 +45,8 @@ func New(cfg *bridge.Config) bridge.Bridger {
b.nickMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo)
b.allowedMentions = b.getAllowedMentions()
b.useAutoWebhooks = b.GetBool("AutoWebhooks")
if b.useAutoWebhooks {
b.Log.Debug("Using automatic webhooks")

View File

@@ -9,6 +9,24 @@ import (
"github.com/matterbridge/discordgo"
)
func (b *Bdiscord) getAllowedMentions() *discordgo.MessageAllowedMentions {
// Allow only the mention types that are not disabled by the config (default is all allowed)
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)
}
return &discordgo.MessageAllowedMentions{
Parse: allowedMentionTypes,
}
}
func (b *Bdiscord) getNick(user *discordgo.User, guildID string) string {
b.membersMutex.RLock()
defer b.membersMutex.RUnlock()

View File

@@ -47,9 +47,8 @@ func (b *Bdiscord) maybeGetLocalAvatar(msg *config.Message) string {
// Returns messageID and error.
func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordgo.Message, error) {
var (
res *discordgo.Message
err error
allowedMentionTypes []discordgo.AllowedMentionType
res *discordgo.Message
err error
)
// If avatar is unset, mutate the message to include the local avatar (but only if settings say we should do this)
@@ -57,18 +56,6 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
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`.
// We can't send empty messages.
@@ -76,12 +63,10 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg
res, err = b.transmitter.Send(
channelID,
&discordgo.WebhookParams{
Content: msg.Text,
Username: msg.Username,
AvatarURL: msg.Avatar,
AllowedMentions: &discordgo.MessageAllowedMentions{
Parse: allowedMentionTypes,
},
Content: msg.Text,
Username: msg.Username,
AvatarURL: msg.Avatar,
AllowedMentions: b.allowedMentions,
},
)
if err != nil {