Skip to content
Open
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
4 changes: 4 additions & 0 deletions jda/src/main/java/co/aikar/commands/CommandConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
public interface CommandConfig extends CommandConfigProvider {
@NotNull List<String> getCommandPrefixes();

default boolean mentionPrefixEnabled() {
return false;
}

@Override
default CommandConfig provide(MessageReceivedEvent event) {
return this;
Expand Down
10 changes: 10 additions & 0 deletions jda/src/main/java/co/aikar/commands/JDACommandConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class JDACommandConfig implements CommandConfig {
protected @NotNull List<String> commandPrefixes = new CopyOnWriteArrayList<>(new String[]{"!"});
protected boolean useMentionPrefix = false;

public JDACommandConfig() {

Expand All @@ -16,4 +17,13 @@ public JDACommandConfig() {
public List<String> getCommandPrefixes() {
return commandPrefixes;
}

public void useMentionPrefix(boolean useMentionPrefix) {
this.useMentionPrefix = useMentionPrefix;
}

@Override
public boolean mentionPrefixEnabled() {
return this.useMentionPrefix;
}
}
12 changes: 11 additions & 1 deletion jda/src/main/java/co/aikar/commands/JDACommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.SelfUser;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -237,6 +238,7 @@ public void log(LogLevel level, String message, Throwable throwable) {
void dispatchEvent(MessageReceivedEvent event) {
Message message = event.getMessage();
String msg = message.getContentRaw();
String selfUserId = event.getJDA().getSelfUser().getId();

CommandConfig config = getCommandConfig(event);

Expand All @@ -247,11 +249,19 @@ void dispatchEvent(MessageReceivedEvent event) {
break;
}
}
if (prefixFound == null && config.mentionPrefixEnabled()) {
if (msg.startsWith("<@" + selfUserId + ">")) {
prefixFound = "<@" + selfUserId + ">";
} else if (msg.startsWith("<@!" + selfUserId + ">")) {
prefixFound = "<@!" + selfUserId + ">";
}
}
if (prefixFound == null) {
return;
}

String[] args = ACFPatterns.SPACE.split(msg.substring(prefixFound.length()), -1);
// str.replaceAll("^[ \t]+", "") - works like String.stripLeading() from Java 11
String[] args = ACFPatterns.SPACE.split(msg.substring(prefixFound.length()).replaceAll("^[ \\t]+", ""), -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say that this pattern should be cached

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, now I'm using ACFPatterns.getPattern() which caches patterns

if (args.length == 0) {
return;
}
Expand Down