Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
Fix mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
132ikl committed May 28, 2020
1 parent 5544a47 commit 35ee773
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,32 +118,53 @@ protected void handle(MessageData messageData) {
private String formatMessage(String message) {
Guild guild = Main.channel.getGuild().block();

// role mentions: @Staff
Pattern roleMentions = Pattern.compile("(@\\w+)");
Matcher roleMentionMatcher = roleMentions.matcher(message);
while (roleMentionMatcher.find()) {
for (int i = 0; i <= roleMentionMatcher.groupCount(); i++) {
String roleMention = roleMentionMatcher.group(i);

// Remove extra @
String name = roleMention.substring(1);

Role role = Main.getRoleByName(name);
if (role != null) {
message = message.replace(roleMention, String.valueOf(role));
message = message.replace(roleMention, role.getMention());
}
}
}

Pattern userMentions = Pattern.compile("(@\\(.+\\))");
// user pings: @User
Pattern userMentions = Pattern.compile("(@\\w+)");
Matcher userMentionMatcher = userMentions.matcher(message);
while (userMentionMatcher.find()) {
for (int i = 0; i <= userMentionMatcher.groupCount(); i++) {
String userMention = userMentionMatcher.group(i);

// Remove @ and ()
// Remove extra @
String name = userMention.substring(1);

Member member = Main.getMemberByName(name);
if (member != null) {
message = message.replace(userMention, member.getNicknameMention());
}
}
}

// multiword user pings: @(Cool User)
Pattern wordMentions = Pattern.compile("(@\\(.+\\))");
Matcher wordMentionMatcher = wordMentions.matcher(message);
while (wordMentionMatcher.find()) {
for (int i = 0; i <= wordMentionMatcher.groupCount(); i++) {
String userMention = wordMentionMatcher.group(i);

// Remove extra @ and ()
String name = userMention.substring(2, (userMention.indexOf(')')));

Member member = Main.getMemberByName(name);
if (member != null) {
message = message.replace(userMention, String.valueOf(member));
message = message.replace(userMention, member.getNicknameMention());
}
}
}
Expand Down

0 comments on commit 35ee773

Please sign in to comment.