Add configurable embeds as command outputs

This commit is contained in:
kageru 2019-07-07 10:54:20 +02:00
parent 85206a7fc3
commit b08deb6e8d
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
8 changed files with 39 additions and 5 deletions

View File

@ -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)
}

View File

@ -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.")

View File

@ -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<String, String>): EmbedBuilder {
val builder = getEmbedBuilder()
for ((heading, content) in contents) {
builder.addField(heading, content)
builder.addField(heading.removePrefix("\"").removeSuffix("\""), content)
}
return builder
}

View File

@ -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)

View File

@ -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
}

View File

@ -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<String, String>?
)
class RawPermissions(val hasOneOf: List<String>?, val hasNoneOf: List<String>?, val onlyDM: Boolean)

View File

@ -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,

View File

@ -32,6 +32,24 @@ class CommandTest : StringSpec({
testMessageSuccess("!ping", "pong")
}
}
"should print embed for command" {
val calls = mutableListOf<EmbedBuilder>()
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(
"""