Changed buildCmdSyntax to helpCmdSyntax

This commit is contained in:
Erik C. Thauvin 2022-02-14 22:36:52 -08:00
parent 0dd02d7039
commit ea40d71f14
9 changed files with 44 additions and 60 deletions

View file

@ -35,6 +35,8 @@ package net.thauvin.erik.mobibot
import net.thauvin.erik.mobibot.Utils.appendIfMissing import net.thauvin.erik.mobibot.Utils.appendIfMissing
import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.getIntProperty 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.isChannelOp
import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.sendList 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("Type a URL on $channel to post it.")
event.sendMessage("For more information on a specific command, type:") event.sendMessage("For more information on a specific command, type:")
event.sendMessage( event.sendMessage(
Utils.helpFormat( helpFormat(
Utils.buildCmdSyntax( helpCmdSyntax("%c ${Constants.HELP_CMD} <command>", event.bot().nick, event is PrivateMessageEvent)
"%c ${Constants.HELP_CMD} <command>", )
event.bot().nick,
event is PrivateMessageEvent
)
),
) )
event.sendMessage("The commands are:") event.sendMessage("The commands are:")
event.sendList(addons.names.commands, 8, isBold = true, isIndent = true) event.sendList(addons.names.commands, 8, isBold = true, isIndent = true)

View file

@ -161,6 +161,16 @@ object Utils {
@JvmStatic @JvmStatic
fun String?.green(): String = colorize(Colors.DARK_GREEN) 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. * Returns a formatted help string.
*/ */

View file

@ -33,12 +33,11 @@
package net.thauvin.erik.mobibot.commands package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Utils.bot 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.isChannelOp
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import org.pircbotx.hooks.events.PrivateMessageEvent import org.pircbotx.hooks.events.PrivateMessageEvent
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
import java.util.concurrent.ConcurrentHashMap
abstract class AbstractCommand { abstract class AbstractCommand {
abstract val name: String abstract val name: String
@ -47,16 +46,14 @@ abstract class AbstractCommand {
abstract val isPublic: Boolean abstract val isPublic: Boolean
abstract val isVisible: Boolean abstract val isVisible: Boolean
val properties: MutableMap<String, String> = ConcurrentHashMap() val properties: MutableMap<String, String> = mutableMapOf()
abstract fun commandResponse(channel: String, args: String, event: GenericMessageEvent) abstract fun commandResponse(channel: String, args: String, event: GenericMessageEvent)
open fun helpResponse(channel: String, topic: String, event: GenericMessageEvent): Boolean { open fun helpResponse(channel: String, topic: String, event: GenericMessageEvent): Boolean {
if (!isOpOnly || isOpOnly == isChannelOp(channel, event)) { if (!isOpOnly || isOpOnly == isChannelOp(channel, event)) {
for (h in help) { for (h in help) {
event.sendMessage( event.sendMessage(helpCmdSyntax(h, event.bot().nick, event is PrivateMessageEvent || !isPublic))
buildCmdSyntax(h, event.bot().nick, event is PrivateMessageEvent || !isPublic),
)
} }
return true return true
} }

View file

@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.bot 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.helpFormat
import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.isChannelOp
import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendList
@ -90,7 +90,7 @@ class Ignore : AbstractCommand() {
override fun helpResponse(channel: String, topic: String, event: GenericMessageEvent): Boolean { override fun helpResponse(channel: String, topic: String, event: GenericMessageEvent): Boolean {
return if (isChannelOp(channel, event)) { return if (isChannelOp(channel, event)) {
for (h in helpOp) { for (h in helpOp) {
event.sendMessage(buildCmdSyntax(h, event.bot().nick, true)) event.sendMessage(helpCmdSyntax(h, event.bot().nick, true))
} }
true true
} else { } else {

View file

@ -33,7 +33,7 @@
package net.thauvin.erik.mobibot.commands.links package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Utils.bot 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.helpFormat
import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
@ -109,11 +109,7 @@ class View : AbstractCommand() {
event.sendMessage("To view more, try: ") event.sendMessage("To view more, try: ")
event.sendMessage( event.sendMessage(
helpFormat( helpFormat(
buildCmdSyntax( helpCmdSyntax("%c $name ${index + 1} $query", event.bot().nick, event is PrivateMessageEvent)
"%c $name ${index + 1} $query",
event.bot().nick,
event is PrivateMessageEvent
)
) )
) )
} }

View file

@ -33,7 +33,7 @@ package net.thauvin.erik.mobibot.commands.tell
import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.bot 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.helpFormat
import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.isChannelOp
import net.thauvin.erik.mobibot.Utils.plural 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("To delete one or all delivered messages:")
event.sendMessage( event.sendMessage(
helpFormat( helpFormat(
buildCmdSyntax( helpCmdSyntax("%c $name $TELL_DEL_KEYWORD <id|$TELL_ALL_KEYWORD>", event.bot().nick, true)
"%c $name $TELL_DEL_KEYWORD <id|$TELL_ALL_KEYWORD>",
event.bot().nick,
true
)
) )
) )
event.sendMessage(help.last()) event.sendMessage(help.last())

View file

@ -32,7 +32,7 @@
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Utils.bot 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 net.thauvin.erik.mobibot.Utils.sendMessage
import org.pircbotx.hooks.events.PrivateMessageEvent import org.pircbotx.hooks.events.PrivateMessageEvent
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
@ -79,7 +79,7 @@ abstract class AbstractModule {
*/ */
open fun helpResponse(event: GenericMessageEvent): Boolean { open fun helpResponse(event: GenericMessageEvent): Boolean {
for (h in help) { 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 return true
} }

View file

@ -33,7 +33,7 @@ package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.bot 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.helpFormat
import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendList
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
@ -111,11 +111,11 @@ class CurrencyConverter : ThreadedModule() {
} else { } else {
val nick = event.bot().nick val nick = event.bot().nick
event.sendMessage("To convert from one currency to another:") 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("For a listing of current reference rates:")
event.sendMessage( event.sendMessage(
helpFormat( 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: ") event.sendMessage("The supported currencies are: ")
@ -161,13 +161,14 @@ class CurrencyConverter : ThreadedModule() {
} else { } else {
val to = cmds[1].uppercase() val to = cmds[1].uppercase()
val from = cmds[3].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 { try {
val amt = cmds[0].replace(",", "").toDouble() val amt = cmds[0].replace(",", "").toDouble()
val doubleFrom = EXCHANGE_RATES[to]!!.toDouble()
val doubleTo = EXCHANGE_RATES[from]!!.toDouble()
PublicMessage( PublicMessage(
amt.formatCurrency(to) + " = " + (amt * doubleTo / doubleFrom).formatCurrency(from) amt.formatCurrency(to) + " = "
+ (amt * toRate.toDouble() / fromRate.toDouble()).formatCurrency(from)
) )
} catch (e: NumberFormatException) { } catch (e: NumberFormatException) {
ErrorMessage("Let's try with some real numbers next time, okay?") ErrorMessage("Let's try with some real numbers next time, okay?")

View file

@ -37,7 +37,6 @@ import assertk.assertions.isEqualTo
import assertk.assertions.length import assertk.assertions.length
import net.thauvin.erik.mobibot.Utils.appendIfMissing import net.thauvin.erik.mobibot.Utils.appendIfMissing
import net.thauvin.erik.mobibot.Utils.bold 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.capitalise
import net.thauvin.erik.mobibot.Utils.capitalizeWords import net.thauvin.erik.mobibot.Utils.capitalizeWords
import net.thauvin.erik.mobibot.Utils.colorize 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.encodeUrl
import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.getIntProperty
import net.thauvin.erik.mobibot.Utils.green 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.helpFormat
import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.obfuscate 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.toUtcDateTime
import net.thauvin.erik.mobibot.Utils.today import net.thauvin.erik.mobibot.Utils.today
import net.thauvin.erik.mobibot.Utils.unescapeXml 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 net.thauvin.erik.mobibot.msg.Message.Companion.DEFAULT_COLOR
import org.pircbotx.Colors import org.pircbotx.Colors
import org.testng.annotations.BeforeClass import org.testng.annotations.BeforeClass
@ -105,15 +103,6 @@ class UtilsTest {
assertThat("test".bold(), "test.bold()").isEqualTo(Colors.BOLD + "test" + Colors.BOLD) 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 @Test
fun testCapitalise() { fun testCapitalise() {
@ -172,6 +161,15 @@ class UtilsTest {
assertThat(ascii.green()).isEqualTo(Colors.DARK_GREEN + ascii + Colors.NORMAL) 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 @Test
fun testHelpFormat() { fun testHelpFormat() {
assertThat(helpFormat(test, isBold = true, isIndent = false), "bold") 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 @Test
@Throws(IOException::class) @Throws(IOException::class)
fun testUrlReader() { fun testUrlReader() {