From f871d2a3812422e05dc989e0284fbbc1549a3a84 Mon Sep 17 00:00:00 2001 From: kageru Date: Wed, 7 Aug 2019 15:13:16 +0200 Subject: [PATCH] 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. --- src/main/kotlin/moe/kageru/kagebot/Kagebot.kt | 5 +++-- src/main/kotlin/moe/kageru/kagebot/command/Command.kt | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt b/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt index cdbe4b7..dae5479 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt @@ -27,8 +27,9 @@ object Kagebot { } for (command in Config.commands) { if (command.matches(readableMessageContent)) { - command.execute(this) - break + if (command.execute(this)) { + break + } } } } diff --git a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt index 07d0ace..bd1a82a 100644 --- a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt +++ b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt @@ -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)