Remove permission denied message

This commit is contained in:
kageru 2019-11-11 19:09:58 +01:00
parent a8ed168122
commit 288be7e4f5
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
8 changed files with 19 additions and 44 deletions

View File

@ -1,5 +1,7 @@
package moe.kageru.kagebot
import arrow.core.extensions.list.foldable.find
import arrow.core.k
import moe.kageru.kagebot.Util.checked
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.ConfigParser
@ -20,14 +22,9 @@ object Kagebot {
handleOwn()
return
}
for (command in Config.commands) {
if (command.matches(readableMessageContent)) {
// only break if we have the permissions to execute this command, else keep searching
if (command.execute(this)) {
break
}
}
}
Config.commands
.find { it.matches(readableMessageContent) && it.isAllowed(this) }
.map { it.execute(this) }
}
private fun MessageCreateEvent.handleOwn() {

View File

@ -32,16 +32,11 @@ class Command(
val embed: EmbedBuilder? = embed?.let(MessageUtil::listToEmbed)
private val feature: MessageFeature? = feature?.let { Config.features.findByString(it) }
fun matches(msg: String) = this.matchType.matches(msg, this)
fun isAllowed(message: MessageCreateEvent) = permissions?.isAllowed(message) ?: true
fun execute(message: MessageCreateEvent): Boolean {
if (permissions?.isAllowed(message) == false) {
if (Config.localization[LocalizationSpec.permissionDenied].isNotBlank()) {
message.channel.sendMessage(Config.localization[LocalizationSpec.permissionDenied])
}
Log.info("Denying command ${this.trigger} to user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})")
return false
}
fun execute(message: MessageCreateEvent) {
Log.info("Executing command ${this.trigger} triggered by user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})")
Globals.commandCounter.incrementAndGet()
this.actions?.run(message, this)
@ -52,11 +47,8 @@ class Command(
MessageUtil.sendEmbed(message.channel, embed)
}
this.feature?.handle(message)
return true
}
fun matches(msg: String) = this.matchType.matches(msg, this)
private fun respond(author: MessageAuthor, response: String) =
response.applyIf(response.contains(AUTHOR_PLACEHOLDER)) {
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))

View File

@ -1,5 +1,7 @@
package moe.kageru.kagebot.config
import arrow.core.ListK
import arrow.core.k
import com.uchuhimo.konf.Config
import com.uchuhimo.konf.source.toml
import moe.kageru.kagebot.command.Command
@ -20,5 +22,5 @@ object Config {
// for easier access
val features: Features get() = featureConfig[FeatureSpec.features]
val commands: List<Command> get() = commandConfig[CommandSpec.command]
val commands: ListK<Command> get() = commandConfig[CommandSpec.command].k()
}

View File

@ -13,7 +13,6 @@ object SystemSpec : ConfigSpec() {
}
object LocalizationSpec : ConfigSpec() {
val permissionDenied by optional("You do not have the permission to use this command.")
val redirectedMessage by optional("says")
val messageDeleted by optional("Your message was deleted.")
val timeout by optional("You have been timed out for @@ minutes.")

View File

@ -1,5 +1,6 @@
package moe.kageru.kagebot.features
import arrow.core.extensions.listk.functorFilter.filter
import moe.kageru.kagebot.MessageUtil.sendEmbed
import moe.kageru.kagebot.command.MatchType
import moe.kageru.kagebot.config.Config

View File

@ -22,11 +22,10 @@ class ConfigTest : ShouldSpec({
}
"should parse test config via command" {
val denied = "denied"
val redir = "says"
val testConfig = """
[localization]
permissionDenied = "$denied"
redirectedMessage = "says"
redirectedMessage = "$redir"
messageDeleted = "dongered"
timeout = "timeout"
""".trimIndent()
@ -35,6 +34,6 @@ class ConfigTest : ShouldSpec({
every { url.openStream().readAllBytes() } returns testConfig.toByteArray()
})
SetConfigFeature().handle(message)
Config.localization[LocalizationSpec.permissionDenied] shouldBe denied
Config.localization[LocalizationSpec.redirectedMessage] shouldBe redir
}
})

View File

@ -17,8 +17,6 @@ import moe.kageru.kagebot.TestUtil.withCommands
import moe.kageru.kagebot.TestUtil.withLocalization
import moe.kageru.kagebot.Util
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.Config.localization
import moe.kageru.kagebot.config.LocalizationSpec
import moe.kageru.kagebot.persistence.Dao
import org.javacord.api.entity.message.embed.EmbedBuilder
import org.javacord.api.entity.permission.Role
@ -149,20 +147,7 @@ class CommandTest : StringSpec({
val replies = mutableListOf<String>()
val mockMessage = mockMessage("!restricted", replies = replies)
mockMessage.process()
replies shouldBe mutableListOf(localization[LocalizationSpec.permissionDenied])
withLocalization(
"""
[localization]
permissionDenied = ""
messageDeleted = "whatever"
redirectedMessage = "asdja"
timeout = "asdasd"
""".trimIndent()
) {
mockMessage.process()
// still one string in there from earlier, nothing new was added
replies.size shouldBe 1
}
replies shouldBe mutableListOf()
}
}
"should accept restricted command for owner" {
@ -234,7 +219,8 @@ class CommandTest : StringSpec({
every { get().getRoles(any()) } returns emptyList()
}
mockMessage.process()
calls shouldBe mutableListOf(localization[LocalizationSpec.permissionDenied], "access granted")
// first message didn’t answer anything
calls shouldBe mutableListOf("access granted")
}
}
"should refuse DM only message in server channel" {
@ -249,7 +235,7 @@ class CommandTest : StringSpec({
) {
val calls = mutableListOf<String>()
mockMessage("!dm", replies = calls).process()
calls shouldBe listOf(localization[LocalizationSpec.permissionDenied])
calls shouldBe mutableListOf()
}
}
/*

View File

@ -3,7 +3,6 @@ serverId = "356414885292277771"
color = "#1793d0"
[localization]
permissionDenied = "no permissions"
redirectedMessage = "says"
messageDeleted = "message dongered"
timeout = "timeout @@ minutes"