-
Notifications
You must be signed in to change notification settings - Fork 829
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
Changes from 1 commit
e7a8d86
f0d4ea1
31f2602
d629fe3
9fa35ff
aeec0d0
59c7746
062f99d
9113131
42db579
37e2334
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ package discordgo | |
import ( | ||
"io" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// A Message stores all data related to a specific Discord message. | ||
|
@@ -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. | ||
|
@@ -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 | ||
} | ||
|
||
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 == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("<#[^>]*>") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compile this once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you missed this, move this to line 183 and use |
||
} | ||
|
||
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 | ||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably return ContentWithMentionsReplaced.