From b08deb6e8dcbcdf6f7eae3c5a74916a587f2700e Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 7 Jul 2019 10:54:20 +0200 Subject: [PATCH] Add configurable embeds as command outputs --- src/main/kotlin/moe/kageru/kagebot/Globals.kt | 2 ++ src/main/kotlin/moe/kageru/kagebot/Kagebot.kt | 3 ++- .../kotlin/moe/kageru/kagebot/MessageUtil.kt | 4 ++-- .../moe/kageru/kagebot/command/Command.kt | 6 ++++++ .../kotlin/moe/kageru/kagebot/config/Config.kt | 4 +++- .../moe/kageru/kagebot/config/RawConfig.kt | 3 ++- src/main/resources/config.toml | 4 ++++ .../moe/kageru/kagebot/command/CommandTest.kt | 18 ++++++++++++++++++ 8 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/moe/kageru/kagebot/Globals.kt b/src/main/kotlin/moe/kageru/kagebot/Globals.kt index a6d8585..7745c45 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Globals.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Globals.kt @@ -1,6 +1,7 @@ package moe.kageru.kagebot import moe.kageru.kagebot.config.Config +import moe.kageru.kagebot.config.SystemConfig import org.javacord.api.DiscordApi import org.javacord.api.entity.server.Server import java.util.concurrent.atomic.AtomicInteger @@ -9,5 +10,6 @@ object Globals { lateinit var server: Server lateinit var api: DiscordApi lateinit var config: Config + lateinit var systemConfig: SystemConfig var commandCounter: AtomicInteger = AtomicInteger(0) } \ No newline at end of file diff --git a/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt b/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt index 648a257..b4375e1 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt @@ -8,6 +8,7 @@ import org.javacord.api.DiscordApiBuilder import org.javacord.api.event.message.MessageCreateEvent import org.javacord.api.event.server.member.ServerMemberJoinEvent import java.io.File +import kotlin.system.exitProcess fun main() { Kagebot.init() @@ -55,7 +56,7 @@ object Kagebot { Globals.config = Config(RawConfig.read()) } catch (e: IllegalArgumentException) { println("Config error:\n$e,\n${e.message},\n${e.stackTrace.joinToString("\n")}") - System.exit(1) + exitProcess(1) } Runtime.getRuntime().addShutdownHook(Thread { log.info("Bot has been interrupted. Shutting down.") diff --git a/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt b/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt index 4eeae32..298af4a 100644 --- a/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt +++ b/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt @@ -16,13 +16,13 @@ object MessageUtil { fun getEmbedBuilder(): EmbedBuilder { val builder = EmbedBuilder() Globals.server.icon.ifPresent { builder.setThumbnail(it) } - return builder.setColor(Globals.config.system.color).setTimestampToNow() + return builder.setColor(Globals.systemConfig.color).setTimestampToNow() } fun mapToEmbed(contents: Map): EmbedBuilder { val builder = getEmbedBuilder() for ((heading, content) in contents) { - builder.addField(heading, content) + builder.addField(heading.removePrefix("\"").removeSuffix("\""), content) } return builder } diff --git a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt index de47e52..e10906e 100644 --- a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt +++ b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt @@ -7,6 +7,7 @@ import moe.kageru.kagebot.MessageUtil import moe.kageru.kagebot.Util.doIf import moe.kageru.kagebot.config.RawCommand import org.javacord.api.entity.message.MessageAuthor +import org.javacord.api.entity.message.embed.EmbedBuilder import org.javacord.api.event.message.MessageCreateEvent private const val AUTHOR_PLACEHOLDER = "@@" @@ -18,6 +19,7 @@ class Command(cmd: RawCommand) { private val permissions: Permissions? private val actions: MessageActions? val regex: Regex? + val embed: EmbedBuilder? init { trigger = cmd.trigger ?: throw IllegalArgumentException("Every command must have a trigger.") @@ -29,6 +31,7 @@ class Command(cmd: RawCommand) { permissions = cmd.permissions?.let { Permissions(it) } actions = cmd.actions?.let { MessageActions(it) } regex = if (matchType == MatchType.REGEX) Regex(trigger) else null + embed = cmd.embed?.let(MessageUtil::mapToEmbed) } fun execute(message: MessageCreateEvent) { @@ -45,6 +48,9 @@ class Command(cmd: RawCommand) { this.response?.let { message.channel.sendMessage(respond(message.messageAuthor, it)) } + this.embed?.let { + message.channel.sendMessage(embed) + } } fun matches(msg: String) = this.matchType.matches(msg, this) diff --git a/src/main/kotlin/moe/kageru/kagebot/config/Config.kt b/src/main/kotlin/moe/kageru/kagebot/config/Config.kt index 2b6bfb2..9ad754a 100644 --- a/src/main/kotlin/moe/kageru/kagebot/config/Config.kt +++ b/src/main/kotlin/moe/kageru/kagebot/config/Config.kt @@ -7,7 +7,7 @@ import moe.kageru.kagebot.features.Features import java.awt.Color class Config(rawConfig: RawConfig) { - val system: SystemConfig = rawConfig.system?.let(::SystemConfig) + private val system: SystemConfig = rawConfig.system?.let(::SystemConfig) ?: throw IllegalArgumentException("No [system] block in config.") var localization: Localization = rawConfig.localization?.let(::Localization) ?: throw IllegalArgumentException("No [localization] block in config.") @@ -15,8 +15,10 @@ class Config(rawConfig: RawConfig) { var features: Features init { + Globals.systemConfig = system Globals.server = api.getServerById(system.serverId).orElseThrow() this.commands = rawConfig.commands?.map(::Command) ?: emptyList() + Globals.config = this this.features = rawConfig.features?.let(::Features) ?: Features.NONE } diff --git a/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt b/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt index 1bc2bd5..95c72ed 100644 --- a/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt +++ b/src/main/kotlin/moe/kageru/kagebot/config/RawConfig.kt @@ -36,7 +36,8 @@ class RawCommand( val response: String?, val matchType: String?, val permissions: RawPermissions?, - @SerializedName("action") val actions: RawMessageActions? + @SerializedName("action") val actions: RawMessageActions?, + val embed: Map? ) class RawPermissions(val hasOneOf: List?, val hasNoneOf: List?, val onlyDM: Boolean) diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml index a0fa46c..b0d7de7 100644 --- a/src/main/resources/config.toml +++ b/src/main/resources/config.toml @@ -40,6 +40,10 @@ trigger = "A.+B" response = "regex matched" matchType = "REGEX" +[[command]] +trigger = "!embed" +embed = { "some embed heading" = "your embed content" } + [[command]] trigger = "answer me" # this will @mention the user who triggered the command, diff --git a/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt b/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt index 7c879c1..80fcf99 100644 --- a/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt +++ b/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt @@ -32,6 +32,24 @@ class CommandTest : StringSpec({ testMessageSuccess("!ping", "pong") } } + "should print embed for command" { + val calls = mutableListOf() + TestUtil.prepareTestEnvironment(calls) + val heading = "heading 1" + val content = "this is the first paragraph of the embed" + withCommands( + """ + [[command]] + trigger = "!embed" + embed = { "$heading" = "$content" } + """.trimIndent() + ) { + Kagebot.processMessage(mockMessage("!embed", replyEmbeds = calls)) + calls.size shouldBe 1 + embedToString(calls[0]) shouldContain "\"$heading\"" + embedToString(calls[0]) shouldContain "\"$content\"" + } + } "should match contains command" { withCommands( """