ottobot-v3/src/main/kotlin/nl/voidcorp/ottobot/commands/management/AdminRoleCommand.kt

42 lines
1.6 KiB
Kotlin

package nl.voidcorp.ottobot.commands.management
import nl.voidcorp.ottobot.command.*
import org.jetbrains.exposed.sql.transactions.transaction
object AdminRoleCommand : Command(
"addadminrole",
commandLevel = CommandLevel.ADMIN,
location = CommandSource.GUILD,
group = CommandGroup.ADMIN,
aliases = listOf("aar")
) {
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
override fun handle(event: CommandMessage): CommandResult {
val guild = getStore(event.guild!!.idLong)
if (event.params.drop(1).isEmpty()) {
val roles = transaction {
guild.adminRoles.map { it.adminRoleId }.map { event.guild.getRoleById(it)?.name ?: "Missing role $it" }
}
.joinToString(prefix = "Admin roles: ") { "`$it`" }
event.reply(roles)
return CommandResult.SUCCESS
}
val l = mutableListOf<String>()
for (p in event.params.drop(1)) {
val res = regex.matchEntire(p)
if (res != null && res.groupValues.size == 2) {
if (event.guild.getRoleById(res.groupValues[1]) != null) {
transaction { guild.modifyAdminRole(res.groupValues[1].toLong()) }
val role = event.guild.getRoleById(res.groupValues[1])!!
l += role.name
} else event.reply("There is no role with id `${res.groupValues[1]}`")
}
}
if (l.isNotEmpty())
event.reply(l.joinToString(prefix = "Added the following roles as admin roles: ") { "`$it`" })
return CommandResult.SUCCESS
}
}