ottobotv2/src/main/kotlin/nl/voidcorp/discord/commands/management/SetLogChannelCommand.kt

44 lines
1.7 KiB
Kotlin

package nl.voidcorp.discord.commands.management
import nl.voidcorp.discord.command.*
import nl.voidcorp.discord.storage.GuildStore
import org.springframework.stereotype.Service
@Service
class SetLogChannelCommand : Command(
"setlogchannel",
"Sets the logging channel",
"setlogchannel #channel",
CommandLevel.MODERATOR,
listOf("loghere", "slc"),
CommandSource.GUILD,
CommandGroup.ADMIN
) {
val regex = "(?:<#?)?(\\d+)>?".toRegex()
override fun handle(event: CommandMessage): CommandResult {
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
if (event.params.drop(1).isEmpty()) {
val logs = "The log channel is " + event.guild.getTextChannelById(guild.loggingChannel ?: -1)
.let { if (it != null) "<#${it.id}>" else "Not set" }
event.reply(logs)
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.loggingChannel = 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]}`")
}
}
repo.save(guild)
if (l.isNotEmpty())
event.reply(l.joinToString(prefix = "The logging channel is: ") { "<#$it>" })
return CommandResult.SUCCESS
}
}