Merge branch 'discord-incoming-mentions'
This commit is contained in:
@@ -2,6 +2,7 @@ package bdiscord
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -21,6 +22,7 @@ type Bdiscord struct {
|
|||||||
Nick string
|
Nick string
|
||||||
UseChannelID bool
|
UseChannelID bool
|
||||||
userMemberMap map[string]*discordgo.Member
|
userMemberMap map[string]*discordgo.Member
|
||||||
|
nickMemberMap map[string]*discordgo.Member
|
||||||
guildID string
|
guildID string
|
||||||
webhookID string
|
webhookID string
|
||||||
webhookToken string
|
webhookToken string
|
||||||
@@ -181,6 +183,8 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg.Text = helper.ClipMessage(msg.Text, MessageLength)
|
msg.Text = helper.ClipMessage(msg.Text, MessageLength)
|
||||||
|
msg.Text = b.replaceUserMentions(msg.Text)
|
||||||
|
|
||||||
// Edit message
|
// Edit message
|
||||||
if msg.ID != "" {
|
if msg.ID != "" {
|
||||||
_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
|
_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
|
||||||
@@ -316,6 +320,7 @@ func (b *Bdiscord) getNick(user *discordgo.User) string {
|
|||||||
return user.Username
|
return user.Username
|
||||||
}
|
}
|
||||||
b.userMemberMap[user.ID] = member
|
b.userMemberMap[user.ID] = member
|
||||||
|
b.nickMemberMap[member.Nick] = member
|
||||||
// only return if nick is set
|
// only return if nick is set
|
||||||
if b.userMemberMap[user.ID].Nick != "" {
|
if b.userMemberMap[user.ID].Nick != "" {
|
||||||
return b.userMemberMap[user.ID].Nick
|
return b.userMemberMap[user.ID].Nick
|
||||||
@@ -323,6 +328,18 @@ func (b *Bdiscord) getNick(user *discordgo.User) string {
|
|||||||
return user.Username
|
return user.Username
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bdiscord) getGuildMemberByNick(nick string) (*discordgo.Member, error) {
|
||||||
|
b.Lock()
|
||||||
|
defer b.Unlock()
|
||||||
|
if _, ok := b.nickMemberMap[nick]; ok {
|
||||||
|
if b.nickMemberMap[nick] != nil {
|
||||||
|
return b.nickMemberMap[nick], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("Couldn't find guild member with nick " + nick) // This will most likely get ignored by the caller
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bdiscord) getChannelID(name string) string {
|
func (b *Bdiscord) getChannelID(name string) string {
|
||||||
idcheck := strings.Split(name, "ID:")
|
idcheck := strings.Split(name, "ID:")
|
||||||
if len(idcheck) > 1 {
|
if len(idcheck) > 1 {
|
||||||
@@ -364,6 +381,21 @@ func (b *Bdiscord) replaceChannelMentions(text string) string {
|
|||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bdiscord) replaceUserMentions(text string) string {
|
||||||
|
re := regexp.MustCompile("@[^ ]+")
|
||||||
|
text = re.ReplaceAllStringFunc(text, func(m string) string {
|
||||||
|
mention := m[1:]
|
||||||
|
b.Log.Debugf("Testing mention: '%s'", mention)
|
||||||
|
member, err := b.getGuildMemberByNick(mention)
|
||||||
|
if err != nil {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
return member.User.Mention()
|
||||||
|
})
|
||||||
|
b.Log.Debugf("Message with mention replaced: %s", text)
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bdiscord) replaceAction(text string) (string, bool) {
|
func (b *Bdiscord) replaceAction(text string) (string, bool) {
|
||||||
if strings.HasPrefix(text, "_") && strings.HasSuffix(text, "_") {
|
if strings.HasPrefix(text, "_") && strings.HasSuffix(text, "_") {
|
||||||
return strings.Replace(text, "_", "", -1), true
|
return strings.Replace(text, "_", "", -1), true
|
||||||
|
|||||||
Reference in New Issue
Block a user