made response optional

This commit is contained in:
kageru 2019-06-08 23:51:29 +02:00
parent 4aaebf2a0a
commit 208de19ff4
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
5 changed files with 44 additions and 17 deletions

View File

@ -1,32 +1,34 @@
package moe.kageru.kagebot
import moe.kageru.kagebot.Util.doIf
import moe.kageru.kagebot.Util.asString
import org.javacord.api.entity.message.MessageAuthor
import org.javacord.api.event.message.MessageCreateEvent
private const val AUTHOR_PLACEHOLDER = "@@"
class Command(trigger: String?, response: String?, matchType: MatchType?, private val deleteMessage: Boolean) {
class Command(
trigger: String?,
private val response: String?,
matchType: MatchType?,
private val deleteMessage: Boolean
) {
val trigger: String = trigger!!
val regex: Regex? = if (matchType == MatchType.REGEX) Regex(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, cmd.deleteMessage)
fun execute(message: MessageCreateEvent) {
if (this.deleteMessage && message.isServerMessage) {
val wasDeleted = message.deleteMessage()
if (wasDeleted.isCompletedExceptionally) {
Log.log.warning("Could not delete message ${message.asString()}")
}
if (this.deleteMessage && message.message.canYouDelete()) {
message.deleteMessage()
}
this.response?.let {
message.channel.sendMessage(respond(message.messageAuthor))
}
message.channel.sendMessage(respond(message.messageAuthor))
}
fun matches(msg: String) = this.matchType.matches(msg, this)
private fun respond(author: MessageAuthor) = this.response.doIf({ it.contains(AUTHOR_PLACEHOLDER) }) {
private fun respond(author: MessageAuthor) = this.response!!.doIf({ it.contains(AUTHOR_PLACEHOLDER) }) {
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))
}
}

View File

@ -8,7 +8,7 @@ import org.javacord.api.event.message.MessageCreateEvent
class Kagebot {
companion object {
fun processMessage(event: MessageCreateEvent) {
if (!event.messageAuthor.isYourself) {
if (event.messageAuthor.isYourself) {
return
}
for (command in config.commands) {

View File

@ -24,6 +24,10 @@ trigger = "answer me"
# i.e. “@author there you go”
response = "@@ there you go"
[[commands]]
trigger = "delet this"
deleteMessage = true
[[commands]]
trigger = "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC3]|:wink:|😉)[^`]*$"
response = "@@ Oboe!"

View File

@ -2,23 +2,37 @@ package moe.kageru.kagebot
import io.kotlintest.shouldBe
import io.kotlintest.specs.StringSpec
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
class CommandTest : StringSpec({
"should match prefix command" {
testMessage("!ping", "pong")
testMessageSuccess("!ping", "pong")
}
"should match contains command" {
testMessage("the trigger is somewhere in this message", "found it")
testMessageSuccess("the trigger is somewhere in this message", "found it")
}
"should match regex command" {
testMessage("AcsdB", "regex matched")
testMessageSuccess("AcsdB", "regex matched")
}
"should ping author" {
testMessage("answer me", "<@1> there you go")
testMessageSuccess("answer me", "<@1> there you go")
}
"should not react to own message" {
val calls = mutableListOf<String>()
Kagebot.processMessage(TestUtil.mockMessage("!ping", capturedCalls = calls, isSelf = true))
calls.size shouldBe 0
}
"should delete messages" {
val mockMessage = TestUtil.mockMessage("delet this")
every { mockMessage.deleteMessage() } returns mockk()
Kagebot.processMessage(mockMessage)
verify(exactly = 1) { mockMessage.deleteMessage() }
}
}) {
companion object {
fun testMessage(content: String, result: String) {
fun testMessageSuccess(content: String, result: String) {
val calls = mutableListOf<String>()
Kagebot.processMessage(TestUtil.mockMessage(content, capturedCalls = calls))
calls.size shouldBe 1

View File

@ -5,11 +5,18 @@ import io.mockk.mockk
import org.javacord.api.event.message.MessageCreateEvent
object TestUtil {
fun mockMessage(content: String, author: Long = 1, capturedCalls: MutableList<String> = mutableListOf()): MessageCreateEvent {
fun mockMessage(
content: String,
author: Long = 1,
capturedCalls: MutableList<String> = mutableListOf(),
isSelf: Boolean = false
): MessageCreateEvent {
val message = mockk<MessageCreateEvent>()
every { message.messageContent } returns content
every { message.messageAuthor.id } returns author
every { message.channel.sendMessage(capture(capturedCalls)) } returns mockk()
every { message.messageAuthor.isYourself } returns isSelf
every { message.message.canYouDelete() } returns true
return message
}
}