diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt index d8ff578..cdc7a8f 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt @@ -35,6 +35,8 @@ package net.thauvin.erik.mobibot import net.thauvin.erik.mobibot.Utils.appendIfMissing import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.getIntProperty +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.sendList @@ -134,13 +136,9 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro event.sendMessage("Type a URL on $channel to post it.") event.sendMessage("For more information on a specific command, type:") event.sendMessage( - Utils.helpFormat( - Utils.buildCmdSyntax( - "%c ${Constants.HELP_CMD} ", - event.bot().nick, - event is PrivateMessageEvent - ) - ), + helpFormat( + helpCmdSyntax("%c ${Constants.HELP_CMD} ", event.bot().nick, event is PrivateMessageEvent) + ) ) event.sendMessage("The commands are:") event.sendList(addons.names.commands, 8, isBold = true, isIndent = true) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index 20286c1..6becd6e 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -161,6 +161,16 @@ object Utils { @JvmStatic fun String?.green(): String = colorize(Colors.DARK_GREEN) + /** + * Build a help command by replacing `%c` with the bot's pub/priv command, and `%n` with the bot's + * nick. + */ + @JvmStatic + fun helpCmdSyntax(text: String, botNick: String, isPrivate: Boolean): String { + val replace = arrayOf(if (isPrivate) "/msg $botNick" else "$botNick:", botNick) + return text.replaceEach(searchFlags, replace) + } + /** * Returns a formatted help string. */ diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/AbstractCommand.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/AbstractCommand.kt index 674fe0f..f773cdd 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/AbstractCommand.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/AbstractCommand.kt @@ -33,12 +33,11 @@ package net.thauvin.erik.mobibot.commands import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.sendMessage import org.pircbotx.hooks.events.PrivateMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent -import java.util.concurrent.ConcurrentHashMap abstract class AbstractCommand { abstract val name: String @@ -47,16 +46,14 @@ abstract class AbstractCommand { abstract val isPublic: Boolean abstract val isVisible: Boolean - val properties: MutableMap = ConcurrentHashMap() + val properties: MutableMap = mutableMapOf() abstract fun commandResponse(channel: String, args: String, event: GenericMessageEvent) open fun helpResponse(channel: String, topic: String, event: GenericMessageEvent): Boolean { if (!isOpOnly || isOpOnly == isChannelOp(channel, event)) { for (h in help) { - event.sendMessage( - buildCmdSyntax(h, event.bot().nick, event is PrivateMessageEvent || !isPublic), - ) + event.sendMessage(helpCmdSyntax(h, event.bot().nick, event is PrivateMessageEvent || !isPublic)) } return true } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt index afd9bf4..df56e34 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt @@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.commands import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.sendList @@ -90,7 +90,7 @@ class Ignore : AbstractCommand() { override fun helpResponse(channel: String, topic: String, event: GenericMessageEvent): Boolean { return if (isChannelOp(channel, event)) { for (h in helpOp) { - event.sendMessage(buildCmdSyntax(h, event.bot().nick, true)) + event.sendMessage(helpCmdSyntax(h, event.bot().nick, true)) } true } else { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt index f853cae..dd22c0e 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt @@ -33,7 +33,7 @@ package net.thauvin.erik.mobibot.commands.links import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.sendMessage @@ -109,11 +109,7 @@ class View : AbstractCommand() { event.sendMessage("To view more, try: ") event.sendMessage( helpFormat( - buildCmdSyntax( - "%c $name ${index + 1} $query", - event.bot().nick, - event is PrivateMessageEvent - ) + helpCmdSyntax("%c $name ${index + 1} $query", event.bot().nick, event is PrivateMessageEvent) ) ) } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt index 1100c99..a581cd5 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt @@ -33,7 +33,7 @@ package net.thauvin.erik.mobibot.commands.tell import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.plural @@ -256,11 +256,7 @@ class Tell(private val serialObject: String) : AbstractCommand() { event.sendMessage("To delete one or all delivered messages:") event.sendMessage( helpFormat( - buildCmdSyntax( - "%c $name $TELL_DEL_KEYWORD ", - event.bot().nick, - true - ) + helpCmdSyntax("%c $name $TELL_DEL_KEYWORD ", event.bot().nick, true) ) ) event.sendMessage(help.last()) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/AbstractModule.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/AbstractModule.kt index 25e7c9d..83eafcf 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/AbstractModule.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/AbstractModule.kt @@ -32,7 +32,7 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.sendMessage import org.pircbotx.hooks.events.PrivateMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent @@ -79,7 +79,7 @@ abstract class AbstractModule { */ open fun helpResponse(event: GenericMessageEvent): Boolean { for (h in help) { - event.sendMessage(buildCmdSyntax(h, event.bot().nick, isPrivateMsgEnabled && event is PrivateMessageEvent)) + event.sendMessage(helpCmdSyntax(h, event.bot().nick, isPrivateMsgEnabled && event is PrivateMessageEvent)) } return true } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index f6de896..e597df7 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -33,7 +33,7 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendMessage @@ -111,11 +111,11 @@ class CurrencyConverter : ThreadedModule() { } else { val nick = event.bot().nick event.sendMessage("To convert from one currency to another:") - event.sendMessage(helpFormat(buildCmdSyntax("%c $CURRENCY_CMD 100 USD to EUR", nick, isPrivateMsgEnabled))) + event.sendMessage(helpFormat(helpCmdSyntax("%c $CURRENCY_CMD 100 USD to EUR", nick, isPrivateMsgEnabled))) event.sendMessage("For a listing of current reference rates:") event.sendMessage( helpFormat( - buildCmdSyntax("%c $CURRENCY_CMD $CURRENCY_RATES_KEYWORD", nick, isPrivateMsgEnabled) + helpCmdSyntax("%c $CURRENCY_CMD $CURRENCY_RATES_KEYWORD", nick, isPrivateMsgEnabled) ) ) event.sendMessage("The supported currencies are: ") @@ -161,13 +161,14 @@ class CurrencyConverter : ThreadedModule() { } else { val to = cmds[1].uppercase() val from = cmds[3].uppercase() - if (EXCHANGE_RATES.containsKey(to) && EXCHANGE_RATES.containsKey(from)) { + val toRate = EXCHANGE_RATES[to] + val fromRate = EXCHANGE_RATES[from] + if (!toRate.isNullOrBlank() && !fromRate.isNullOrBlank()) { try { val amt = cmds[0].replace(",", "").toDouble() - val doubleFrom = EXCHANGE_RATES[to]!!.toDouble() - val doubleTo = EXCHANGE_RATES[from]!!.toDouble() PublicMessage( - amt.formatCurrency(to) + " = " + (amt * doubleTo / doubleFrom).formatCurrency(from) + amt.formatCurrency(to) + " = " + + (amt * toRate.toDouble() / fromRate.toDouble()).formatCurrency(from) ) } catch (e: NumberFormatException) { ErrorMessage("Let's try with some real numbers next time, okay?") diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt index 29ae070..2cd92ba 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt @@ -37,7 +37,6 @@ import assertk.assertions.isEqualTo import assertk.assertions.length import net.thauvin.erik.mobibot.Utils.appendIfMissing import net.thauvin.erik.mobibot.Utils.bold -import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.capitalise import net.thauvin.erik.mobibot.Utils.capitalizeWords import net.thauvin.erik.mobibot.Utils.colorize @@ -45,6 +44,7 @@ import net.thauvin.erik.mobibot.Utils.cyan import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.green +import net.thauvin.erik.mobibot.Utils.helpCmdSyntax import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.obfuscate @@ -58,8 +58,6 @@ import net.thauvin.erik.mobibot.Utils.toIsoLocalDate import net.thauvin.erik.mobibot.Utils.toUtcDateTime import net.thauvin.erik.mobibot.Utils.today import net.thauvin.erik.mobibot.Utils.unescapeXml -import net.thauvin.erik.mobibot.Utils.uptime -import net.thauvin.erik.mobibot.Utils.urlReader import net.thauvin.erik.mobibot.msg.Message.Companion.DEFAULT_COLOR import org.pircbotx.Colors import org.testng.annotations.BeforeClass @@ -105,15 +103,6 @@ class UtilsTest { assertThat("test".bold(), "test.bold()").isEqualTo(Colors.BOLD + "test" + Colors.BOLD) } - @Test - fun testBuildCmdSyntax() { - val bot = "mobibot" - assertThat(buildCmdSyntax("%c $test %n $test", bot, false), "public") - .isEqualTo("$bot: $test $bot $test") - assertThat(buildCmdSyntax("%c %n $test %c $test %n", bot, true), "public") - .isEqualTo("/msg $bot $bot $test /msg $bot $test $bot") - } - @Test fun testCapitalise() { @@ -172,6 +161,15 @@ class UtilsTest { assertThat(ascii.green()).isEqualTo(Colors.DARK_GREEN + ascii + Colors.NORMAL) } + @Test + fun testHelpCmdSyntax() { + val bot = "mobibot" + assertThat(helpCmdSyntax("%c $test %n $test", bot, false), "public") + .isEqualTo("$bot: $test $bot $test") + assertThat(helpCmdSyntax("%c %n $test %c $test %n", bot, true), "public") + .isEqualTo("/msg $bot $bot $test /msg $bot $test $bot") + } + @Test fun testHelpFormat() { assertThat(helpFormat(test, isBold = true, isIndent = false), "bold") @@ -260,18 +258,6 @@ class UtilsTest { ) } - @Test - fun testUptime() { - assertThat(uptime(547800300076L), "full") - .isEqualTo("17 years 2 months 2 weeks 1 day 6 hours 45 minutes") - assertThat(uptime(2700000L), "minutes").isEqualTo("45 minutes") - assertThat(uptime(24300000L), "hours minutes").isEqualTo("6 hours 45 minutes") - assertThat(uptime(110700000L), "days hours minutes").isEqualTo("1 day 6 hours 45 minutes") - assertThat(uptime(1320300000L), "weeks days hours minutes") - .isEqualTo("2 weeks 1 day 6 hours 45 minutes") - assertThat(uptime(0L), "0 minutes").isEqualTo("0 minute") - } - @Test @Throws(IOException::class) fun testUrlReader() {