Use lists for embed content configuration (fixes #12)

This commit is contained in:
kageru 2019-07-12 14:08:22 +02:00
parent 65a4ac5bed
commit dc14e3ee1e
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
9 changed files with 41 additions and 35 deletions

View File

@ -19,11 +19,20 @@ object MessageUtil {
return builder.setColor(Globals.systemConfig.color).setTimestampToNow()
}
fun mapToEmbed(contents: Map<String, String>): EmbedBuilder {
/*
* The reason we use a list here (rather than a map) is that maps would not retain the order specified in the config.
* I tried LinkedHashMaps, but those dont seem to work either.
*/
fun listToEmbed(contents: List<String>): EmbedBuilder {
if (contents.size % 2 == 1) {
throw IllegalStateException("Embed must have even number of content strings (title/content pairs)")
}
val builder = getEmbedBuilder()
for ((heading, content) in contents) {
builder.addField(heading.removePrefix("\"").removeSuffix("\""), content)
contents.zip(1..contents.size).filter { it.second % 2 == 0 }
for ((heading, content) in contents.withIndex().filter { it.index % 2 == 0 }
zip contents.withIndex().filter { it.index % 2 == 1 }) {
builder.addField(heading.value, content.value)
}
return builder
}
}
}

View File

@ -33,7 +33,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)
embed = cmd.embed?.let(MessageUtil::listToEmbed)
feature = cmd.feature?.let { Globals.features.findByString(it) }
}

View File

@ -8,7 +8,7 @@ class RawCommand(
val matchType: String?,
val permissions: RawPermissions?,
@SerializedName("action") val actions: RawMessageActions?,
val embed: Map<String, String>?,
val embed: List<String>?,
val feature: String?
)

View File

@ -1,4 +1,4 @@
package moe.kageru.kagebot.config
class RawFeatures(val welcome: RawWelcomeFeature?)
class RawWelcomeFeature(val content: Map<String, String>?, val fallbackChannel: String?, val fallbackMessage: String?)
class RawWelcomeFeature(val content: List<String>?, val fallbackChannel: String?, val fallbackMessage: String?)

View File

@ -20,24 +20,16 @@ class DebugFeature : MessageFeature() {
private fun getPerformanceStats(): EmbedBuilder {
val osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean::class.java)
val runtime = Runtime.getRuntime()
return MessageUtil.mapToEmbed(
mapOf(
Pair(
"Bot:",
getBotStats()
),
Pair(
"Memory:",
getMemoryInfo(runtime, osBean)
),
Pair(
"CPU:",
getCpuInfo(osBean)
),
Pair(
"System:",
getOsInfo()
)
return MessageUtil.listToEmbed(
listOf(
"Bot:",
getBotStats(),
"Memory:",
getMemoryInfo(runtime, osBean),
"CPU:",
getCpuInfo(osBean),
"System:",
getOsInfo()
)
)
}

View File

@ -13,7 +13,7 @@ class WelcomeFeature(rawWelcome: RawWelcomeFeature) : MessageFeature() {
}
val embed: EmbedBuilder? by lazy {
rawWelcome.content?.let(MessageUtil::mapToEmbed)
rawWelcome.content?.let(MessageUtil::listToEmbed)
}
val fallbackChannel: TextChannel? = rawWelcome.fallbackChannel?.let {
if (rawWelcome.fallbackMessage == null) {

View File

@ -15,11 +15,15 @@ messageDeleted = "Your message was deleted because it contained a banned word or
[feature.welcome]
fallbackChannel = "555097559023222825"
fallbackMessage = "@@ I would like to greet you, but I can’t. :("
# This is a list of pairs where the key is the title and the value the content of the paragraph.
# This is a list of strings like [title1, content1, title2, content2, ...]
# Do not use empty strings to get empty headings or paragraphs. The discord API rejects those.
[feature.welcome.content]
"Welcome to the Server" = "This is the content of the first paragraph"
"Second paragraph heading" = "Second paragraph content"
content = [
"Welcome to the Server" , "This is the content of the first paragraph",
"Second paragraph heading", "Second paragraph content",
"3rd", "aoisd",
"fourth", "asasd",
"5th", "asdasd"
]
[[command]]
trigger = "!ping"
@ -37,7 +41,7 @@ matchType = "REGEX"
[[command]]
trigger = "!embed"
embed = { "some embed heading" = "your embed content" }
embed = [ "some embed heading", "your embed content" ]
[[command]]
trigger = "answer me"

View File

@ -44,7 +44,7 @@ class CommandTest : StringSpec({
"""
[[command]]
trigger = "!embed"
embed = { "$heading" = "$content" }
embed = [ "$heading", "$content" ]
""".trimIndent()
) {
TestUtil.withReplyContents(expected = listOf(heading, content)) {

View File

@ -12,9 +12,10 @@ fallbackChannel = "123"
fallbackMessage = "@@ welcome"
# This is a list of pairs where the key is the title and the value the content of the paragraph.
# Do not use empty strings to get empty headings or paragraphs. The discord API rejects those.
[feature.welcome.content]
"Welcome to the Server" = "This is the content of the first paragraph"
"Second paragraph heading" = "Second paragraph content"
content = [
"Welcome to the Server", "This is the content of the first paragraph",
"Second paragraph heading", "Second paragraph content"
]
[[command]]
trigger = "!debug"