diff --git a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt index 034b963..39b9deb 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt @@ -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 } } diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt index d5f4c91..e275c42 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt index db51935..236ea51 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt @@ -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, diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt index 3cd19b8..263dcb8 100644 --- a/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt +++ b/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt @@ -11,7 +11,7 @@ import org.springframework.stereotype.Service class DebugCommand(@Lazy private val list: List) : 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 diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/HelpCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/HelpCommand.kt new file mode 100644 index 0000000..0b01f92 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/commands/HelpCommand.kt @@ -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("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 + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/JoinRoleCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/JoinRoleCommand.kt new file mode 100644 index 0000000..e8a0f08 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/commands/JoinRoleCommand.kt @@ -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() + val success = mutableListOf() + val roles = mutableListOf() + 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 + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt index 517e153..2d5a896 100644 --- a/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt +++ b/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt @@ -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 diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt index 5b51879..427dc17 100644 --- a/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt +++ b/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt @@ -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