OttoBot/src/main/kotlin/nl/voidcorp/dbot/commands/Categories.kt

146 lines
5.5 KiB
Kotlin

package nl.voidcorp.dbot.commands
import net.dv8tion.jda.core.Permission
import net.dv8tion.jda.core.entities.ChannelType
import net.dv8tion.jda.core.entities.TextChannel
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 {
/*if (ce.member.hasPermission(Permission.ADMINISTRATOR)) return true*/
if (AdminCategory.test(ce)) 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.title.contains("bot")
if (!test) {
ce.reply("Non music commands can only be used in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel")
}
return test and check(ce)*/
}
}
object GeneralCategory : UnityCategory("general")
/*val MusicCategory = Command.Category("Music Commands") { ce ->
if (ce.member.roles.firstOrNull { it.title.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.title == "music-bot"
if (!res) ce.reply("Music commands are only supported in the ${ce.guild.getTextChannelsByName("music-bot", true).first().asMention} voiceChannel!")
res
}
musicExists -> {
val res = ce.textChannel.title == "music"
if (!res) ce.reply("Music commands are only supported in the ${ce.guild.getTextChannelsByName("music", true).first().asMention} voiceChannel!")
res
}
botExists -> {
val res = ce.textChannel.title == "bot"
if (!res) ce.reply("Music commands are only supported in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel!")
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"), errorMessage =
{ "Music commands can only be used in ${it.asMention}!" }) {
override fun test(ce: UnityCommandEvent): Boolean {
return super.test(ce) and AdminCategory.test(ce)
}
}
object AdminCategory : UnityCategory("Admin Stuff", errorMessage = { "You need to be and admin to use this!" }) {
override fun test(ce: UnityCommandEvent): Boolean {
return when {
ce.member.hasPermission(Permission.ADMINISTRATOR) -> true
ce.member.roles.firstOrNull { it.name.equals("admin", true) } != null -> true
ce.member.roles.map { it.idLong }.any {
GSM.getSettings(ce.guild).adminRoles.contains(it)
} -> true
else -> false
}
}
}
object HiddenCategory : UnityCategory("hidden") {
override fun test(ce: UnityCommandEvent): Boolean {
if (ce.member.user.id != "168743656738521088") {
throw UnknownCommandError
} else {
return true
}
}
}
/*
val GeneralCategory = Command.Category("General commands") { ce ->
if (ce.member.roles.firstOrNull { it.title.equals("admin", true) } != null) return@Category true
if (ce.guild.getTextChannelsByName("bot", true).firstOrNull() == null) return@Category true
val test = ce.textChannel.title.contains("bot")
if (!test) {
ce.reply("Non music commands can only be used in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel")
}
test
}*/
/*val FunCategory = Command.Category("Fun Commands") { ce ->
if (ce.member.roles.firstOrNull { it.title.equals("admin", true) } != null) return@Category true
if (ce.guild.getTextChannelsByName("bot", true).firstOrNull() == null) return@Category true
val test = ce.textChannel.title.contains("bot")
if (!test) {
ce.reply("Non music commands can only be used in the ${ce.guild.getTextChannelsByName("bot", true).first().asMention} voiceChannel")
}
test
}*/
object FunCategory : UnityCategory("Fun commands")