From aaecc8a9b4c71efbe9aba4ea5e6f148d2020d970 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 8 Dec 2020 12:17:38 -0800 Subject: [PATCH] Added addons.help() --- config/detekt/baseline.xml | 3 +- .../java/net/thauvin/erik/mobibot/Addons.kt | 17 ++++++++ .../java/net/thauvin/erik/mobibot/Mobibot.kt | 40 +------------------ .../erik/mobibot/modules/AbstractModule.kt | 3 +- .../erik/mobibot/modules/CurrencyConverter.kt | 9 +++-- 5 files changed, 28 insertions(+), 44 deletions(-) diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 03d17a4..bfccac4 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -56,7 +56,7 @@ NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties) NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean ) NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) - NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean) + NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean) : Boolean NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ @Throws(IOException::class, FeedException::class) fun loadEntries(file: String, channel: String, entries: MutableList<EntryLink>): String NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List<EntryLink>, history: MutableList<String>, isDayBackup: Boolean ) @@ -75,6 +75,7 @@ NestedBlockDepth:Weather2.kt$Weather2.Companion$ @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List<Message> NestedBlockDepth:WorldTime.kt$WorldTime$override fun commandResponse( sender: String, cmd: String, args: String, isPrivate: Boolean ) ReturnCount:Addons.kt$Addons$ fun exec(sender: String, login: String, cmd: String, args: String, isOp: Boolean, isPrivate: Boolean): Boolean + ReturnCount:Addons.kt$Addons$ fun help(sender: String, topic: String, isOp: Boolean, isPrivate: Boolean): Boolean ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$ @JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List<Message> ThrowsCount:StockQuote.kt$StockQuote.Companion$ @JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List<Message> ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject diff --git a/src/main/java/net/thauvin/erik/mobibot/Addons.kt b/src/main/java/net/thauvin/erik/mobibot/Addons.kt index 9c8e769..147aea0 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Addons.kt +++ b/src/main/java/net/thauvin/erik/mobibot/Addons.kt @@ -121,6 +121,23 @@ class Addons { return false } + /** + * Commands and Modules help. + */ + fun help(sender: String, topic: String, isOp: Boolean, isPrivate: Boolean): Boolean { + for (command in commands) { + if (command.isVisible && command.name.startsWith(topic)) { + return command.helpResponse(topic, sender, isOp, isPrivate) + } + } + for (module in modules) { + if (module.commands.contains(topic)) { + return module.helpResponse(sender, isPrivate) + } + } + return false + } + /** * Sort commands and modules names. */ diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.kt b/src/main/java/net/thauvin/erik/mobibot/Mobibot.kt index ca7caa6..a743c88 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.kt +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.kt @@ -34,11 +34,11 @@ package net.thauvin.erik.mobibot import net.thauvin.erik.mobibot.PinboardUtils.addPin import net.thauvin.erik.mobibot.PinboardUtils.deletePin import net.thauvin.erik.mobibot.PinboardUtils.updatePin +import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.colorize import net.thauvin.erik.mobibot.Utils.ensureDir import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.helpFormat -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.isoLocalDate import net.thauvin.erik.mobibot.Utils.today import net.thauvin.erik.mobibot.commands.AddLog @@ -234,18 +234,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert } } - /** - * Responds with the commands help, if any. - */ - private fun helpCommands(sender: String, topic: String, isPrivate: Boolean): Boolean { - for (command in addons.commands) { - if (command.isVisible && command.name.startsWith(topic)) { - return command.helpResponse(topic, sender, isOp(sender), isPrivate) - } - } - return false - } - /** * Responds with the default help. */ @@ -265,38 +253,14 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert } } - /** - * Responds with the modules help, if any. - */ - private fun helpModules(sender: String, topic: String, isPrivate: Boolean): Boolean { - for (module in addons.modules) { - for (cmd in module.commands) { - if (topic == cmd) { - module.helpResponse(sender, isPrivate) - return true - } - } - } - return false - } /** * Responds with the default, commands or modules help. */ private fun helpResponse(sender: String, topic: String, isPrivate: Boolean) { val isOp = isOp(sender) - if (topic.isBlank()) { + if (topic.isBlank() || !addons.help(sender, topic.toLowerCase().trim(), isOp, isPrivate)) { helpDefault(sender, isOp, isPrivate) - } else { - // Command, Modules or Default - if (!helpCommands(sender, topic, isPrivate) && !helpModules( - sender, - topic.toLowerCase().trim(), - isPrivate - ) - ) { - helpDefault(sender, isOp, isPrivate) - } } } diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt b/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt index 695549d..4fe3d8d 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt @@ -74,10 +74,11 @@ abstract class AbstractModule(val bot: Mobibot) { /** * Responds with the module's help. */ - open fun helpResponse(sender: String, isPrivate: Boolean) { + open fun helpResponse(sender: String, isPrivate: Boolean) : Boolean { for (h in help) { bot.send(sender, Utils.buildCmdSyntax(h, bot.nick, isPrivateMsgEnabled && isPrivate), isPrivate) } + return true } /** diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index 279f748..6d9587a 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -67,12 +67,12 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) { * Converts the specified currencies. */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { - with(bot) { + bot.apply { if (EXCHANGE_RATES.isEmpty()) { try { loadRates() } catch (e: ModuleException) { - if (bot.logger.isWarnEnabled) logger.warn(e.debugMessage, e) + if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) } } @@ -93,8 +93,8 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) { } } - override fun helpResponse(sender: String, isPrivate: Boolean) { - with(bot) { + override fun helpResponse(sender: String, isPrivate: Boolean) : Boolean { + bot.apply { if (EXCHANGE_RATES.isEmpty()) { try { loadRates() @@ -125,6 +125,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) { sendList(sender, ArrayList(EXCHANGE_RATES.keys), 11, isPrivate, isIndent = true) } } + return true } companion object {