Add a guaranteed mention. Begin permission work

This commit is contained in:
Julius de Jeu 2019-05-21 15:13:39 +02:00
parent 4ee192750e
commit d22c8e57bb
6 changed files with 47 additions and 17 deletions

View file

@ -10,6 +10,7 @@ abstract class Command(
val name: String, val name: String,
val helpMesage: String = "", val helpMesage: String = "",
val usage: String = "", val usage: String = "",
val commandGroup: CommandLevel = CommandGroup.VERIFIED,
val aliases: List<String> = emptyList(), val aliases: List<String> = emptyList(),
val location: CommandSource = CommandSource.BOTH, val location: CommandSource = CommandSource.BOTH,
val permissions: Set<Permission> = emptySet() val permissions: Set<Permission> = emptySet()
@ -41,15 +42,17 @@ abstract class Command(
} }
} }
private fun guildStuff(event: MessageReceivedEvent, str: String): CommandResult { private fun guildStuff(event: MessageReceivedEvent, str: String) =
val intersect = permissions.intersect(event.member?.permissions as Iterable<Permission>) if ((permissions.intersect(event.member?.permissions as Iterable<Permission>).isNotEmpty() or permissions.isEmpty())) try {
return if ((intersect.isNotEmpty() or permissions.isEmpty())) {
handle(CommandMessage(event, translateCommandline(str))) handle(CommandMessage(event, translateCommandline(str)))
} catch (e: Exception) {
CommandResult.ERROR
} else CommandResult.PERMISSIONS } else CommandResult.PERMISSIONS
}
private fun privateStuff(event: MessageReceivedEvent, str: String): CommandResult { private fun privateStuff(event: MessageReceivedEvent, str: String) = try {
return handle(CommandMessage(event, translateCommandline(str))) handle(CommandMessage(event, translateCommandline(str)))
} catch (e: Exception) {
CommandResult.ERROR
} }
abstract fun handle(event: CommandMessage): CommandResult abstract fun handle(event: CommandMessage): CommandResult

View file

@ -1,7 +1,8 @@
package nl.voidcorp.discord.command package nl.voidcorp.discord.command
enum class CommandGroup { object CommandGroup {
GENERAL, val ADMIN = CommandLevel("Administrator")
ADMIN, val MODERATOR = CommandLevel("Moderator", ADMIN)
MEME val VERIFIED = CommandLevel("Verified", MODERATOR)
val UNVERIFIED = CommandLevel("Unverified", VERIFIED)
} }

View file

@ -0,0 +1,3 @@
package nl.voidcorp.discord.command
data class CommandLevel(val levelNAme: String, val parent: CommandLevel? = null)

View file

@ -6,9 +6,12 @@ import nl.voidcorp.discord.command.CommandResult
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
class Echo : Command("echo") { class Echo : Command("echo", usage = "echo whatever") {
override fun handle(event: CommandMessage): CommandResult { 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 return CommandResult.SUCCESS
} }
} }

View file

@ -6,9 +6,9 @@ import nl.voidcorp.discord.command.CommandResult
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
class Yeet : Command("nice") { class Nice : Command("nice") {
override fun handle(event: CommandMessage): CommandResult { override fun handle(event: CommandMessage): CommandResult {
event.reply("nice") event.reply("_nice_")
return CommandResult.SUCCESS return CommandResult.SUCCESS
} }
} }

View file

@ -3,9 +3,9 @@ package nl.voidcorp.discord.events
import net.dv8tion.jda.api.MessageBuilder import net.dv8tion.jda.api.MessageBuilder
import net.dv8tion.jda.api.entities.ChannelType import net.dv8tion.jda.api.entities.ChannelType
import net.dv8tion.jda.api.events.message.MessageReceivedEvent import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.events.message.MessageUpdateEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter import net.dv8tion.jda.api.hooks.ListenerAdapter
import nl.voidcorp.discord.command.Command import nl.voidcorp.discord.command.Command
import nl.voidcorp.discord.command.CommandMessage
import nl.voidcorp.discord.command.CommandResult import nl.voidcorp.discord.command.CommandResult
import nl.voidcorp.discord.creator import nl.voidcorp.discord.creator
import nl.voidcorp.discord.logger import nl.voidcorp.discord.logger
@ -26,10 +26,14 @@ class CommandListener(
override fun onMessageReceived(event: MessageReceivedEvent) { override fun onMessageReceived(event: MessageReceivedEvent) {
if (event.author.isBot) return if (event.author.isBot) return
val prefix: String = when { 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.TEXT -> Command.settings.getPrefix(event.guild)
event.channelType == ChannelType.PRIVATE -> Command.settings.prefix event.channelType == ChannelType.PRIVATE -> Command.settings.prefix
else -> return else -> return
} }
if (!event.message.contentRaw.startsWith(prefix)) return if (!event.message.contentRaw.startsWith(prefix)) return
val res = commands.map { it to it.onCommand(event, prefix) }.filter { it.second != CommandResult.NOPE } 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() event.channel.sendMessage(mb.append(" since this shouldn't happen...").build()).queue()
} else if (res.isEmpty()) { } else if (res.isEmpty()) {
event.channel.sendMessage("I don't seem to know this command...").queue() 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) = // override fun onMessageUpdate(event: MessageUpdateEvent) =
onMessageReceived(MessageReceivedEvent(event.jda, event.messageIdLong, event.message)) // onMessageReceived(MessageReceivedEvent(event.jda, event.messageIdLong, event.message))
} }