properly parse config with nullability

This commit is contained in:
kageru 2019-06-08 12:28:14 +02:00
parent dd37444393
commit c72219eabb
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 40 additions and 10 deletions

View File

@ -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
}

View File

@ -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
}
}
}
}

View File

@ -7,7 +7,12 @@ admins = [
[commands]
commands = [
{
input = "!ping",
output = "pong"
trigger = "!ping",
response = "pong"
},
{
trigger = "somewhere",
response = "found it",
matchType = "CONTAINS"
}
]