support @mention of author in response
This commit is contained in:
parent
80eca6a2af
commit
24d43c4a7f
|
@ -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 {
|
||||
|
|
|
@ -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}>"
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
package moe.kageru.kagebot
|
||||
|
Loading…
Reference in New Issue
Block a user