From bd95ae33dd223aa2d9d43577f729549ff13a205a Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Tue, 21 May 2019 23:12:06 +0200 Subject: [PATCH] Add some test commands. Finalize permissions. Add maintainer level. --- build.gradle | 4 +- src/main/kotlin/nl/voidcorp/discord/Loader.kt | 3 -- .../nl/voidcorp/discord/command/Command.kt | 41 ++++++++++++++++--- .../voidcorp/discord/command/CommandGroup.kt | 3 +- .../voidcorp/discord/command/CommandLevel.kt | 2 +- .../voidcorp/discord/commands/DebugCommand.kt | 19 +++++++++ .../commands/PermissionLevelCommand.kt | 14 +++++++ .../discord/events/CommandListener.kt | 6 +-- .../nl/voidcorp/discord/storage/GuildRepo.kt | 9 ++++ .../nl/voidcorp/discord/storage/GuildStore.kt | 8 +++- .../nl/voidcorp/discord/storage/Store.kt | 8 ---- .../kotlin/nl/voidcorp/discord/CommandTest.kt | 14 +++++++ .../kotlin/nl/voidcorp/discord/MainTest.kt | 4 ++ 13 files changed, 111 insertions(+), 24 deletions(-) create mode 100644 src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt create mode 100644 src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt create mode 100644 src/main/kotlin/nl/voidcorp/discord/storage/GuildRepo.kt delete mode 100644 src/main/kotlin/nl/voidcorp/discord/storage/Store.kt create mode 100644 src/test/kotlin/nl/voidcorp/discord/CommandTest.kt create mode 100644 src/test/kotlin/nl/voidcorp/discord/MainTest.kt diff --git a/build.gradle b/build.gradle index 4fed856..ba4a8d4 100644 --- a/build.gradle +++ b/build.gradle @@ -30,11 +30,11 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" implementation 'org.jetbrains.kotlin:kotlin-reflect' - testCompile group: 'junit', name: 'junit', version: '4.12' + + testImplementation "org.junit.jupiter:junit-jupiter:5.4.2" implementation 'net.dv8tion:JDA:4.ALPHA.0_88' -// runtimeOnly "ch.qos.logback:logback-classic:1.2.1" implementation "com.h2database:h2" implementation 'org.springframework.boot:spring-boot-starter-data-jpa' diff --git a/src/main/kotlin/nl/voidcorp/discord/Loader.kt b/src/main/kotlin/nl/voidcorp/discord/Loader.kt index 8c84ebf..77fec3f 100644 --- a/src/main/kotlin/nl/voidcorp/discord/Loader.kt +++ b/src/main/kotlin/nl/voidcorp/discord/Loader.kt @@ -1,9 +1,6 @@ package nl.voidcorp.discord import net.dv8tion.jda.api.JDABuilder -import nl.voidcorp.discord.command.Command -import nl.voidcorp.discord.command.CommandMessage -import nl.voidcorp.discord.command.CommandResult import nl.voidcorp.discord.events.CommandListener import nl.voidcorp.discord.events.OttoListener import org.springframework.stereotype.Component diff --git a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt index 7d445ed..690300d 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt @@ -2,7 +2,12 @@ package nl.voidcorp.discord.command import net.dv8tion.jda.api.Permission import net.dv8tion.jda.api.entities.ChannelType +import net.dv8tion.jda.api.entities.Member import net.dv8tion.jda.api.events.message.MessageReceivedEvent +import nl.voidcorp.discord.creator +import nl.voidcorp.discord.storage.GuildRepo +import nl.voidcorp.discord.storage.GuildStore +import org.springframework.beans.factory.annotation.Autowired import java.util.* @@ -10,11 +15,12 @@ abstract class Command( val name: String, val helpMesage: String = "", val usage: String = "", - val commandGroup: CommandLevel = CommandGroup.VERIFIED, + val commandLevel: CommandLevel = CommandGroup.VERIFIED, val aliases: List = emptyList(), - val location: CommandSource = CommandSource.BOTH, - val permissions: Set = emptySet() + val location: CommandSource = CommandSource.BOTH ) { + @Autowired + lateinit var repo: GuildRepo fun onCommand(event: MessageReceivedEvent, prefix: String): CommandResult { val starts = @@ -34,7 +40,7 @@ abstract class Command( event.message.contentRaw.drop(prefix.length).trim() ) else CommandResult.NOPE CommandSource.BOTH -> - if (event.channelType == ChannelType.PRIVATE) guildStuff( + if (event.channelType == ChannelType.TEXT) guildStuff( event, event.message.contentRaw.drop(prefix.length).trim() ) @@ -43,7 +49,7 @@ abstract class Command( } private fun guildStuff(event: MessageReceivedEvent, str: String) = - if ((permissions.intersect(event.member?.permissions as Iterable).isNotEmpty() or permissions.isEmpty())) try { + if (isPermOK(commandLevel, getLevel(event.member!!))) try { handle(CommandMessage(event, translateCommandline(str))) } catch (e: Exception) { CommandResult.ERROR @@ -57,11 +63,36 @@ abstract class Command( abstract fun handle(event: CommandMessage): CommandResult + fun getLevel(member: Member): CommandLevel { + val guildStore = repo.findByGuildId(member.guild.idLong) ?: GuildStore(-1) + + return when { + member.user.idLong == creator.idLong -> CommandGroup.MAINTAINER + member.hasPermission(Permission.ADMINISTRATOR) + or guildStore.adminRoles.intersect(member.roles.map { it.idLong }).isNotEmpty() + -> CommandGroup.ADMIN + guildStore.moderatorRoles.intersect(member.roles.map { it.idLong }).isNotEmpty() + -> CommandGroup.MODERATOR + member.roles.isNotEmpty() or guildStore.defaultVerified -> CommandGroup.VERIFIED + else -> CommandGroup.UNVERIFIED + } + } companion object { var settings = CommandSettings() + fun isPermOK(required: CommandLevel, user: CommandLevel): Boolean { + var test: CommandLevel? = required + while (test != null) { + if (test == user) { + return true + } + test = test.parent + } + return false + } + /** * [code borrowed from ant.jar] * Crack a command line. diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt index d84935a..d5f4c91 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt @@ -1,7 +1,8 @@ package nl.voidcorp.discord.command object CommandGroup { - val ADMIN = CommandLevel("Administrator") + val MAINTAINER = CommandLevel("Maintainer") + val ADMIN = CommandLevel("Administrator", MAINTAINER) val MODERATOR = CommandLevel("Moderator", ADMIN) val VERIFIED = CommandLevel("Verified", MODERATOR) val UNVERIFIED = CommandLevel("Unverified", VERIFIED) diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt index 39e3188..a08fa9c 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt @@ -1,3 +1,3 @@ package nl.voidcorp.discord.command -data class CommandLevel(val levelNAme: String, val parent: CommandLevel? = null) \ No newline at end of file +data class CommandLevel(val levelName: String, val parent: CommandLevel? = null) \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt new file mode 100644 index 0000000..3cd19b8 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/commands/DebugCommand.kt @@ -0,0 +1,19 @@ +package nl.voidcorp.discord.commands + +import nl.voidcorp.discord.command.Command +import nl.voidcorp.discord.command.CommandGroup +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 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(list.joinToString(prefix = "`debug`, ") { "`${it.name}`" }) + event.reply("EndDebugInfo") + return CommandResult.SUCCESS + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt new file mode 100644 index 0000000..79cebd7 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt @@ -0,0 +1,14 @@ +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.stereotype.Component + +@Component +class PermissionLevelCommand : Command("permissions") { + override fun handle(event: CommandMessage): CommandResult { + event.reply("Your highest permission level is `${getLevel(event.member!!).levelName}`") + return CommandResult.SUCCESS + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/events/CommandListener.kt b/src/main/kotlin/nl/voidcorp/discord/events/CommandListener.kt index 5a42438..05a4b1e 100644 --- a/src/main/kotlin/nl/voidcorp/discord/events/CommandListener.kt +++ b/src/main/kotlin/nl/voidcorp/discord/events/CommandListener.kt @@ -57,13 +57,13 @@ class CommandListener( CommandResult.SUCCESS -> Unit CommandResult.ERROR -> CommandMessage( event, - listOf() + emptyList() ).reply(MessageBuilder("There was an error executing this command").build()) - CommandResult.PERMISSIONS -> CommandMessage(event, listOf()).reply( + CommandResult.PERMISSIONS -> CommandMessage(event, emptyList()).reply( MessageBuilder("Sorry, but you don't seem to have the needed permissions to execute this command...").build() ) CommandResult.NOPE -> logger.warn("The command ${command.name} somehow responded with a nope?") - CommandResult.PARAMETERS -> CommandMessage(event, listOf()).reply( + CommandResult.PARAMETERS -> CommandMessage(event, emptyList()).reply( MessageBuilder("Sorry, but you are missing some parameters: `${command.usage}`").build() ) } diff --git a/src/main/kotlin/nl/voidcorp/discord/storage/GuildRepo.kt b/src/main/kotlin/nl/voidcorp/discord/storage/GuildRepo.kt new file mode 100644 index 0000000..95725ed --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/storage/GuildRepo.kt @@ -0,0 +1,9 @@ +package nl.voidcorp.discord.storage + +import org.springframework.data.jpa.repository.JpaRepository + + +interface GuildRepo : JpaRepository { + fun findByGuildId(id: Long): GuildStore? + fun existsByGuildId(id: Long): Boolean +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt b/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt index 2deb3f4..3204aa3 100644 --- a/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt +++ b/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt @@ -1,12 +1,18 @@ package nl.voidcorp.discord.storage import javax.annotation.Generated +import javax.persistence.ElementCollection import javax.persistence.Entity import javax.persistence.Id @Entity data class GuildStore( + var guildId: Long, + @ElementCollection var moderatorRoles: MutableList = mutableListOf(), + @ElementCollection var adminRoles: MutableList = mutableListOf(), + var defaultVerified: Boolean = false, @Id @Generated - var id: Long? = null) \ No newline at end of file + var id: Long? = null +) \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/storage/Store.kt b/src/main/kotlin/nl/voidcorp/discord/storage/Store.kt deleted file mode 100644 index 0d8fd07..0000000 --- a/src/main/kotlin/nl/voidcorp/discord/storage/Store.kt +++ /dev/null @@ -1,8 +0,0 @@ -package nl.voidcorp.discord.storage - -import org.springframework.data.repository.Repository - - -interface GuildRepo : Repository { - fun findById(id: Long): GuildStore -} \ No newline at end of file diff --git a/src/test/kotlin/nl/voidcorp/discord/CommandTest.kt b/src/test/kotlin/nl/voidcorp/discord/CommandTest.kt new file mode 100644 index 0000000..c1bca86 --- /dev/null +++ b/src/test/kotlin/nl/voidcorp/discord/CommandTest.kt @@ -0,0 +1,14 @@ +package nl.voidcorp.discord + +import nl.voidcorp.discord.command.Command +import nl.voidcorp.discord.command.CommandGroup +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class CommandTest { + + @Test + fun `Test permission slide Verified into Admin`() { + Assertions.assertTrue(Command.isPermOK(CommandGroup.VERIFIED, CommandGroup.ADMIN)) + } +} \ No newline at end of file diff --git a/src/test/kotlin/nl/voidcorp/discord/MainTest.kt b/src/test/kotlin/nl/voidcorp/discord/MainTest.kt new file mode 100644 index 0000000..a5e2f61 --- /dev/null +++ b/src/test/kotlin/nl/voidcorp/discord/MainTest.kt @@ -0,0 +1,4 @@ +package nl.voidcorp.discord + +class MainTest { +} \ No newline at end of file