support @mention of author in response

This commit is contained in:
kageru 2019-06-08 21:14:57 +02:00
parent 80eca6a2af
commit 24d43c4a7f
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
6 changed files with 35 additions and 4 deletions

View File

@ -1,15 +1,22 @@
package moe.kageru.kagebot
import org.javacord.api.entity.message.MessageAuthor
import moe.kageru.kagebot.Util.doIf
private const val AUTHOR_PLACEHOLDER = "@@"
class Command(trigger: String?, response: String?, matchType: MatchType?) {
val trigger: String = trigger!!
val regex: Regex? = if (matchType == MatchType.REGEX) Regex.fromLiteral(trigger!!) else null
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)
fun matches(msg: String) = this.matchType.matches(msg, this)
fun respond() = this.response
fun respond(author: MessageAuthor) = this.response.doIf({ it.contains(AUTHOR_PLACEHOLDER) }) {
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))
}
}
enum class MatchType {

View File

@ -0,0 +1,9 @@
package moe.kageru.kagebot
import org.javacord.api.entity.message.MessageAuthor
object MessageUtil {
fun mention(user: MessageAuthor): String {
return "<@${user.id}>"
}
}

View File

@ -0,0 +1,7 @@
package moe.kageru.kagebot
object Util {
inline fun <T> T.doIf(condition: (T) -> Boolean, op: (T) -> T): T {
return if (condition(this)) op(this) else this
}
}

View File

@ -25,7 +25,7 @@ fun createBot() {
api.addMessageCreateListener { event ->
for (command in config.commands) {
if (command.matches(event.messageContent)) {
event.channel.sendMessage(command.respond())
event.channel.sendMessage(command.respond(event.messageAuthor))
break
}
}

View File

@ -11,4 +11,10 @@ response = "pong"
[[commands]]
trigger = "somewhere"
response = "found it"
matchType = "CONTAINS"
matchType = "CONTAINS"
[[commands]]
trigger = "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC3]|:wink:|😉)[^`]*$"
response = "@@ Oboe!"
matchType = "REGEX"
deleteMessage = true

View File

@ -0,0 +1,2 @@
package moe.kageru.kagebot