properly parse config with nullability
This commit is contained in:
parent
dd37444393
commit
c72219eabb
|
@ -1,6 +1,30 @@
|
|||
package moe.kageru.kagebot
|
||||
|
||||
class Command(private val input: String, private val output: String) {
|
||||
fun matches(msg: String) = msg.startsWith(this.input)
|
||||
fun respond() = this.output
|
||||
}
|
||||
class Command(trigger: String?, response: String?, matchType: MatchType?) {
|
||||
val trigger: String = trigger!!
|
||||
val regex: Regex? = if (matchType == MatchType.REGEX) Regex.fromLiteral(trigger!!) else null
|
||||
private val response: String = response!!
|
||||
private val matchType: MatchType = matchType ?: MatchType.PREFIX
|
||||
|
||||
constructor(cmd: Command) : this(cmd.trigger, cmd.response, cmd.matchType)
|
||||
|
||||
fun matches(msg: String) = this.matchType.matches(msg, this)
|
||||
fun respond() = this.response
|
||||
}
|
||||
|
||||
enum class MatchType {
|
||||
PREFIX {
|
||||
override fun matches(message: String, command: Command) = message.startsWith(command.trigger)
|
||||
},
|
||||
FULL {
|
||||
override fun matches(message: String, command: Command) = message == command.trigger
|
||||
},
|
||||
CONTAINS {
|
||||
override fun matches(message: String, command: Command) = message.contains(command.trigger)
|
||||
},
|
||||
REGEX {
|
||||
override fun matches(message: String, command: Command) = command.regex!!.matches(message)
|
||||
};
|
||||
|
||||
abstract fun matches(message: String, command: Command): Boolean
|
||||
}
|
||||
|
|
|
@ -23,10 +23,11 @@ fun createBot() {
|
|||
})
|
||||
log.info("kagebot Mk II running")
|
||||
api.addMessageCreateListener { event ->
|
||||
if (config.commands.commands[0].matches(event.messageContent)) {
|
||||
event.channel.sendMessage(config.commands.commands[0].respond())
|
||||
println("message was created")
|
||||
for (command in config.commands.commands) {
|
||||
if (command.matches(event.messageContent)) {
|
||||
event.channel.sendMessage(command.respond())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,12 @@ admins = [
|
|||
[commands]
|
||||
commands = [
|
||||
{
|
||||
input = "!ping",
|
||||
output = "pong"
|
||||
trigger = "!ping",
|
||||
response = "pong"
|
||||
},
|
||||
{
|
||||
trigger = "somewhere",
|
||||
response = "found it",
|
||||
matchType = "CONTAINS"
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue
Block a user