Cleaned up usage of the Utils class.

This commit is contained in:
Erik C. Thauvin 2021-05-11 12:42:46 -07:00
parent 273ac60c7d
commit a798800198
22 changed files with 73 additions and 85 deletions

View file

@ -34,14 +34,11 @@ package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.FeedReader
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class ChannelFeed(bot: Mobibot, channel: String) : AbstractCommand(bot) {
override val name = channel
override val help = listOf(
"To list the last 5 posts from the channel's weblog feed:",
Utils.helpFormat("%c $channel")
)
override val help = listOf("To list the last 5 posts from the channel's weblog feed:", helpFormat("%c $channel"))
override val isOp = false
override val isPublic = true
override val isVisible = true

View file

@ -33,13 +33,13 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class Cycle(bot: Mobibot) : AbstractCommand(bot) {
@Suppress("MagicNumber")
private val wait = 10
override val name = "cycle"
override val help = listOf("To have the bot leave the channel and come back:", Utils.helpFormat("%c $name"))
override val help = listOf("To have the bot leave the channel and come back:", helpFormat("%c $name"))
override val isOp = true
override val isPublic = false
override val isVisible = true

View file

@ -33,14 +33,11 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class Me(bot: Mobibot) : AbstractCommand(bot) {
override val name = "me"
override val help = listOf(
"To have the bot perform an action:",
Utils.helpFormat("%c $name <action>")
)
override val help = listOf("To have the bot perform an action:", helpFormat("%c $name <action>"))
override val isOp = true
override val isPublic = false
override val isVisible = true

View file

@ -33,14 +33,11 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class Modules(bot: Mobibot) : AbstractCommand(bot) {
override val name = "modules"
override val help = listOf(
"To view a list of enabled modules:",
Utils.helpFormat("%c $name")
)
override val help = listOf("To view a list of enabled modules:", helpFormat("%c $name"))
override val isOp = true
override val isPublic = false
override val isVisible = true

View file

@ -33,13 +33,13 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class Msg(bot: Mobibot) : AbstractCommand(bot) {
override val name = "msg"
override val help = listOf(
"To have the bot send a private message to someone:",
Utils.helpFormat("%c $name <nick> <text>")
helpFormat("%c $name <nick> <text>")
)
override val isOp = true
override val isPublic = true

View file

@ -33,14 +33,11 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
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:",
Utils.helpFormat("%c $name <nick>")
)
override val help = listOf("To change the bot's nickname:", helpFormat("%c $name <nick>"))
override val isOp = true
override val isPublic = true
override val isVisible = true

View file

@ -33,14 +33,11 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class Say(bot: Mobibot) : AbstractCommand(bot) {
override val name = "say"
override val help = listOf(
"To have the bot say something on the channel:",
Utils.helpFormat("%c $name <text>")
)
override val help = listOf("To have the bot say something on the channel:", helpFormat("%c $name <text>"))
override val isOp = true
override val isPublic = false
override val isVisible = true

View file

@ -33,14 +33,11 @@
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
class Users(bot: Mobibot) : AbstractCommand(bot) {
override val name = "users"
override val help = listOf(
"To list the users present on the channel:",
Utils.helpFormat("%c $name")
)
override val help = listOf("To list the users present on the channel:", helpFormat("%c $name"))
override val isOp = false
override val isPublic = true
override val isVisible = true

View file

@ -34,7 +34,8 @@ package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntryLink
@ -43,12 +44,12 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) {
override val name = COMMAND
override val help = listOf(
"To add a comment:",
Utils.helpFormat("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1",
helpFormat("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${bold(Constants.LINK_CMD)}1.1",
"To edit a comment, use its label: ",
Utils.helpFormat("${Constants.LINK_CMD}1.1:This is an edited comment"),
helpFormat("${Constants.LINK_CMD}1.1:This is an edited comment"),
"To delete a comment, use its label and a minus sign: ",
Utils.helpFormat("${Constants.LINK_CMD}1.1:-")
helpFormat("${Constants.LINK_CMD}1.1:-")
)
override val isOp = false
override val isPublic = true
@ -99,7 +100,7 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) {
bot.send(sender, "To change a comment's author:", isPrivate)
bot.send(
sender,
Utils.helpFormat("${Constants.LINK_CMD}1.1:?<nick>"),
helpFormat("${Constants.LINK_CMD}1.1:?<nick>"),
isPrivate
)
}

View file

@ -34,7 +34,8 @@ package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries
import net.thauvin.erik.mobibot.entries.EntriesUtils
@ -44,15 +45,15 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
override val name = "posting"
override val help = listOf(
"Post a URL, by saying it on a line on its own:",
Utils.helpFormat("<url> [<title>] ${Tags.COMMAND}: <+tag> [...]]"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1",
helpFormat("<url> [<title>] ${Tags.COMMAND}: <+tag> [...]]"),
"I will reply with a label, for example: ${bold(Constants.LINK_CMD)}1",
"To add a title, use its label and a pipe:",
Utils.helpFormat("${Constants.LINK_CMD}1:|This is the title"),
helpFormat("${Constants.LINK_CMD}1:|This is the title"),
"To add a comment:",
Utils.helpFormat("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1",
helpFormat("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${bold(Constants.LINK_CMD)}1.1",
"To edit a comment, see: ",
Utils.helpFormat("%c ${Constants.HELP_CMD} ${Comment.COMMAND}")
helpFormat("%c ${Constants.HELP_CMD} ${Comment.COMMAND}")
)
override val isOp = false
override val isPublic = true

View file

@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntryLink
@ -43,7 +43,7 @@ class Tags(bot: Mobibot) : AbstractCommand(bot) {
override val name = COMMAND
override val help = listOf(
"To categorize or tag a URL, use its label and a T:",
Utils.helpFormat("${Constants.LINK_CMD}1T:<+tag|-tag> [...]")
helpFormat("${Constants.LINK_CMD}1T:<+tag|-tag> [...]")
)
override val isOp = false
override val isPublic = true

View file

@ -33,7 +33,8 @@
package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries
import net.thauvin.erik.mobibot.entries.EntriesUtils
@ -45,7 +46,7 @@ class View(bot: Mobibot) : AbstractCommand(bot) {
override val name = VIEW_CMD
override val help = listOf(
"To list or search the current URL posts:",
Utils.helpFormat("%c $name [<start>] [<query>]")
helpFormat("%c $name [<start>] [<query>]")
)
override val isOp = false
override val isPublic = true
@ -109,7 +110,7 @@ class View(bot: Mobibot) : AbstractCommand(bot) {
i++
if (sent == maxEntries && i < max) {
bot.send(
sender, "To view more, try: " + Utils.bold("${bot.nick}: $name ${i + 1} $lcArgs"), false
sender, "To view more, try: " + bold("${bot.nick}: $name ${i + 1} $lcArgs"), false
)
}
}

View file

@ -32,7 +32,7 @@
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax
/**
* The `Module` abstract class.
@ -76,7 +76,7 @@ abstract class AbstractModule(val bot: Mobibot) {
*/
open fun helpResponse(sender: String, isPrivate: Boolean): Boolean {
for (h in help) {
bot.send(sender, Utils.buildCmdSyntax(h, bot.nick, isPrivateMsgEnabled && isPrivate), isPrivate)
bot.send(sender, buildCmdSyntax(h, bot.nick, isPrivateMsgEnabled && isPrivate), isPrivate)
}
return true
}

View file

@ -34,7 +34,8 @@ package net.thauvin.erik.mobibot.modules
import net.objecthunter.exp4j.ExpressionBuilder
import net.objecthunter.exp4j.tokenizer.UnknownFunctionOrVariableException
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.helpFormat
import java.text.DecimalFormat
/**
@ -74,13 +75,13 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
fun calculate(query: String): String {
val decimalFormat = DecimalFormat("#.##")
val calc = ExpressionBuilder(query).build()
return query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
return query.replace(" ", "") + " = " + bold(decimalFormat.format(calc.evaluate()))
}
}
init {
commands.add(CALC_CMD)
help.add("To solve a mathematical calculation:")
help.add(Utils.helpFormat("%c $CALC_CMD <calculation>"))
help.add(helpFormat("%c $CALC_CMD <calculation>"))
}
}

View file

@ -35,7 +35,7 @@ import net.thauvin.erik.crypto.CryptoException
import net.thauvin.erik.crypto.CryptoPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.msg.PublicMessage
/**
@ -83,10 +83,10 @@ class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(CRYPTO_CMD)
help.add("To retrieve a cryptocurrency's market price:")
help.add(Utils.helpFormat("%c $CRYPTO_CMD <symbol> [<currency>]"))
help.add(helpFormat("%c $CRYPTO_CMD <symbol> [<currency>]"))
help.add("For example:")
help.add(Utils.helpFormat("%c $CRYPTO_CMD BTC"))
help.add(Utils.helpFormat("%c $CRYPTO_CMD ETH EUR"))
help.add(Utils.helpFormat("%c $CRYPTO_CMD ETH2 GPB"))
help.add(helpFormat("%c $CRYPTO_CMD BTC"))
help.add(helpFormat("%c $CRYPTO_CMD ETH EUR"))
help.add(helpFormat("%c $CRYPTO_CMD ETH2 GPB"))
}
}

View file

@ -32,7 +32,8 @@
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.helpFormat
import kotlin.random.Random
/**
@ -52,13 +53,13 @@ class Dice(bot: Mobibot) : AbstractModule(bot) {
with(bot) {
send(
channel,
"$sender rolled two dice: ${Utils.bold(playerRoll.first)} and ${Utils.bold(playerRoll.second)}"
+ " for a total of ${Utils.bold(playerTotal)}",
"$sender rolled two dice: ${bold(playerRoll.first)} and ${bold(playerRoll.second)}"
+ " for a total of ${bold(playerTotal)}",
isPrivate
)
action(
"rolled two dice: ${Utils.bold(roll.first)} and ${Utils.bold(roll.second)}" +
" for a total of ${Utils.bold(total)}"
"rolled two dice: ${bold(roll.first)} and ${bold(roll.second)}" +
" for a total of ${bold(total)}"
)
when (winLoseOrTie(total, playerTotal)) {
Result.WIN -> action("wins.")
@ -99,6 +100,6 @@ class Dice(bot: Mobibot) : AbstractModule(bot) {
init {
commands.add(DICE_CMD)
help.add("To roll the dice:")
help.add(Utils.helpFormat("%c $DICE_CMD"))
help.add(helpFormat("%c $DICE_CMD"))
}
}

View file

@ -33,7 +33,7 @@ package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
import org.apache.commons.net.whois.WhoisClient
import java.io.IOException
import java.net.InetAddress
@ -164,6 +164,6 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
init {
commands.add(LOOKUP_CMD)
help.add("To perform a DNS lookup query:")
help.add(Utils.helpFormat("%c $LOOKUP_CMD <ip address or hostname>"))
help.add(helpFormat("%c $LOOKUP_CMD <ip address or hostname>"))
}
}

View file

@ -31,7 +31,7 @@
*/
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.obfuscate
import org.apache.commons.lang3.StringUtils
/**
@ -68,7 +68,7 @@ class ModuleException : Exception {
* Return the sanitized message (e.g. remove API keys, etc.)
*/
fun getSanitizedMessage(vararg sanitize: String): String {
val obfuscate = sanitize.map { Utils.obfuscate(it) }.toTypedArray()
val obfuscate = sanitize.map { obfuscate(it) }.toTypedArray()
return when {
cause != null -> {
cause.javaClass.name + ": " + StringUtils.replaceEach(cause.message, sanitize, obfuscate)

View file

@ -32,7 +32,7 @@
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
import kotlin.random.Random
/**
@ -85,6 +85,6 @@ class Ping(bot: Mobibot) : AbstractModule(bot) {
init {
commands.add(PING_CMD)
help.add("To ping the bot:")
help.add(Utils.helpFormat("%c $PING_CMD"))
help.add(helpFormat("%c $PING_CMD"))
}
}

View file

@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.TwitterTimer
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.commands.links.LinksMgr
import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.msg.Message
@ -235,7 +235,7 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(TWITTER_CMD)
help.add("To post to Twitter:")
help.add(Utils.helpFormat("%c $TWITTER_CMD <message>"))
help.add(helpFormat("%c $TWITTER_CMD <message>"))
properties[AUTOPOST_PROP] = "false"
initProperties(CONSUMER_KEY_PROP, CONSUMER_SECRET_PROP, HANDLE_PROP, TOKEN_PROP, TOKEN_SECRET_PROP)
}

View file

@ -32,7 +32,8 @@
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.msg.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage
@ -76,16 +77,16 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
val tz = COUNTRIES_MAP[(query.substring(query.indexOf(' ') + 1).trim()).uppercase()]
val response: String = if (tz != null) {
if (BEATS_KEYWORD == tz) {
"The current Internet Time is: " + Utils.bold(internetTime() + ' ' + BEATS_KEYWORD)
"The current Internet Time is: " + bold(internetTime() + ' ' + BEATS_KEYWORD)
} else {
(ZonedDateTime.now()
.withZoneSameInstant(ZoneId.of(tz))
.format(
DateTimeFormatter.ofPattern(
"'The time is ${Utils.bold("'HH:mm'")} on ${Utils.bold("'EEEE, d MMMM yyyy'")} in '"
"'The time is ${bold("'HH:mm'")} on ${bold("'EEEE, d MMMM yyyy'")} in '"
)
)
+ Utils.bold(tz.substring(tz.indexOf('/') + 1).replace('_', ' '))
+ bold(tz.substring(tz.indexOf('/') + 1).replace('_', ' '))
)
}
} else {
@ -214,9 +215,9 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
init {
help.add("To display a country's current date/time:")
help.add(Utils.helpFormat("%c $TIME_CMD") + " [<country code>]")
help.add(helpFormat("%c $TIME_CMD") + " [<country code>]")
help.add("For a listing of the supported countries:")
help.add(Utils.helpFormat("%c $TIME_CMD"))
help.add(helpFormat("%c $TIME_CMD"))
commands.add(TIME_CMD)
}
}

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle
#Tue May 11 01:30:42 PDT 2021
version.buildmeta=726
#Tue May 11 12:39:39 PDT 2021
version.buildmeta=727
version.major=0
version.minor=8
version.patch=0
version.prerelease=beta
version.project=mobibot
version.semver=0.8.0-beta+726
version.semver=0.8.0-beta+727