From af9546e3d1abd3a461b7faf99fe631ac3f2a5a3d Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 30 Jul 2021 01:26:12 -0700 Subject: [PATCH] Cleanup. --- .../thauvin/erik/mobibot/commands/Ignore.kt | 7 +-- .../net/thauvin/erik/mobibot/commands/Nick.kt | 2 +- .../thauvin/erik/mobibot/commands/Users.kt | 18 +++---- .../erik/mobibot/entries/EntriesMgr.kt | 4 +- .../erik/mobibot/modules/CryptoPrices.kt | 14 +++--- .../net/thauvin/erik/mobibot/modules/Dice.kt | 2 +- .../net/thauvin/erik/mobibot/modules/Joke.kt | 12 +++-- .../erik/mobibot/modules/StockQuote.kt | 47 ++++++++++++++----- .../thauvin/erik/mobibot/modules/Weather2.kt | 36 +++++++------- .../thauvin/erik/mobibot/modules/WorldTime.kt | 10 ++-- 10 files changed, 94 insertions(+), 58 deletions(-) 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 ce4fd32..69e9688 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt @@ -54,11 +54,8 @@ class Ignore(bot: Mobibot) : AbstractCommand(bot) { "To toggle your ignore status:", helpFormat("%c $name $me") ) - private val helpOp = listOf( - "To ignore a link posted to the channel:", - helpFormat("https://www.foo.bar " + bold("%n"), false), - "To add/remove nicks from the ignored list:", - helpFormat("%c $name [ ...]") + private val helpOp = help.plus( + arrayOf("To add/remove nicks from the ignored list:", helpFormat("%c $name [ ...]")) ) override val isOp = false diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Nick.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Nick.kt index b4495b3..2f956e2 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Nick.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Nick.kt @@ -37,7 +37,7 @@ import net.thauvin.erik.mobibot.Utils.helpFormat class Nick(bot: Mobibot) : AbstractCommand(bot) { override val name = "nick" - override val help = listOf("To change the bot's nickname:", helpFormat("%c $name ")) + override val help = listOf("To change the bot's nickname:", helpFormat("%c $name ")) override val isOp = true override val isPublic = true override val isVisible = true diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt index 4db1781..de83bdf 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt @@ -51,15 +51,17 @@ class Users(bot: Mobibot) : AbstractCommand(bot) { isPrivate: Boolean ) { val nicks = mutableListOf() - bot.getUsers(bot.channel).forEach { user -> - if (bot.isOp(user.nick)) { - nicks.add("@${user.nick}") - } else { - nicks.add(user.nick) + with(bot) { + getUsers(channel).forEach { user -> + if (isOp(user.nick)) { + nicks.add("@${user.nick}") + } else { + nicks.add(user.nick) + } } - } - @Suppress("MagicNumber") - bot.sendList(sender, nicks.sorted(), 8, isPrivate, isIndent = true) + @Suppress("MagicNumber") + sendList(sender, nicks.sorted(), 8, isPrivate, isIndent = true) + } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesMgr.kt b/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesMgr.kt index 6f9ffff..6fe809b 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesMgr.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesMgr.kt @@ -114,7 +114,9 @@ object EntriesMgr { output.output(rss, fw) } } else { - bot.logger.warn("Unable to generate the backlogs feed. No property configured.") + if (bot.logger.isErrorEnabled) { + bot.logger.warn("Unable to generate the backlogs feed. No property configured.") + } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt index c4b2fcd..db28550 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt @@ -87,11 +87,13 @@ class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) { init { commands.add(CRYPTO_CMD) - help.add("To retrieve a cryptocurrency's market price:") - help.add(helpFormat("%c $CRYPTO_CMD []")) - help.add("For example:") - help.add(helpFormat("%c $CRYPTO_CMD BTC")) - help.add(helpFormat("%c $CRYPTO_CMD ETH EUR")) - help.add(helpFormat("%c $CRYPTO_CMD ETH2 GPB")) + with(help) { + add("To retrieve a cryptocurrency's market price:") + add(helpFormat("%c $CRYPTO_CMD []")) + add("For example:") + add(helpFormat("%c $CRYPTO_CMD BTC")) + add(helpFormat("%c $CRYPTO_CMD ETH EUR")) + add(helpFormat("%c $CRYPTO_CMD ETH2 GPB")) + } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt index 46f788c..ec3a0e6 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt @@ -75,7 +75,7 @@ class Dice(bot: Mobibot) : AbstractModule(bot) { private fun roll(): Pair { @Suppress("MagicNumber") - return Pair(Random.nextInt(1, 7), Random.nextInt(1, 7)) + return Random.nextInt(1, 7) to Random.nextInt(1, 7) } companion object { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt index f2624ec..40b6fc0 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt @@ -59,11 +59,13 @@ class Joke(bot: Mobibot) : ThreadedModule(bot) { * Returns a random joke from [The Internet Chuck Norris Database](http://www.icndb.com/). */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { - try { - bot.send(cyan(randomJoke().msg)) - } catch (e: ModuleException) { - if (bot.logger.isWarnEnabled) bot.logger.warn(e.debugMessage, e) - bot.send(sender, e.message, isPrivate) + with(bot) { + try { + send(cyan(randomJoke().msg)) + } catch (e: ModuleException) { + if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) + send(sender, e.message, isPrivate) + } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index 1459914..e00838d 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -51,7 +51,7 @@ import java.net.URL */ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { /** - * Returns the specified stock quote from Alpha Avantage. + * Returns the specified stock quote from Alpha Vantage. */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { with(bot) { @@ -163,27 +163,52 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { add(ErrorMessage(INVALID_SYMBOL)) return messages } + add( PublicMessage( "Symbol: " + unescapeXml(quote.getString("01. symbol")) + " [" + unescapeXml(symbolInfo.getString("2. name")) + ']' ) ) - add(PublicMessage(" Price: " + unescapeXml(quote.getString("05. price")))) - add(PublicMessage(" Previous: " + unescapeXml(quote.getString("08. previous close")))) - add(NoticeMessage(" Open: " + unescapeXml(quote.getString("02. open")))) - add(NoticeMessage(" High: " + unescapeXml(quote.getString("03. high")))) - add(NoticeMessage(" Low: " + unescapeXml(quote.getString("04. low")))) - add(NoticeMessage(" Volume: " + unescapeXml(quote.getString("06. volume")))) + + @Suppress("MagicNumber") + val pad = 10 + add( - NoticeMessage( - " Latest: " + unescapeXml(quote.getString("07. latest trading day")) + PublicMessage( + "Price:".padEnd(pad).prependIndent() + + unescapeXml(quote.getString("05. price")) ) ) + add( + PublicMessage( + "Previous:".padEnd(pad).prependIndent() + + unescapeXml(quote.getString("08. previous close")) + ) + ) + + val data = arrayOf( + "Open" to "02. open", + "High" to "03. high", + "Low" to "04. low", + "Volume" to "06. volume", + "Latest" to "07. latest trading day" + ) + + data.forEach { + add( + NoticeMessage( + "${it.first}:".padEnd(pad).prependIndent(), + unescapeXml(quote.getString(it.second)) + ) + ) + } + add( NoticeMessage( - " Change: " + unescapeXml(quote.getString("09. change")) + " [" - + unescapeXml(quote.getString("10. change percent")) + ']' + "Change:".padEnd(pad).prependIndent() + + unescapeXml(quote.getString("09. change")) + + " [" + unescapeXml(quote.getString("10. change percent")) + ']' ) ) } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt index b1f7d3e..9ae9e50 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -56,18 +56,20 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { if (args.isNotBlank()) { - try { - val messages = getWeather(args, properties[OWM_API_KEY_PROP]) - if (messages[0].isError) { - helpResponse(sender, isPrivate) - } else { - for (msg in messages) { - bot.send(sender, msg) + with(bot) { + try { + val messages = getWeather(args, properties[OWM_API_KEY_PROP]) + if (messages[0].isError) { + helpResponse(sender, isPrivate) + } else { + for (msg in messages) { + send(sender, msg) + } } + } catch (e: ModuleException) { + if (logger.isDebugEnabled) logger.debug(e.debugMessage, e) + send(e.message) } - } catch (e: ModuleException) { - if (bot.logger.isDebugEnabled) bot.logger.debug(e.debugMessage, e) - bot.send(e.message) } } else { helpResponse(sender, isPrivate) @@ -174,7 +176,7 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { } } } catch (e: APIException) { - throw ModuleException("getWeather($query)", "Unable to perform weather lookup.", e) + throw ModuleException("getWeather($query)", "A weather API error has occurred.", e) } catch (e: NullPointerException) { throw ModuleException("getWeather($query)", "Unable to perform weather lookup.", e) } @@ -195,11 +197,13 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { init { commands.add(WEATHER_CMD) - help.add("To display weather information:") - help.add(helpFormat("%c $WEATHER_CMD [, ]")) - help.add("For example:") - help.add(helpFormat("%c $WEATHER_CMD paris, fr")) - help.add("The default ISO 3166 country code is ${bold("US")}. Zip codes supported in most countries.") + with(help) { + add("To display weather information:") + add(helpFormat("%c $WEATHER_CMD [, ]")) + add("For example:") + add(helpFormat("%c $WEATHER_CMD paris, fr")) + add("The default ISO 3166 country code is ${bold("US")}. Zip codes supported in most countries.") + } initProperties(OWM_API_KEY_PROP) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt index 0837590..a9be03d 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt @@ -211,10 +211,12 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) { override val isPrivateMsgEnabled = true init { - help.add("To display a country's current date/time:") - help.add(helpFormat("%c $TIME_CMD []")) - help.add("For a listing of the supported countries:") - help.add(helpFormat("%c $TIME_CMD")) + with(help) { + add("To display a country's current date/time:") + add(helpFormat("%c $TIME_CMD []")) + add("For a listing of the supported countries:") + add(helpFormat("%c $TIME_CMD")) + } commands.add(TIME_CMD) } }