Only break from command match loop after successfully executing a command

This means you can have multiple commands with the same or overlappings triggers,
and the bot will execute the first the user has access to.
This commit is contained in:
kageru 2019-08-07 15:13:16 +02:00
parent e3219c7800
commit f871d2a381
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 6 additions and 4 deletions

View File

@ -27,8 +27,9 @@ object Kagebot {
}
for (command in Config.commands) {
if (command.matches(readableMessageContent)) {
command.execute(this)
break
if (command.execute(this)) {
break
}
}
}
}

View File

@ -39,13 +39,13 @@ class Command(cmd: RawCommand) {
fun isAllowed(message: MessageCreateEvent) = permissions?.isAllowed(message) ?: true
fun execute(message: MessageCreateEvent) {
fun execute(message: MessageCreateEvent): Boolean {
if (permissions?.isAllowed(message) == false) {
if (Config.localization.permissionDenied.isNotBlank()) {
message.channel.sendMessage(Config.localization.permissionDenied)
}
Log.info("Denying command ${this.trigger} to user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})")
return
return false
}
Log.info("Executing command ${this.trigger} triggered by user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})")
Globals.commandCounter.incrementAndGet()
@ -57,6 +57,7 @@ class Command(cmd: RawCommand) {
MessageUtil.sendEmbed(message.channel, embed)
}
this.feature?.handle(message)
return true
}
fun matches(msg: String) = this.matchType.matches(msg, this)