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

45 lines
1.7 KiB
Kotlin

package nl.voidcorp.ottobot.commands.management
import nl.voidcorp.ottobot.command.*
import org.jetbrains.exposed.sql.transactions.transaction
object AddBotChannelCommand : Command(
"addbotchannel",
aliases = listOf("abc"),
usage = "addbotchannel #channel (or just the id)",
commandLevel = CommandLevel.MODERATOR,
group = CommandGroup.ADMIN,
location = CommandSource.GUILD
) {
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.botChannels.map { it.botchannel }
.map { event.guild.getTextChannelById(it)?.id ?: "Missing channel $it" }
}
.joinToString(prefix = "Bot channels: ") { "<#$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.getTextChannelById(res.groupValues[1]) != null) {
guild.modifyBotChannel(res.groupValues[1].toLong())
val channel = event.guild.getTextChannelById(res.groupValues[1])!!
l += channel.id
} else event.reply("There is no channel with id `${res.groupValues[1]}`")
}
}
if (l.isNotEmpty())
event.reply(l.joinToString(prefix = "Added the following bot channels: ") { "<#$it>" })
return CommandResult.SUCCESS
}
}