add deletion parameter to command
This commit is contained in:
parent
3fd802682b
commit
4aaebf2a0a
|
@ -1,20 +1,32 @@
|
|||
package moe.kageru.kagebot
|
||||
|
||||
import org.javacord.api.entity.message.MessageAuthor
|
||||
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?) {
|
||||
class Command(trigger: String?, 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)
|
||||
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()}")
|
||||
}
|
||||
}
|
||||
message.channel.sendMessage(respond(message.messageAuthor))
|
||||
}
|
||||
|
||||
fun matches(msg: String) = this.matchType.matches(msg, this)
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,12 @@ import org.javacord.api.event.message.MessageCreateEvent
|
|||
class Kagebot {
|
||||
companion object {
|
||||
fun processMessage(event: MessageCreateEvent) {
|
||||
if (!event.messageAuthor.isYourself) {
|
||||
return
|
||||
}
|
||||
for (command in config.commands) {
|
||||
if (command.matches(event.messageContent)) {
|
||||
event.channel.sendMessage(command.respond(event.messageAuthor))
|
||||
command.execute(event)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
package moe.kageru.kagebot
|
||||
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
|
||||
object Util {
|
||||
inline fun <T> T.doIf(condition: (T) -> Boolean, op: (T) -> T): T {
|
||||
return if (condition(this)) op(this) else this
|
||||
}
|
||||
|
||||
fun MessageCreateEvent.asString(): String =
|
||||
"<${this.messageAuthor.discriminatedName}> ${this.readableMessageContent}"
|
||||
|
||||
}
|
|
@ -7,7 +7,7 @@ fun main() {
|
|||
try {
|
||||
Kagebot()
|
||||
} catch (e: Exception) {
|
||||
log.warning("An exception occurred in the main thread, exiting. ${e.stackTrace.joinToString("\n")}")
|
||||
log.warning("An exception occurred in the main thread, exiting.\n${e.stackTrace.joinToString("\n")}")
|
||||
System.exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user