Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContentWithMentionReplaced on roles and nicks (Fixes #208) #375

Merged
merged 11 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tweaked a little
  • Loading branch information
LEGOlord208 authored and LEGOlord208 committed May 8, 2017
commit 9fa35fff342df11c17f9dd39f3c8fb41308e54a5
36 changes: 23 additions & 13 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package discordgo
import (
"io"
"regexp"
"strings"
)

// A Message stores all data related to a specific Discord message.
Expand Down Expand Up @@ -176,7 +177,7 @@ func (m *Message) ContentWithMentionsReplaced() (content string) {
return
}

var patternRoles *regexp.Regexp
var patternChannels *regexp.Regexp

// ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
// username of the mention, but also role IDs and more.
Expand All @@ -190,33 +191,42 @@ func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, e

channel, err := s.State.Channel(m.ChannelID)
if err != nil {
content = m.ContentWithMentionsReplaced()
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably return ContentWithMentionsReplaced.

}

for _, user := range m.Mentions {
member, err := s.State.Member(channel.GuildID, user.ID)
if err != nil {
continue
nick := user.Username
if (err == nil && member.Nick != "") || nick == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the nick == "" check is unneeded, a Member will always have a Username.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. No idea why I put that there 😆

nick = member.Nick
}

nick := member.Nick
if nick == "" {
nick = user.Username
content = strings.NewReplacer(
"<@"+user.ID+">", "@"+nick,
"<@!"+user.ID+">", "@"+nick,
).Replace(content)
}
for _, roleID := range m.MentionRoles {
role, err := s.State.Role(channel.GuildID, roleID)
if err != nil || !role.Mentionable {
continue
}
content = regexp.MustCompile("<@!?"+regexp.QuoteMeta(user.ID)+">").ReplaceAllString(content, "@"+nick)

content = strings.Replace(content, "<&"+role.ID+">", "@"+role.Name, -1)
}

if patternRoles == nil {
patternRoles = regexp.MustCompile("<&[^>]*>")
if patternChannels == nil {
patternChannels = regexp.MustCompile("<#[^>]*>")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compile this once.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed this, move this to line 183 and use "<#[0-9]*>"

}

patternRoles.ReplaceAllStringFunc(content, func(mention string) string {
role, err := s.State.Role(channel.GuildID, mention)
if err != nil || !role.Mentionable {
content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
channel, err := s.State.Channel(mention[2 : len(mention)-1])
if err != nil {
return mention
}

return "<@&" + role.ID + ">"
return "#" + channel.Name
})
return
}
41 changes: 41 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package discordgo

import (
"testing"
)

func TestContentWithMoreMentionsReplaced(t *testing.T) {
s := &Session{StateEnabled: true, State: NewState()}

user := &User{
ID: "user",
Username: "User Name",
}

s.StateEnabled = true
s.State.GuildAdd(&Guild{ID: "guild"})
s.State.RoleAdd("guild", &Role{
ID: "role",
Name: "Role Name",
Mentionable: true,
})
s.State.MemberAdd(&Member{
User: user,
Nick: "User Nick",
GuildID: "guild",
})
s.State.ChannelAdd(&Channel{
Name: "Channel Name",
GuildID: "guild",
ID: "channel",
})
m := &Message{
Content: "<&role> <@!user> <@user> <#channel>",
ChannelID: "channel",
MentionRoles: []string{"role"},
Mentions: []*User{user},
}
if result, _ := m.ContentWithMoreMentionsReplaced(s); result != "@Role Name @User Nick @User Nick #Channel Name" {
t.Error(result)
}
}