Remove permission denied message
This commit is contained in:
parent
a8ed168122
commit
288be7e4f5
|
@ -1,5 +1,7 @@
|
||||||
package moe.kageru.kagebot
|
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.Util.checked
|
||||||
import moe.kageru.kagebot.config.Config
|
import moe.kageru.kagebot.config.Config
|
||||||
import moe.kageru.kagebot.config.ConfigParser
|
import moe.kageru.kagebot.config.ConfigParser
|
||||||
|
@ -20,14 +22,9 @@ object Kagebot {
|
||||||
handleOwn()
|
handleOwn()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for (command in Config.commands) {
|
Config.commands
|
||||||
if (command.matches(readableMessageContent)) {
|
.find { it.matches(readableMessageContent) && it.isAllowed(this) }
|
||||||
// only break if we have the permissions to execute this command, else keep searching
|
.map { it.execute(this) }
|
||||||
if (command.execute(this)) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun MessageCreateEvent.handleOwn() {
|
private fun MessageCreateEvent.handleOwn() {
|
||||||
|
|
|
@ -32,16 +32,11 @@ class Command(
|
||||||
val embed: EmbedBuilder? = embed?.let(MessageUtil::listToEmbed)
|
val embed: EmbedBuilder? = embed?.let(MessageUtil::listToEmbed)
|
||||||
private val feature: MessageFeature? = feature?.let { Config.features.findByString(it) }
|
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 isAllowed(message: MessageCreateEvent) = permissions?.isAllowed(message) ?: true
|
||||||
|
|
||||||
fun execute(message: MessageCreateEvent): Boolean {
|
fun execute(message: MessageCreateEvent) {
|
||||||
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
|
|
||||||
}
|
|
||||||
Log.info("Executing command ${this.trigger} triggered by user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})")
|
Log.info("Executing command ${this.trigger} triggered by user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})")
|
||||||
Globals.commandCounter.incrementAndGet()
|
Globals.commandCounter.incrementAndGet()
|
||||||
this.actions?.run(message, this)
|
this.actions?.run(message, this)
|
||||||
|
@ -52,11 +47,8 @@ class Command(
|
||||||
MessageUtil.sendEmbed(message.channel, embed)
|
MessageUtil.sendEmbed(message.channel, embed)
|
||||||
}
|
}
|
||||||
this.feature?.handle(message)
|
this.feature?.handle(message)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun matches(msg: String) = this.matchType.matches(msg, this)
|
|
||||||
|
|
||||||
private fun respond(author: MessageAuthor, response: String) =
|
private fun respond(author: MessageAuthor, response: String) =
|
||||||
response.applyIf(response.contains(AUTHOR_PLACEHOLDER)) {
|
response.applyIf(response.contains(AUTHOR_PLACEHOLDER)) {
|
||||||
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))
|
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package moe.kageru.kagebot.config
|
package moe.kageru.kagebot.config
|
||||||
|
|
||||||
|
import arrow.core.ListK
|
||||||
|
import arrow.core.k
|
||||||
import com.uchuhimo.konf.Config
|
import com.uchuhimo.konf.Config
|
||||||
import com.uchuhimo.konf.source.toml
|
import com.uchuhimo.konf.source.toml
|
||||||
import moe.kageru.kagebot.command.Command
|
import moe.kageru.kagebot.command.Command
|
||||||
|
@ -20,5 +22,5 @@ object Config {
|
||||||
|
|
||||||
// for easier access
|
// for easier access
|
||||||
val features: Features get() = featureConfig[FeatureSpec.features]
|
val features: Features get() = featureConfig[FeatureSpec.features]
|
||||||
val commands: List<Command> get() = commandConfig[CommandSpec.command]
|
val commands: ListK<Command> get() = commandConfig[CommandSpec.command].k()
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ object SystemSpec : ConfigSpec() {
|
||||||
}
|
}
|
||||||
|
|
||||||
object LocalizationSpec : ConfigSpec() {
|
object LocalizationSpec : ConfigSpec() {
|
||||||
val permissionDenied by optional("You do not have the permission to use this command.")
|
|
||||||
val redirectedMessage by optional("says")
|
val redirectedMessage by optional("says")
|
||||||
val messageDeleted by optional("Your message was deleted.")
|
val messageDeleted by optional("Your message was deleted.")
|
||||||
val timeout by optional("You have been timed out for @@ minutes.")
|
val timeout by optional("You have been timed out for @@ minutes.")
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package moe.kageru.kagebot.features
|
package moe.kageru.kagebot.features
|
||||||
|
|
||||||
|
import arrow.core.extensions.listk.functorFilter.filter
|
||||||
import moe.kageru.kagebot.MessageUtil.sendEmbed
|
import moe.kageru.kagebot.MessageUtil.sendEmbed
|
||||||
import moe.kageru.kagebot.command.MatchType
|
import moe.kageru.kagebot.command.MatchType
|
||||||
import moe.kageru.kagebot.config.Config
|
import moe.kageru.kagebot.config.Config
|
||||||
|
|
|
@ -22,11 +22,10 @@ class ConfigTest : ShouldSpec({
|
||||||
}
|
}
|
||||||
|
|
||||||
"should parse test config via command" {
|
"should parse test config via command" {
|
||||||
val denied = "denied"
|
val redir = "says"
|
||||||
val testConfig = """
|
val testConfig = """
|
||||||
[localization]
|
[localization]
|
||||||
permissionDenied = "$denied"
|
redirectedMessage = "$redir"
|
||||||
redirectedMessage = "says"
|
|
||||||
messageDeleted = "dongered"
|
messageDeleted = "dongered"
|
||||||
timeout = "timeout"
|
timeout = "timeout"
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
|
@ -35,6 +34,6 @@ class ConfigTest : ShouldSpec({
|
||||||
every { url.openStream().readAllBytes() } returns testConfig.toByteArray()
|
every { url.openStream().readAllBytes() } returns testConfig.toByteArray()
|
||||||
})
|
})
|
||||||
SetConfigFeature().handle(message)
|
SetConfigFeature().handle(message)
|
||||||
Config.localization[LocalizationSpec.permissionDenied] shouldBe denied
|
Config.localization[LocalizationSpec.redirectedMessage] shouldBe redir
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -17,8 +17,6 @@ import moe.kageru.kagebot.TestUtil.withCommands
|
||||||
import moe.kageru.kagebot.TestUtil.withLocalization
|
import moe.kageru.kagebot.TestUtil.withLocalization
|
||||||
import moe.kageru.kagebot.Util
|
import moe.kageru.kagebot.Util
|
||||||
import moe.kageru.kagebot.config.Config
|
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 moe.kageru.kagebot.persistence.Dao
|
||||||
import org.javacord.api.entity.message.embed.EmbedBuilder
|
import org.javacord.api.entity.message.embed.EmbedBuilder
|
||||||
import org.javacord.api.entity.permission.Role
|
import org.javacord.api.entity.permission.Role
|
||||||
|
@ -149,20 +147,7 @@ class CommandTest : StringSpec({
|
||||||
val replies = mutableListOf<String>()
|
val replies = mutableListOf<String>()
|
||||||
val mockMessage = mockMessage("!restricted", replies = replies)
|
val mockMessage = mockMessage("!restricted", replies = replies)
|
||||||
mockMessage.process()
|
mockMessage.process()
|
||||||
replies shouldBe mutableListOf(localization[LocalizationSpec.permissionDenied])
|
replies shouldBe mutableListOf()
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"should accept restricted command for owner" {
|
"should accept restricted command for owner" {
|
||||||
|
@ -234,7 +219,8 @@ class CommandTest : StringSpec({
|
||||||
every { get().getRoles(any()) } returns emptyList()
|
every { get().getRoles(any()) } returns emptyList()
|
||||||
}
|
}
|
||||||
mockMessage.process()
|
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" {
|
"should refuse DM only message in server channel" {
|
||||||
|
@ -249,7 +235,7 @@ class CommandTest : StringSpec({
|
||||||
) {
|
) {
|
||||||
val calls = mutableListOf<String>()
|
val calls = mutableListOf<String>()
|
||||||
mockMessage("!dm", replies = calls).process()
|
mockMessage("!dm", replies = calls).process()
|
||||||
calls shouldBe listOf(localization[LocalizationSpec.permissionDenied])
|
calls shouldBe mutableListOf()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -3,7 +3,6 @@ serverId = "356414885292277771"
|
||||||
color = "#1793d0"
|
color = "#1793d0"
|
||||||
|
|
||||||
[localization]
|
[localization]
|
||||||
permissionDenied = "no permissions"
|
|
||||||
redirectedMessage = "says"
|
redirectedMessage = "says"
|
||||||
messageDeleted = "message dongered"
|
messageDeleted = "message dongered"
|
||||||
timeout = "timeout @@ minutes"
|
timeout = "timeout @@ minutes"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user