Allow users to customize command prefix, split regex and NoSpaceText

This commit is contained in:
Abbas Abou Daya 2020-07-18 01:27:39 -07:00
parent 023772142a
commit e6aae3c282

View File

@ -233,6 +233,18 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
return true; return true;
} }
protected String getCommandPrefix() {
return "/";
}
protected String getCommandRegexSplit() {
return " ";
}
protected boolean allowNoSpaceText() {
return false;
}
/** /**
* Registers the declared abilities using method reflection. Also, replies are accumulated using the built abilities and standalone methods that return a Reply. * Registers the declared abilities using method reflection. Also, replies are accumulated using the built abilities and standalone methods that return a Reply.
* <p> * <p>
@ -494,17 +506,23 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
if (!update.hasMessage() || !msg.hasText()) if (!update.hasMessage() || !msg.hasText())
return Trio.of(update, abilities.get(DEFAULT), new String[]{}); return Trio.of(update, abilities.get(DEFAULT), new String[]{});
String[] tokens = msg.getText().split(" "); Ability ability;
String[] tokens = msg.getText().split(getCommandRegexSplit());
if (tokens[0].startsWith("/")) { if (allowNoSpaceText()) {
String abilityToken = stripBotUsername(tokens[0].substring(1)).toLowerCase(); String abName = abilities.keySet().stream()
Ability ability = abilities.get(abilityToken); .filter(name -> msg.getText().startsWith(format("%s%s", getCommandPrefix(), name)))
tokens = Arrays.copyOfRange(tokens, 1, tokens.length); .findFirst().orElse(DEFAULT);
return Trio.of(update, ability, tokens); ability = abilities.get(abName);
} else { } else {
Ability ability = abilities.get(DEFAULT); if (tokens[0].startsWith(getCommandPrefix())) {
return Trio.of(update, ability, tokens); String abilityToken = stripBotUsername(tokens[0].substring(1)).toLowerCase();
ability = abilities.get(abilityToken);
tokens = Arrays.copyOfRange(tokens, 1, tokens.length);
} else {
ability = abilities.get(DEFAULT);
}
} }
return Trio.of(update, ability, tokens);
} }
private String stripBotUsername(String token) { private String stripBotUsername(String token) {