From b4c22756703b2c99606d0942996f67aa550d6578 Mon Sep 17 00:00:00 2001 From: kageru Date: Thu, 25 Jul 2019 21:24:46 +0200 Subject: [PATCH] Send DM to user after timeout --- .../moe/kageru/kagebot/config/ConfigParser.kt | 15 ++++++++++++--- .../kotlin/moe/kageru/kagebot/config/RawConfig.kt | 7 ++++++- .../moe/kageru/kagebot/features/TimeoutFeature.kt | 4 ++++ src/main/resources/config.toml | 2 ++ src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt | 3 ++- src/test/kotlin/moe/kageru/kagebot/TestUtil.kt | 2 +- .../moe/kageru/kagebot/command/CommandTest.kt | 1 + src/test/resources/testconfig.toml | 1 + 8 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/moe/kageru/kagebot/config/ConfigParser.kt b/src/main/kotlin/moe/kageru/kagebot/config/ConfigParser.kt index d31ea8b..0b64f54 100644 --- a/src/main/kotlin/moe/kageru/kagebot/config/ConfigParser.kt +++ b/src/main/kotlin/moe/kageru/kagebot/config/ConfigParser.kt @@ -12,7 +12,8 @@ object ConfigParser { fun initialLoad(rawConfig: RawConfig) { val systemConfig = rawConfig.system?.let(::SystemConfig) ?: throw IllegalArgumentException("No [system] block in config.") - Config.server = Globals.api.getServerById(systemConfig.serverId).orElseThrow { IllegalArgumentException("Invalid server configured.") } + Config.server = Globals.api.getServerById(systemConfig.serverId) + .orElseThrow { IllegalArgumentException("Invalid server configured.") } Config.systemConfig = systemConfig reloadLocalization(rawConfig) reloadFeatures(rawConfig) @@ -42,13 +43,21 @@ class SystemConfig(val serverId: String, val color: Color) { ) } -class Localization(val permissionDenied: String, val redirectedMessage: String, val messageDeleted: String) { +class Localization( + val permissionDenied: String, + val redirectedMessage: String, + val messageDeleted: String, + val timeout: String +) { + constructor(rawLocalization: RawLocalization) : this( permissionDenied = rawLocalization.permissionDenied ?: throw IllegalArgumentException("No [localization.permissionDenied] defined"), redirectedMessage = rawLocalization.redirectedMessage ?: throw IllegalArgumentException("No [localization.redirectMessage] defined"), messageDeleted = rawLocalization.messageDeleted - ?: throw IllegalArgumentException("No [localization.messageDeleted] defined") + ?: throw IllegalArgumentException("No [localization.messageDeleted] defined"), + timeout = rawLocalization.timeout + ?: throw IllegalArgumentException("No [localization.timeout] defined") ) } diff --git a/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt b/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt index b48e0a5..3c14aeb 100644 --- a/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt +++ b/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt @@ -34,4 +34,9 @@ class RawConfig( } class RawSystemConfig(val serverId: String?, val color: String?) -class RawLocalization(val permissionDenied: String?, val redirectedMessage: String?, val messageDeleted: String?) +class RawLocalization( + val permissionDenied: String?, + val redirectedMessage: String?, + val messageDeleted: String?, + val timeout: String? +) diff --git a/src/main/kotlin/moe/kageru/kagebot/features/TimeoutFeature.kt b/src/main/kotlin/moe/kageru/kagebot/features/TimeoutFeature.kt index 0e1d8d3..3a986d1 100644 --- a/src/main/kotlin/moe/kageru/kagebot/features/TimeoutFeature.kt +++ b/src/main/kotlin/moe/kageru/kagebot/features/TimeoutFeature.kt @@ -1,6 +1,7 @@ package moe.kageru.kagebot.features import moe.kageru.kagebot.Log +import moe.kageru.kagebot.MessageUtil.sendEmbed import moe.kageru.kagebot.Util.findRole import moe.kageru.kagebot.Util.findUser import moe.kageru.kagebot.Util.ifNotEmpty @@ -39,6 +40,9 @@ class TimeoutFeature(raw: RawTimeoutFeature) : MessageFeature { user.addRole(timeoutRole) val releaseTime = Instant.now().plus(Duration.ofMinutes(time)).epochSecond Dao.saveTimeout(releaseTime, listOf(user.id) + oldRoles) + user.sendEmbed { + addField("Timeout", Config.localization.timeout.replace("@@", time.toString())) + } Log.info("Removed roles ${oldRoles.joinToString()} from user ${user.discriminatedName}") } ?: message.channel.sendMessage("Could not find user $target. Consider using the user ID.") } diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml index 515915c..5440929 100644 --- a/src/main/resources/config.toml +++ b/src/main/resources/config.toml @@ -8,6 +8,8 @@ permissionDenied = "You do not have permission to use this command." # results in says redirectedMessage = "says" messageDeleted = "Your message was deleted because it contained a banned word or phrase." +# @@ will be replaced with the time +timeout = "You have been timed out for @@ minutes" # If this is enable, every new user will receive a welcome message. # If the user has disabled their DMs, the fallbackMessage will be sent in the fallbackChannel instead. diff --git a/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt b/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt index 7dfbe10..28c1e83 100644 --- a/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt +++ b/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt @@ -15,7 +15,7 @@ class ConfigTest : ShouldSpec({ Config.systemConfig shouldNotBe null Config.localization shouldNotBe null Config.features shouldNotBe null - Config.commands.size shouldBe 2 + Config.commands.size shouldBe 3 } "should parse test config via command" { @@ -25,6 +25,7 @@ class ConfigTest : ShouldSpec({ permissionDenied = "$denied" redirectedMessage = "says" messageDeleted = "dongered" + timeout = "timeout" [[command]] response = "this command is broken" diff --git a/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt b/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt index d9792c8..0c743d0 100644 --- a/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt +++ b/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt @@ -52,7 +52,7 @@ object TestUtil { every { messageAuthor.isBotUser } returns isBot every { messageAuthor.isYourself } returns isBot every { messageAuthor.isBotOwner } returns false - every { messageAuthor.asUser() } returns Optional.of(messageableAuthor()) + every { messageAuthor.asUser() } returns Optional.of(messageableAuthor(replyEmbeds)) } } diff --git a/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt b/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt index 1ab3b06..1b24e64 100644 --- a/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt +++ b/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt @@ -139,6 +139,7 @@ class CommandTest : StringSpec({ permissionDenied = "" messageDeleted = "whatever" redirectedMessage = "asdja" + timeout = "asdasd" """.trimIndent() ) { mockMessage.process() diff --git a/src/test/resources/testconfig.toml b/src/test/resources/testconfig.toml index 13d1aa0..fbcd9ee 100644 --- a/src/test/resources/testconfig.toml +++ b/src/test/resources/testconfig.toml @@ -6,6 +6,7 @@ color = "#1793d0" permissionDenied = "no permissions" redirectedMessage = "says" messageDeleted = "message dongered" +timeout = "timeout" [feature.welcome] fallbackChannel = "123"