From 4abdb268ef23de445ece6096699e42d1872fdb33 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 14 Feb 2022 22:38:51 -0800 Subject: [PATCH] Code cleanup. --- src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt | 15 ++------------- .../net/thauvin/erik/mobibot/commands/Die.kt | 3 --- .../thauvin/erik/mobibot/commands/links/Tags.kt | 2 +- .../net/thauvin/erik/mobibot/modules/Weather2.kt | 10 ++++++---- .../thauvin/erik/mobibot/ExceptionSanitizer.kt | 4 ++-- 5 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index 6becd6e..b218892 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -50,8 +50,6 @@ import java.time.format.DateTimeFormatter import java.util.Date import java.util.Properties import java.util.stream.Collectors -import kotlin.time.DurationUnit -import kotlin.time.toDuration /** * Miscellaneous utilities. @@ -97,16 +95,6 @@ object Utils { return getBot() as PircBotX } - /** - * Build a help command by replacing `%c` with the bot's pub/priv command, and `%n` with the bot's - * nick. - */ - @JvmStatic - fun buildCmdSyntax(text: String, botNick: String, isPrivate: Boolean): String { - val replace = arrayOf(if (isPrivate) "/msg $botNick" else "$botNick:", botNick) - return text.replaceEach(searchFlags, replace) - } - /** * Capitalize a string. */ @@ -116,9 +104,9 @@ object Utils { /** * Capitalize words */ + @JvmStatic fun String.capitalizeWords(): String = split(" ").joinToString(" ") { it.capitalise() } - /** * Colorize a string. */ @@ -254,6 +242,7 @@ object Utils { * Send a formatted commands/modules, etc. list. */ @JvmStatic + @JvmOverloads fun GenericMessageEvent.sendList( list: List, maxPerLine: Int, diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Die.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Die.kt index 26f05c3..9ccbcf3 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Die.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Die.kt @@ -54,9 +54,6 @@ class Die : AbstractCommand() { } companion object { - /** - * Max days property. - */ const val DIE_PROP = "die" } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Tags.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Tags.kt index 7c0d904..842866e 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Tags.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Tags.kt @@ -83,6 +83,6 @@ class Tags : AbstractCommand() { } override fun matches(message: String): Boolean { - return message.matches("^${Constants.LINK_CMD}[0-9]+${Constants.TAG_CMD}:.*".toRegex()) + return message.matches("^${Constants.LINK_CMD}\\d+${Constants.TAG_CMD}:.*".toRegex()) } } 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 403dc79..8dcdc15 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -96,8 +96,8 @@ class Weather2 : ThreadedModule() { /** * Converts and rounds temperature from °F to °C. */ - fun ftoC(d: Double?): Pair { - val c = (d!! - 32) * 5 / 9 + fun ftoC(d: Double): Pair { + val c = (d - 32) * 5 / 9 return d.roundToInt() to c.roundToInt() } @@ -154,8 +154,10 @@ class Weather2 : ThreadedModule() { cwd.mainData?.let { with(it) { if (hasTemp()) { - val t = ftoC(temp) - messages.add(PublicMessage("Temperature: ${t.first}°F, ${t.second}°C")) + temp?.let { t -> + val (f, c) = ftoC(t) + messages.add(PublicMessage("Temperature: ${f}°F, ${c}°C")) + } } if (hasHumidity()) { humidity?.let { h -> diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/ExceptionSanitizer.kt b/src/test/kotlin/net/thauvin/erik/mobibot/ExceptionSanitizer.kt index ba486f0..b1c6dba 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/ExceptionSanitizer.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/ExceptionSanitizer.kt @@ -48,11 +48,11 @@ object ExceptionSanitizer { if (!cause?.message.isNullOrBlank()) { return ModuleException( debugMessage, - cause!!.javaClass.name + ": " + cause!!.message!!.replaceEach(search, obfuscate), + cause?.javaClass?.name + ": " + cause?.message?.replaceEach(search, obfuscate), this ) } else if (!message.isNullOrBlank()) { - return ModuleException(debugMessage, message!!.replaceEach(search, obfuscate), this) + return ModuleException(debugMessage, message?.replaceEach(search, obfuscate), this) } } }