Added addons.help()

This commit is contained in:
Erik C. Thauvin 2020-12-08 12:17:38 -08:00
parent 4b52ab8c28
commit aaecc8a9b4
5 changed files with 28 additions and 44 deletions

View file

@ -56,7 +56,7 @@
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties)</ID> <ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties)</ID>
<ID>NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID> <ID>NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID> <ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean)</ID> <ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean) : Boolean</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message</ID> <ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message</ID>
<ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ @Throws(IOException::class, FeedException::class) fun loadEntries(file: String, channel: String, entries: MutableList&lt;EntryLink&gt;): String</ID> <ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ @Throws(IOException::class, FeedException::class) fun loadEntries(file: String, channel: String, entries: MutableList&lt;EntryLink&gt;): String</ID>
<ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List&lt;EntryLink&gt;, history: MutableList&lt;String&gt;, isDayBackup: Boolean )</ID> <ID>NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List&lt;EntryLink&gt;, history: MutableList&lt;String&gt;, isDayBackup: Boolean )</ID>
@ -75,6 +75,7 @@
<ID>NestedBlockDepth:Weather2.kt$Weather2.Companion$ @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List&lt;Message&gt;</ID> <ID>NestedBlockDepth:Weather2.kt$Weather2.Companion$ @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List&lt;Message&gt;</ID>
<ID>NestedBlockDepth:WorldTime.kt$WorldTime$override fun commandResponse( sender: String, cmd: String, args: String, isPrivate: Boolean )</ID> <ID>NestedBlockDepth:WorldTime.kt$WorldTime$override fun commandResponse( sender: String, cmd: String, args: String, isPrivate: Boolean )</ID>
<ID>ReturnCount:Addons.kt$Addons$ fun exec(sender: String, login: String, cmd: String, args: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID> <ID>ReturnCount:Addons.kt$Addons$ fun exec(sender: String, login: String, cmd: String, args: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID>
<ID>ReturnCount:Addons.kt$Addons$ fun help(sender: String, topic: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID>
<ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$ @JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List&lt;Message&gt;</ID> <ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$ @JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List&lt;Message&gt;</ID>
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$ @JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List&lt;Message&gt;</ID> <ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$ @JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List&lt;Message&gt;</ID>
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID> <ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID>

View file

@ -121,6 +121,23 @@ class Addons {
return false 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. * Sort commands and modules names.
*/ */

View file

@ -34,11 +34,11 @@ package net.thauvin.erik.mobibot
import net.thauvin.erik.mobibot.PinboardUtils.addPin import net.thauvin.erik.mobibot.PinboardUtils.addPin
import net.thauvin.erik.mobibot.PinboardUtils.deletePin import net.thauvin.erik.mobibot.PinboardUtils.deletePin
import net.thauvin.erik.mobibot.PinboardUtils.updatePin 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.colorize
import net.thauvin.erik.mobibot.Utils.ensureDir import net.thauvin.erik.mobibot.Utils.ensureDir
import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.getIntProperty
import net.thauvin.erik.mobibot.Utils.helpFormat 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.isoLocalDate
import net.thauvin.erik.mobibot.Utils.today import net.thauvin.erik.mobibot.Utils.today
import net.thauvin.erik.mobibot.commands.AddLog 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. * 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. * Responds with the default, commands or modules help.
*/ */
private fun helpResponse(sender: String, topic: String, isPrivate: Boolean) { private fun helpResponse(sender: String, topic: String, isPrivate: Boolean) {
val isOp = isOp(sender) val isOp = isOp(sender)
if (topic.isBlank()) { if (topic.isBlank() || !addons.help(sender, topic.toLowerCase().trim(), isOp, isPrivate)) {
helpDefault(sender, 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)
}
} }
} }

View file

@ -74,10 +74,11 @@ abstract class AbstractModule(val bot: Mobibot) {
/** /**
* Responds with the module's help. * 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) { for (h in help) {
bot.send(sender, Utils.buildCmdSyntax(h, bot.nick, isPrivateMsgEnabled && isPrivate), isPrivate) bot.send(sender, Utils.buildCmdSyntax(h, bot.nick, isPrivateMsgEnabled && isPrivate), isPrivate)
} }
return true
} }
/** /**

View file

@ -67,12 +67,12 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
* Converts the specified currencies. * Converts the specified currencies.
*/ */
override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) {
with(bot) { bot.apply {
if (EXCHANGE_RATES.isEmpty()) { if (EXCHANGE_RATES.isEmpty()) {
try { try {
loadRates() loadRates()
} catch (e: ModuleException) { } 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) { override fun helpResponse(sender: String, isPrivate: Boolean) : Boolean {
with(bot) { bot.apply {
if (EXCHANGE_RATES.isEmpty()) { if (EXCHANGE_RATES.isEmpty()) {
try { try {
loadRates() loadRates()
@ -125,6 +125,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
sendList(sender, ArrayList(EXCHANGE_RATES.keys), 11, isPrivate, isIndent = true) sendList(sender, ArrayList(EXCHANGE_RATES.keys), 11, isPrivate, isIndent = true)
} }
} }
return true
} }
companion object { companion object {