2019-06-12 23:43:36 +02:00
package moe.kageru.kagebot.command
2019-06-15 14:15:34 +02:00
import moe.kageru.kagebot.Globals
2019-06-15 14:35:17 +02:00
import moe.kageru.kagebot.Log.log
2019-06-12 23:43:36 +02:00
import moe.kageru.kagebot.MessageUtil
import moe.kageru.kagebot.Util.doIf
2019-07-13 15:39:50 +02:00
import moe.kageru.kagebot.config.Config
2019-06-12 23:43:36 +02:00
import moe.kageru.kagebot.config.RawCommand
2019-07-11 21:05:35 +02:00
import moe.kageru.kagebot.features.MessageFeature
2019-06-12 23:43:36 +02:00
import org.javacord.api.entity.message.MessageAuthor
2019-07-07 10:54:20 +02:00
import org.javacord.api.entity.message.embed.EmbedBuilder
2019-06-12 23:43:36 +02:00
import org.javacord.api.event.message.MessageCreateEvent
private const val AUTHOR _PLACEHOLDER = " @@ "
class Command ( cmd : RawCommand ) {
val trigger : String
private val response : String ?
val matchType : MatchType
private val permissions : Permissions ?
private val actions : MessageActions ?
val regex : Regex ?
2019-07-07 10:54:20 +02:00
val embed : EmbedBuilder ?
2019-07-11 21:05:35 +02:00
val feature : MessageFeature ?
2019-06-12 23:43:36 +02:00
init {
2019-06-15 12:27:20 +02:00
trigger = cmd . trigger ?: throw IllegalArgumentException ( " Every command must have a trigger. " )
2019-06-12 23:43:36 +02:00
response = cmd . response
matchType = cmd . matchType ?. let { type ->
MatchType . values ( ) . find { it . name . equals ( type , ignoreCase = true ) }
?: throw IllegalArgumentException ( " Invalid [command.matchType]: “ ${cmd.matchType} ” " )
} ?: MatchType . PREFIX
permissions = cmd . permissions ?. let { Permissions ( it ) }
actions = cmd . actions ?. let { MessageActions ( it ) }
regex = if ( matchType == MatchType . REGEX ) Regex ( trigger ) else null
2019-07-12 14:08:22 +02:00
embed = cmd . embed ?. let ( MessageUtil :: listToEmbed )
2019-07-13 15:39:50 +02:00
feature = cmd . feature ?. let { Config . features . findByString ( it ) }
2019-06-12 23:43:36 +02:00
}
2019-07-11 20:14:22 +02:00
fun isAllowed ( message : MessageCreateEvent ) = permissions ?. isAllowed ( message ) ?: true
2019-06-12 23:43:36 +02:00
fun execute ( message : MessageCreateEvent ) {
if ( permissions ?. isAllowed ( message ) == false ) {
2019-07-13 15:39:50 +02:00
if ( Config . localization . permissionDenied . isNotBlank ( ) ) {
message . channel . sendMessage ( Config . localization . permissionDenied )
2019-06-15 11:47:11 +02:00
}
2019-06-15 14:35:17 +02:00
log . info ( " Denying command ${this.trigger} to user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id} ) " )
2019-06-12 23:43:36 +02:00
return
}
2019-06-15 14:35:17 +02:00
log . info ( " Executing command ${this.trigger} triggered by user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id} ) " )
2019-06-15 14:15:34 +02:00
Globals . commandCounter . incrementAndGet ( )
2019-06-12 23:43:36 +02:00
this . actions ?. run ( message , this )
this . response ?. let {
2019-06-15 12:48:23 +02:00
message . channel . sendMessage ( respond ( message . messageAuthor , it ) )
2019-06-12 23:43:36 +02:00
}
2019-07-07 10:54:20 +02:00
this . embed ?. let {
2019-07-14 16:35:20 +02:00
MessageUtil . sendEmbed ( message . channel , embed )
2019-07-07 10:54:20 +02:00
}
2019-07-11 21:05:35 +02:00
this . feature ?. handle ( message )
2019-06-12 23:43:36 +02:00
}
fun matches ( msg : String ) = this . matchType . matches ( msg , this )
2019-06-15 14:35:17 +02:00
2019-06-15 12:48:23 +02:00
private fun respond ( author : MessageAuthor , response : String ) = response . doIf ( { it . contains ( AUTHOR _PLACEHOLDER ) } ) {
2019-06-12 23:43:36 +02:00
it . replace ( AUTHOR _PLACEHOLDER , MessageUtil . mention ( author ) )
}
}
enum class MatchType {
PREFIX {
override fun matches ( message : String , command : Command ) = message . startsWith ( 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
}