Add the remaining role commands and disable listroles. Fix an issue with command permissions in private channels. Add help and make it print a list of commands.

This commit is contained in:
Julius de Jeu 2019-05-23 12:25:13 +02:00
parent 8c39f6882f
commit 94970a3e6a
8 changed files with 86 additions and 20 deletions

View file

@ -49,25 +49,28 @@ abstract class Command(
}
private fun guildStuff(event: MessageReceivedEvent, str: String) =
if (isPermOK(commandLevel, getLevel(event.member!!))) try {
if (isPermOK(commandLevel, getLevel(event.member))) try {
handle(CommandMessage(event, translateCommandline(str)))
} catch (e: Exception) {
e.printStackTrace()
CommandResult.ERROR
} else CommandResult.PERMISSIONS
private fun privateStuff(event: MessageReceivedEvent, str: String) = try {
handle(CommandMessage(event, translateCommandline(str)))
} catch (e: Exception) {
e.printStackTrace()
CommandResult.ERROR
}
private fun privateStuff(event: MessageReceivedEvent, str: String) =
if (isPermOK(commandLevel, getLevel(event.member))) try {
handle(CommandMessage(event, translateCommandline(str)))
} catch (e: Exception) {
e.printStackTrace()
CommandResult.ERROR
} else {
CommandResult.PERMISSIONS
}
abstract fun handle(event: CommandMessage): CommandResult
fun getLevel(member: Member): CommandLevel {
fun getLevel(member: Member?): CommandLevel {
if (member == null) return CommandGroup.ADMIN
val guildStore = repo.findByGuildId(member.guild.idLong) ?: GuildStore(-1)
return when {
member.user.idLong == creator.idLong -> CommandGroup.MAINTAINER
member.hasPermission(Permission.ADMINISTRATOR)
@ -76,7 +79,7 @@ abstract class Command(
guildStore.moderatorRoles.intersect(member.roles.map { it.idLong }).isNotEmpty()
-> CommandGroup.MODERATOR
member.roles.isNotEmpty() or guildStore.defaultVerified -> CommandGroup.VERIFIED
else -> CommandGroup.UNVERIFIED
else -> CommandGroup.ALL
}
}

View file

@ -5,5 +5,5 @@ object CommandGroup {
val ADMIN = CommandLevel("Administrator", MAINTAINER)
val MODERATOR = CommandLevel("Moderator", ADMIN)
val VERIFIED = CommandLevel("Verified", MODERATOR)
val UNVERIFIED = CommandLevel("Unverified", VERIFIED)
val ALL = CommandLevel("Unverified", VERIFIED)
}

View file

@ -4,6 +4,7 @@ import net.dv8tion.jda.api.MessageBuilder
import net.dv8tion.jda.api.entities.*
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
data class CommandMessage(
private val event: MessageReceivedEvent,
val params: List<String>,

View file

@ -11,7 +11,7 @@ import org.springframework.stereotype.Service
class DebugCommand(@Lazy private val list: List<Command>) : Command("debug", commandLevel = CommandGroup.MAINTAINER) {
override fun handle(event: CommandMessage): CommandResult {
event.reply("DebugInfo")
event.reply("Commands found: ${list.size}")
event.reply("Commands found: ${list.size + 1}")
event.reply(list.joinToString(prefix = "`debug`, ") { "`${it.name}`" })
event.reply("EndDebugInfo")
return CommandResult.SUCCESS

View file

@ -0,0 +1,17 @@
package nl.voidcorp.discord.commands
import nl.voidcorp.discord.command.Command
import nl.voidcorp.discord.command.CommandMessage
import nl.voidcorp.discord.command.CommandResult
import org.springframework.context.annotation.Lazy
import org.springframework.stereotype.Service
@Service
class HelpCommand(@Lazy private val list: List<Command>) : Command("help") {
override fun handle(event: CommandMessage): CommandResult {
event.reply("cursed_help")
val commands = list.filter { isPermOK(it.commandLevel, getLevel(event.member)) }
event.reply(commands.joinToString { "`${it.name}`" })
return CommandResult.SUCCESS
}
}

View file

@ -0,0 +1,48 @@
package nl.voidcorp.discord.commands
import net.dv8tion.jda.api.entities.Role
import nl.voidcorp.discord.command.*
import nl.voidcorp.discord.storage.GuildStore
import org.springframework.stereotype.Service
@Service
class JoinRoleCommand :
Command(
"joinrole",
aliases = listOf("role"),
commandLevel = CommandGroup.ALL,
usage = "joinrole [rolename]",
location = CommandSource.GUILD
) {
override fun handle(event: CommandMessage): CommandResult {
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
if (event.params.size == 1) {
if (guild.roleMap.isNotEmpty())
event.reply(guild.roleMap.keys.joinToString(prefix = "The available roles are: ") { "`$it`" })
else
event.reply("There are no roles to pick here...")
} else {
val fail = mutableListOf<String>()
val success = mutableListOf<String>()
val roles = mutableListOf<Role>()
for (p in event.params.drop(1)) {
val roleID = guild.roleMap[p]
if (roleID == null) {
fail += p
} else {
val role = event.guild.getRoleById(roleID)
if (role != null) {
success += p
roles += role
} else {
fail += p
}
}
}
event.guild.controller.addRolesToMember(event.member!!, roles).queue()
if (success.isNotEmpty()) event.reply("I have given you the following roles: ${roles.joinToString { "`${it.name}`" }}!")
if (fail.isNotEmpty()) event.reply("I failed to find the following roles: ${fail.joinToString { "`$it`" }}")
}
return CommandResult.SUCCESS
}
}

View file

@ -7,10 +7,9 @@ import nl.voidcorp.discord.storage.GuildRepo
import nl.voidcorp.discord.storage.GuildStore
import org.springframework.stereotype.Service
@Service
class ListRolesCommand(val guildRepo: GuildRepo) : Command("listroles", aliases = listOf("roles")) {
class ListRolesCommand : Command("listroles", aliases = listOf("roles")) {
override fun handle(event: CommandMessage): CommandResult {
val guild = guildRepo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
if (guild.roleMap.isNotEmpty())
event.reply(guild.roleMap.keys.joinToString(prefix = "The available roles are: ") { "`$it`" })
else

View file

@ -1,13 +1,11 @@
package nl.voidcorp.discord.commands
import nl.voidcorp.discord.command.Command
import nl.voidcorp.discord.command.CommandMessage
import nl.voidcorp.discord.command.CommandResult
import nl.voidcorp.discord.command.CommandSource
import nl.voidcorp.discord.command.*
import org.springframework.stereotype.Service
@Service
class PermissionLevelCommand : Command("permissions", location = CommandSource.GUILD) {
class PermissionLevelCommand :
Command("permissions", location = CommandSource.GUILD, commandLevel = CommandGroup.ALL) {
override fun handle(event: CommandMessage): CommandResult {
event.reply("Your highest permission level is `${getLevel(event.member!!).levelName}`")
return CommandResult.SUCCESS