Add configurable embeds as command outputs
This commit is contained in:
parent
85206a7fc3
commit
b08deb6e8d
|
@ -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)
|
||||
}
|
|
@ -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.")
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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(
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user