Skip to content

Commit

Permalink
text_no_mentions: linear text-based logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 16, 2022
1 parent 6630e13 commit 347b8d7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ObjectTag getContext(String name) {
return new ElementTag(getEvent().getMessage().getId());
case "no_mention_message":
DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
return new ElementTag(DiscordMessageTag.stripMentions(getEvent().getMessage().getContentRaw(), getEvent().getMessage().getMentionedUsers()));
return new ElementTag(DiscordMessageTag.stripMentions(getEvent().getMessage().getContentRaw()));
case "formatted_message":
DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
return new ElementTag(getEvent().getMessage().getContentDisplay());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public ObjectTag getContext(String name) {
return new ElementTag(getEvent().getMessage().getId());
case "no_mention_message":
DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
return new ElementTag(DiscordMessageTag.stripMentions(getEvent().getMessage().getContentRaw(), getEvent().getMessage().getMentionedUsers()));
return new ElementTag(DiscordMessageTag.stripMentions(getEvent().getMessage().getContentRaw()));
case "formatted_message":
DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
return new ElementTag(getEvent().getMessage().getContentDisplay());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public ObjectTag setPrefix(String prefix) {

@Override
public void applyProperty(Mechanism mechanism) {
Debug.echoError("Cannot apply properties to a DiscordChannelTag!");
mechanism.echoError("Cannot apply properties to a DiscordChannelTag!");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.dv8tion.jda.api.entities.*;

Expand Down Expand Up @@ -167,13 +168,28 @@ public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}

public static String stripMentions(String message, List<User> mentioned) {
for (User user : mentioned) {
message = message.replace(user.getAsMention(), "")
.replace("<@" +user.getId() + ">", "")
.replace("<@!" +user.getId() + ">", "");
public static AsciiMatcher digits = new AsciiMatcher(AsciiMatcher.DIGITS);

public static String stripMentions(String message) {
StringBuilder output = new StringBuilder(message.length());
char[] rawChars = message.toCharArray();
for (int i = 0; i < rawChars.length; i++) {
char c = rawChars[i];
if (c == '<' && (i + 3) < rawChars.length && rawChars[i + 1] == '@') {
char next = rawChars[i + 2];
if (digits.isMatch(next) || next == '!' || next == '&') {
int end = message.indexOf('>', i);
if (end > i + 3 && end < i + 32) {
if (digits.isOnlyMatches(message.substring(i + 3, end))) {
i = end;
continue;
}
}
}
}
output.append(c);
}
return message;
return output.toString();
}

public static void registerTags() {
Expand Down Expand Up @@ -254,7 +270,7 @@ public static void registerTags() {
// Returns the text of the message, with '@' mentions removed.
// -->
tagProcessor.registerTag(ElementTag.class, "text_no_mentions", (attribute, object) -> {
return new ElementTag(stripMentions(object.getMessage().getContentRaw(), object.getMessage().getMentionedUsers()));
return new ElementTag(stripMentions(object.getMessage().getContentRaw()));
});

// <--[tag]
Expand Down Expand Up @@ -470,7 +486,7 @@ public ObjectTag setPrefix(String prefix) {

@Override
public void applyProperty(Mechanism mechanism) {
Debug.echoError("Cannot apply properties to a DiscordMessageTag!");
mechanism.echoError("Cannot apply properties to a DiscordMessageTag!");
}

@Override
Expand Down

0 comments on commit 347b8d7

Please sign in to comment.