Make permission denied message optional
This commit is contained in:
parent
120079ba3d
commit
f03f9bdb9f
|
@ -32,7 +32,7 @@ object Util {
|
|||
}
|
||||
|
||||
private val channelIdRegex = Regex("\\d{18}")
|
||||
fun String.isEntityId() = channelIdRegex.matches(this)
|
||||
private fun String.isEntityId() = channelIdRegex.matches(this)
|
||||
|
||||
@Throws(IllegalArgumentException::class)
|
||||
fun findRole(idOrName: String): Role {
|
||||
|
|
|
@ -31,7 +31,9 @@ class Command(cmd: RawCommand) {
|
|||
|
||||
fun execute(message: MessageCreateEvent) {
|
||||
if (permissions?.isAllowed(message) == false) {
|
||||
message.channel.sendMessage(config.localization.permissionDenied)
|
||||
if (config.localization.permissionDenied.isNotBlank()) {
|
||||
message.channel.sendMessage(config.localization.permissionDenied)
|
||||
}
|
||||
return
|
||||
}
|
||||
this.actions?.run(message, this)
|
||||
|
|
|
@ -48,8 +48,8 @@ class Localization(val permissionDenied: String, val redirectedMessage: String,
|
|||
permissionDenied = rawLocalization.permissionDenied
|
||||
?: throw IllegalArgumentException("No [localization.permissionDenied] defined"),
|
||||
redirectedMessage = rawLocalization.redirectedMessage
|
||||
?: throw IllegalArgumentException("No [localization.permissionDenied] defined"),
|
||||
?: throw IllegalArgumentException("No [localization.redirectMessage] defined"),
|
||||
messageDeleted = rawLocalization.messageDeleted
|
||||
?: throw IllegalArgumentException("No [localization.permissionDenied] defined")
|
||||
?: throw IllegalArgumentException("No [localization.messageDeleted] defined")
|
||||
)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ serverId = "356414885292277771"
|
|||
color = "#1793d0"
|
||||
|
||||
[localization]
|
||||
# empty string to disable the message
|
||||
permissionDenied = "You do not have permission to use this command."
|
||||
# results in <name> says <message>
|
||||
redirectedMessage = "says"
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package moe.kageru.kagebot
|
||||
|
||||
import io.kotlintest.matchers.string.shouldContain
|
||||
import io.kotlintest.matchers.string.shouldNotContain
|
||||
import io.kotlintest.shouldBe
|
||||
import io.kotlintest.specs.StringSpec
|
||||
import io.mockk.every
|
||||
|
@ -12,14 +11,15 @@ import moe.kageru.kagebot.TestUtil.embedToString
|
|||
import moe.kageru.kagebot.TestUtil.messageableAuthor
|
||||
import moe.kageru.kagebot.TestUtil.mockMessage
|
||||
import moe.kageru.kagebot.TestUtil.testMessageSuccess
|
||||
import moe.kageru.kagebot.TestUtil.withConfig
|
||||
import moe.kageru.kagebot.TestUtil.withCommands
|
||||
import moe.kageru.kagebot.TestUtil.withLocalization
|
||||
import org.javacord.api.entity.message.embed.EmbedBuilder
|
||||
import java.util.*
|
||||
|
||||
class CommandTest : StringSpec({
|
||||
TestUtil.prepareTestEnvironment()
|
||||
"should match prefix command" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!ping"
|
||||
|
@ -30,7 +30,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should match contains command" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "somewhere"
|
||||
|
@ -42,7 +42,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should match regex command" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "A.+B"
|
||||
|
@ -54,7 +54,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should ping author" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "answer me"
|
||||
|
@ -65,7 +65,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should not react to own message" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!ping"
|
||||
|
@ -78,7 +78,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should delete messages and send copy to author" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "delet this"
|
||||
|
@ -98,7 +98,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should refuse command without permissions" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!restricted"
|
||||
|
@ -114,10 +114,22 @@ class CommandTest : StringSpec({
|
|||
every { mockMessage.messageAuthor.asUser() } returns Optional.of(messageableAuthor())
|
||||
Kagebot.processMessage(mockMessage)
|
||||
replies shouldBe mutableListOf(config.localization.permissionDenied)
|
||||
withLocalization(
|
||||
"""
|
||||
[localization]
|
||||
permissionDenied = ""
|
||||
messageDeleted = "whatever"
|
||||
redirectedMessage = "asdja"
|
||||
""".trimIndent()
|
||||
) {
|
||||
Kagebot.processMessage(mockMessage)
|
||||
// still one string in there from earlier, nothing new was added
|
||||
replies.size shouldBe 1
|
||||
}
|
||||
}
|
||||
}
|
||||
"should accept restricted command for owner" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!restricted"
|
||||
|
@ -136,7 +148,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should accept restricted command with permissions" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!restricted"
|
||||
|
@ -159,7 +171,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should deny command to excluded roles" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!almostUnrestricted"
|
||||
|
@ -189,7 +201,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
}
|
||||
"should refuse DM only message in server channel" {
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!dm"
|
||||
|
@ -210,7 +222,7 @@ class CommandTest : StringSpec({
|
|||
"should redirect" {
|
||||
val calls = mutableListOf<EmbedBuilder>()
|
||||
TestUtil.prepareTestEnvironment(calls)
|
||||
withConfig(
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!redirect"
|
||||
|
|
|
@ -77,9 +77,19 @@ object TestUtil {
|
|||
return (embed.delegate as EmbedBuilderDelegateImpl).toJsonNode().toString()
|
||||
}
|
||||
|
||||
fun <R> withConfig(config: String, test: (() -> R)) {
|
||||
fun <R> withCommands(config: String, test: (() -> R)) {
|
||||
val oldCmds = Globals.config.commands
|
||||
val rawConfig = RawConfig.readFromString(config)
|
||||
Globals.config.reloadCommands(rawConfig)
|
||||
test()
|
||||
Globals.config.commands = oldCmds
|
||||
}
|
||||
|
||||
fun <R> withLocalization(config: String, test: (() -> R)) {
|
||||
val oldLoc = Globals.config.localization
|
||||
val rawConfig = RawConfig.readFromString(config)
|
||||
Globals.config.reloadLocalization(rawConfig.localization!!)
|
||||
test()
|
||||
Globals.config.localization = oldLoc
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user