From d22c8e57bbf8cbaadcde1344fdbf789c67413670 Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Tue, 21 May 2019 15:13:39 +0200 Subject: [PATCH] Add a guaranteed mention. Begin permission work --- .../nl/voidcorp/discord/command/Command.kt | 15 ++++++----- .../voidcorp/discord/command/CommandGroup.kt | 9 ++++--- .../voidcorp/discord/command/CommandLevel.kt | 3 +++ .../nl/voidcorp/discord/commands/Echo.kt | 7 +++-- .../discord/commands/{Yeet.kt => Nice.kt} | 4 +-- .../discord/events/CommandListener.kt | 26 ++++++++++++++++--- 6 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt rename src/main/kotlin/nl/voidcorp/discord/commands/{Yeet.kt => Nice.kt} (84%) diff --git a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt index cb35598..7d445ed 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt @@ -10,6 +10,7 @@ abstract class Command( val name: String, val helpMesage: String = "", val usage: String = "", + val commandGroup: CommandLevel = CommandGroup.VERIFIED, val aliases: List = emptyList(), val location: CommandSource = CommandSource.BOTH, val permissions: Set = emptySet() @@ -41,15 +42,17 @@ abstract class Command( } } - private fun guildStuff(event: MessageReceivedEvent, str: String): CommandResult { - val intersect = permissions.intersect(event.member?.permissions as Iterable) - return if ((intersect.isNotEmpty() or permissions.isEmpty())) { + private fun guildStuff(event: MessageReceivedEvent, str: String) = + if ((permissions.intersect(event.member?.permissions as Iterable).isNotEmpty() or permissions.isEmpty())) try { handle(CommandMessage(event, translateCommandline(str))) + } catch (e: Exception) { + CommandResult.ERROR } else CommandResult.PERMISSIONS - } - private fun privateStuff(event: MessageReceivedEvent, str: String): CommandResult { - return handle(CommandMessage(event, translateCommandline(str))) + private fun privateStuff(event: MessageReceivedEvent, str: String) = try { + handle(CommandMessage(event, translateCommandline(str))) + } catch (e: Exception) { + CommandResult.ERROR } abstract fun handle(event: CommandMessage): CommandResult diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandGroup.kt index 4a067ab..d84935a 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 -enum class CommandGroup { - GENERAL, - ADMIN, - MEME +object CommandGroup { + val ADMIN = CommandLevel("Administrator") + val MODERATOR = CommandLevel("Moderator", ADMIN) + val VERIFIED = CommandLevel("Verified", MODERATOR) + val UNVERIFIED = CommandLevel("Unverified", VERIFIED) } \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt new file mode 100644 index 0000000..39e3188 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/command/CommandLevel.kt @@ -0,0 +1,3 @@ +package nl.voidcorp.discord.command + +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/Echo.kt b/src/main/kotlin/nl/voidcorp/discord/commands/Echo.kt index 3503645..00cc999 100644 --- a/src/main/kotlin/nl/voidcorp/discord/commands/Echo.kt +++ b/src/main/kotlin/nl/voidcorp/discord/commands/Echo.kt @@ -6,9 +6,12 @@ import nl.voidcorp.discord.command.CommandResult import org.springframework.stereotype.Service @Service -class Echo : Command("echo") { +class Echo : Command("echo", usage = "echo whatever") { override fun handle(event: CommandMessage): CommandResult { - event.reply(event.params.drop(1).joinToString(" ")) + val msg = event.params.drop(1).joinToString(" ") + if (msg.isEmpty()) + return CommandResult.PARAMETERS + event.reply(msg) return CommandResult.SUCCESS } } \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/Yeet.kt b/src/main/kotlin/nl/voidcorp/discord/commands/Nice.kt similarity index 84% rename from src/main/kotlin/nl/voidcorp/discord/commands/Yeet.kt rename to src/main/kotlin/nl/voidcorp/discord/commands/Nice.kt index 9252a26..64b47dc 100644 --- a/src/main/kotlin/nl/voidcorp/discord/commands/Yeet.kt +++ b/src/main/kotlin/nl/voidcorp/discord/commands/Nice.kt @@ -6,9 +6,9 @@ import nl.voidcorp.discord.command.CommandResult import org.springframework.stereotype.Service @Service -class Yeet : Command("nice") { +class Nice : Command("nice") { override fun handle(event: CommandMessage): CommandResult { - event.reply("nice") + event.reply("_nice_") 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 6bb835f..5a42438 100644 --- a/src/main/kotlin/nl/voidcorp/discord/events/CommandListener.kt +++ b/src/main/kotlin/nl/voidcorp/discord/events/CommandListener.kt @@ -3,9 +3,9 @@ package nl.voidcorp.discord.events import net.dv8tion.jda.api.MessageBuilder import net.dv8tion.jda.api.entities.ChannelType import net.dv8tion.jda.api.events.message.MessageReceivedEvent -import net.dv8tion.jda.api.events.message.MessageUpdateEvent import net.dv8tion.jda.api.hooks.ListenerAdapter import nl.voidcorp.discord.command.Command +import nl.voidcorp.discord.command.CommandMessage import nl.voidcorp.discord.command.CommandResult import nl.voidcorp.discord.creator import nl.voidcorp.discord.logger @@ -26,10 +26,14 @@ class CommandListener( override fun onMessageReceived(event: MessageReceivedEvent) { if (event.author.isBot) return val prefix: String = when { + event.message.contentRaw.startsWith("<@${event.jda.selfUser.id}>") -> "<@${event.jda.selfUser.id}>" + event.message.contentRaw.startsWith("<@!${event.jda.selfUser.id}>") -> "<@!${event.jda.selfUser.id}>" event.channelType == ChannelType.TEXT -> Command.settings.getPrefix(event.guild) event.channelType == ChannelType.PRIVATE -> Command.settings.prefix else -> return } + + if (!event.message.contentRaw.startsWith(prefix)) return val res = commands.map { it to it.onCommand(event, prefix) }.filter { it.second != CommandResult.NOPE } @@ -47,11 +51,27 @@ class CommandListener( event.channel.sendMessage(mb.append(" since this shouldn't happen...").build()).queue() } else if (res.isEmpty()) { event.channel.sendMessage("I don't seem to know this command...").queue() + } else { + val (command, result) = res[0] + when (result) { + CommandResult.SUCCESS -> Unit + CommandResult.ERROR -> CommandMessage( + event, + listOf() + ).reply(MessageBuilder("There was an error executing this command").build()) + CommandResult.PERMISSIONS -> CommandMessage(event, listOf()).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( + MessageBuilder("Sorry, but you are missing some parameters: `${command.usage}`").build() + ) + } } } - override fun onMessageUpdate(event: MessageUpdateEvent) = - onMessageReceived(MessageReceivedEvent(event.jda, event.messageIdLong, event.message)) +// override fun onMessageUpdate(event: MessageUpdateEvent) = +// onMessageReceived(MessageReceivedEvent(event.jda, event.messageIdLong, event.message)) } \ No newline at end of file