Merge pull request #631 from jugendhacker/patch-jugendhacker-1
Fixed case insensitive usernames
This commit is contained in:
commit
777dc843ef
@ -28,10 +28,22 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
|
|||||||
* Creates a TelegramLongPollingCommandBot using default options
|
* Creates a TelegramLongPollingCommandBot using default options
|
||||||
* Use ICommandRegistry's methods on this bot to register commands
|
* Use ICommandRegistry's methods on this bot to register commands
|
||||||
*
|
*
|
||||||
* @param botUsername Username of the bot
|
|
||||||
*/
|
*/
|
||||||
public TelegramLongPollingCommandBot(String botUsername) {
|
public TelegramLongPollingCommandBot() {
|
||||||
this(ApiContext.getInstance(DefaultBotOptions.class), botUsername);
|
this(ApiContext.getInstance(DefaultBotOptions.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a TelegramLongPollingCommandBot using default options
|
||||||
|
* Use ICommandRegistry's methods on this bot to register commands
|
||||||
|
*
|
||||||
|
* @param botUsername Username of the bot
|
||||||
|
* @deprecated Overwrite {@link #getBotUsername() getBotUsername} instead
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public TelegramLongPollingCommandBot(String botUsername){
|
||||||
|
this();
|
||||||
|
this.botUsername = botUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,10 +52,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
|
|||||||
* Use ICommandRegistry's methods on this bot to register commands
|
* Use ICommandRegistry's methods on this bot to register commands
|
||||||
*
|
*
|
||||||
* @param options Bot options
|
* @param options Bot options
|
||||||
* @param botUsername Username of the bot
|
|
||||||
*/
|
*/
|
||||||
public TelegramLongPollingCommandBot(DefaultBotOptions options, String botUsername) {
|
public TelegramLongPollingCommandBot(DefaultBotOptions options) {
|
||||||
this(options, true, botUsername);
|
this(options, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,12 +64,10 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
|
|||||||
* @param options Bot options
|
* @param options Bot options
|
||||||
* @param allowCommandsWithUsername true to allow commands with parameters (default),
|
* @param allowCommandsWithUsername true to allow commands with parameters (default),
|
||||||
* false otherwise
|
* false otherwise
|
||||||
* @param botUsername bot username of this bot
|
|
||||||
*/
|
*/
|
||||||
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername, String botUsername) {
|
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) {
|
||||||
super(options);
|
super(options);
|
||||||
this.botUsername = botUsername;
|
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this.getBotUsername());
|
||||||
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, botUsername);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -143,9 +152,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
|
|||||||
* @return Bot username
|
* @return Bot username
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final String getBotUsername() {
|
public String getBotUsername(){
|
||||||
return botUsername;
|
return this.botUsername;
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process all updates, that are not commands.
|
* Process all updates, that are not commands.
|
||||||
|
@ -8,6 +8,7 @@ import java.util.Collection;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class manages all the commands for a bot. You can register and deregister commands on demand
|
* This class manages all the commands for a bot. You can register and deregister commands on demand
|
||||||
@ -122,7 +123,7 @@ public final class CommandRegistry implements ICommandRegistry {
|
|||||||
*/
|
*/
|
||||||
private String removeUsernameFromCommandIfNeeded(String command) {
|
private String removeUsernameFromCommandIfNeeded(String command) {
|
||||||
if (allowCommandsWithUsername) {
|
if (allowCommandsWithUsername) {
|
||||||
return command.replace("@" + botUsername, "").trim();
|
return command.replaceAll("(?i)@" + Pattern.quote(botUsername), "").trim();
|
||||||
}
|
}
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user