Add a guaranteed mention. Begin permission work

merge-requests/1/head
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 helpMesage: String = "",
val usage: String = "",
val commandGroup: CommandLevel = CommandGroup.VERIFIED,
val aliases: List<String> = emptyList(),
val location: CommandSource = CommandSource.BOTH,
val permissions: Set<Permission> = 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<Permission>)
return if ((intersect.isNotEmpty() or permissions.isEmpty())) {
private fun guildStuff(event: MessageReceivedEvent, str: String) =
if ((permissions.intersect(event.member?.permissions as Iterable<Permission>).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

View File

@ -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)
}

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
@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
}
}

View File

@ -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
}
}

View File

@ -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))
}