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

56 lines
2.5 KiB
Kotlin

package nl.voidcorp.ottobot.commands
import net.dv8tion.jda.api.EmbedBuilder
import nl.voidcorp.ottobot.command.*
object HelpCommand : Command("help", commandLevel = CommandLevel.ALL) {
override fun handle(event: CommandMessage): CommandResult {
val builder =
EmbedBuilder().setAuthor(event.message.jda.selfUser.name, null, event.message.jda.selfUser.avatarUrl)
if (event.guild != null) {
builder.setColor(event.guild.selfMember.color)
}
if (event.params.drop(1).isEmpty()) {
builder.setTitle("Available Commands")
allCommands.filter {
isPermOK(it.commandLevel, getLevel(event.member))
}.filter {
(it.location == CommandSource.BOTH) or
((it.location == CommandSource.GUILD) and (event.guild != null)) or
((it.location == CommandSource.PRIVATE) and (event.guild == null))
}.groupBy({ it.group }, { it.name }).toSortedMap()
.forEach { (k, v) ->
builder.addField(k.name.capitalize(), v.joinToString(separator = "\n"), false)
}
event.reply(builder.build())
} else {
val command = event.params.drop(1).first()
when {
command == "help" -> event.reply("Help help? Help help help help!")
allCommands.none { it.name == command } -> event.reply("I have never heard of the command $command...")
allCommands.filter {
isPermOK(
it.commandLevel,
getLevel(event.member)
)
}
.none { it.name == command } -> event.reply("Sorry, I can't tell you about a command you shouldn't have access to...")
else -> {
val cmd =
allCommands.filter { isPermOK(it.commandLevel, getLevel(event.member)) }
.first { it.name == command }
builder.setTitle(command).addField("Info", cmd.helpMesage, false)
.addField("Usage", "`${cmd.usage.ifBlank { command }}`", true)
.addField("Aliases", cmd.aliases.joinToString(), true)
.addField("Minimum access level", cmd.commandLevel.levelName, true)
.addField("Group", cmd.group.name.capitalize(), true)
event.reply(builder.build())
}
}
}
return CommandResult.SUCCESS
}
}