2018-10-29 15:28:58 +01:00
package nl.voidcorp.dbot.commands
2018-10-30 20:38:44 +01:00
import net.dv8tion.jda.core.Permission
2018-10-29 15:28:58 +01:00
import net.dv8tion.jda.core.entities.ChannelType
import net.dv8tion.jda.core.entities.TextChannel
2018-10-30 20:38:44 +01:00
open class UnityCategory (
val name : String ,
val channels : List < String > = listOf ( " bot " ) , val roles : List < String > = listOf ( ) ,
val errorMessage : ( TextChannel ) -> String = { " This command can only be used in ${it.asMention} . " } ,
val check : ( UnityCommandEvent ) -> Boolean = { true }
) {
open fun test ( ce : UnityCommandEvent ) : Boolean {
2018-10-29 15:28:58 +01:00
/*if (ce.member.hasPermission(Permission.ADMINISTRATOR)) return true*/
if ( ce . member . roles . firstOrNull { it . name . equals ( " admin " , true ) } != null ) return true
if ( channels . all { ce . guild . getTextChannelsByName ( it , true ) . firstOrNull ( ) == null } ) return true
if ( channels . isEmpty ( ) ) return true
lateinit var ch : TextChannel
for ( channel in channels . map { ce . guild . getTextChannelsByName ( it , true ) . firstOrNull ( ) } ) {
if ( channel != null ) {
ch = channel
}
}
return if ( ce . channelType == ChannelType . PRIVATE ) {
true
} else {
if ( ce . textChannel != ch ) {
ce . reply ( errorMessage ( ch ) )
false
} else {
if ( roles . isNotEmpty ( ) ) {
val rnames = ce . member . roles . map { it . name }
if ( roles . intersect ( rnames ) . isEmpty ( ) ) {
ce . reply ( " You need one of these roles to execute this command: ${roles.joinToString { "`$it`" } } " )
return false
}
}
check ( ce )
}
}
/ *
val test = ce . textChannel . name . contains ( " bot " )
if ( ! test ) {
2018-11-05 17:41:18 +01:00
ce . reply ( " Non music commands can only be used in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel " )
2018-10-29 15:28:58 +01:00
}
return test and check ( ce ) * /
}
}
object GeneralCategory : UnityCategory ( " general " )
/ * val MusicCategory = Command . Category ( " Music Commands " ) { ce ->
if ( ce . member . roles . firstOrNull { it . name . equals ( " admin " , true ) } != null ) return @Category true
val botExists = ce . guild . getTextChannelsByName ( " bot " , true ) . firstOrNull ( ) != null
val musicExists = ce . guild . getTextChannelsByName ( " music " , true ) . firstOrNull ( ) != null
val musicBotExists = ce . guild . getTextChannelsByName ( " music-bot " , true ) . firstOrNull ( ) != null
when {
musicBotExists -> {
val res = ce . textChannel . name == " music-bot "
2018-11-05 17:41:18 +01:00
if ( ! res ) ce . reply ( " Music commands are only supported in the ${ce.guild.getTextChannelsByName("music-bot", true).first().asMention} voiceChannel! " )
2018-10-29 15:28:58 +01:00
res
}
musicExists -> {
val res = ce . textChannel . name == " music "
2018-11-05 17:41:18 +01:00
if ( ! res ) ce . reply ( " Music commands are only supported in the ${ce.guild.getTextChannelsByName("music", true).first().asMention} voiceChannel! " )
2018-10-29 15:28:58 +01:00
res
}
botExists -> {
val res = ce . textChannel . name == " bot "
2018-11-05 17:41:18 +01:00
if ( ! res ) ce . reply ( " Music commands are only supported in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel! " )
2018-10-29 15:28:58 +01:00
res
}
else -> true
}
} * /
object MusicCategory : UnityCategory ( " Music Commands " , listOf ( " bot " , " music " , " music-bot " ) , errorMessage = { " Music commands can only be used in ${it.asMention} ! " } )
object MusicCategoryPrivate : UnityCategory ( " Music Commands " , listOf ( " bot " , " music " , " music-bot " ) , listOf ( " admin " ) , { " Music commands can only be used in ${it.asMention} ! " } )
2018-10-30 20:38:44 +01:00
object AdminCategory : UnityCategory ( " Admin Stuff " ) {
override fun test ( ce : UnityCommandEvent ) : Boolean {
val res = when {
ce . member . hasPermission ( Permission . ADMINISTRATOR ) -> true
ce . member . roles . firstOrNull { it . name . equals ( " admin " , true ) } != null -> true
else -> false
}
if ( ! res ) {
ce . reply ( " You are not an admin! " )
}
return res
}
}
object HiddenCategory : UnityCategory ( " hidden " ) {
override fun test ( ce : UnityCommandEvent ) : Boolean {
if ( ce . member . user . id != " 168743656738521088 " ) {
throw UnknownCommandError
} else {
return true
}
}
}
2018-10-29 15:28:58 +01:00
/ *
val GeneralCategory = Command . Category ( " General commands " ) { ce ->
if ( ce . member . roles . firstOrNull { it . name . equals ( " admin " , true ) } != null ) return @Category true
if ( ce . guild . getTextChannelsByName ( " bot " , true ) . firstOrNull ( ) == null ) return @Category true
val test = ce . textChannel . name . contains ( " bot " )
if ( ! test ) {
2018-11-05 17:41:18 +01:00
ce . reply ( " Non music commands can only be used in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel " )
2018-10-29 15:28:58 +01:00
}
test
} * /
/ * val FunCategory = Command . Category ( " Fun Commands " ) { ce ->
if ( ce . member . roles . firstOrNull { it . name . equals ( " admin " , true ) } != null ) return @Category true
if ( ce . guild . getTextChannelsByName ( " bot " , true ) . firstOrNull ( ) == null ) return @Category true
val test = ce . textChannel . name . contains ( " bot " )
if ( ! test ) {
2018-11-05 17:41:18 +01:00
ce . reply ( " Non music commands can only be used in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel " )
2018-10-29 15:28:58 +01:00
}
test
} * /
object FunCategory : UnityCategory ( " Fun commands " )