Changed buildCmdSyntax to helpCmdSyntax
This commit is contained in:
parent
0dd02d7039
commit
ea40d71f14
9 changed files with 44 additions and 60 deletions
|
@ -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} <command>",
|
||||
event.bot().nick,
|
||||
event is PrivateMessageEvent
|
||||
)
|
||||
),
|
||||
helpFormat(
|
||||
helpCmdSyntax("%c ${Constants.HELP_CMD} <command>", event.bot().nick, event is PrivateMessageEvent)
|
||||
)
|
||||
)
|
||||
event.sendMessage("The commands are:")
|
||||
event.sendList(addons.names.commands, 8, isBold = true, isIndent = true)
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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<String, String> = ConcurrentHashMap()
|
||||
val properties: MutableMap<String, String> = 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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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 <id|$TELL_ALL_KEYWORD>",
|
||||
event.bot().nick,
|
||||
true
|
||||
)
|
||||
helpCmdSyntax("%c $name $TELL_DEL_KEYWORD <id|$TELL_ALL_KEYWORD>", event.bot().nick, true)
|
||||
)
|
||||
)
|
||||
event.sendMessage(help.last())
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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?")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue