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

Add option to use bot mention as prefix #302

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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;
}
}
14 changes: 13 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 All @@ -17,6 +18,8 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JDACommandManager extends CommandManager<
MessageReceivedEvent,
Expand Down Expand Up @@ -237,6 +240,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 +251,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);
String msgNoPrefix = msg.substring(prefixFound.length());
String[] args = ACFPatterns.SPACE.split(ACFUtil.replace(msgNoPrefix, ACFPatterns.getPattern("^[ ]+"), ""), -1);
if (args.length == 0) {
return;
}
Expand Down