use json annotations to make config more intuitive

This commit is contained in:
kageru 2019-06-10 13:03:18 +02:00
parent f2ece85d56
commit 41a1e62574
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
5 changed files with 29 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package moe.kageru.kagebot
import com.google.gson.annotations.SerializedName
import moe.kageru.kagebot.Config.Companion.config
import moe.kageru.kagebot.Util.doIf
import org.javacord.api.entity.message.MessageAuthor
@ -12,7 +13,7 @@ class Command(
private val response: String?,
matchType: MatchType?,
private val permissions: Permissions?,
private val actions: MessageActions?
@SerializedName("action") private val actions: MessageActions?
) {
val trigger: String = trigger!!
val regex: Regex? = if (matchType == MatchType.REGEX) Regex(trigger!!) else null

View File

@ -1,11 +1,16 @@
package moe.kageru.kagebot
import com.google.gson.annotations.SerializedName
import com.moandjiezana.toml.Toml
import org.javacord.api.DiscordApi
import org.javacord.api.entity.server.Server
import java.io.File
class Config(val system: System, val localization: Localization, val commands: List<Command>) {
class Config(
val system: System,
val localization: Localization,
@SerializedName("command") val commands: List<Command>
) {
companion object {
val config: Config by lazy { read("config.toml") }
val secret = File("secret").readText().replace("\n", "")

View File

@ -1,19 +1,15 @@
package moe.kageru.kagebot
import moe.kageru.kagebot.Config.Companion.config
import org.javacord.api.entity.message.MessageAuthor
import org.javacord.api.entity.message.embed.EmbedBuilder
import org.javacord.api.event.message.MessageCreateEvent
import java.awt.Color
import moe.kageru.kagebot.Config.Companion.config
object MessageUtil {
fun mention(user: MessageAuthor): String {
return "<@${user.id}>"
}
fun MessageCreateEvent.asString(): String =
"<${this.messageAuthor.discriminatedName}> ${this.readableMessageContent}"
fun getEmbedBuilder(): EmbedBuilder {
val builder = EmbedBuilder()
Config.server!!.icon.ifPresent { builder.setThumbnail(it) }

View File

@ -7,57 +7,59 @@ permissionDenied = "You do not have permission to use this command."
# results in <name> says <message>
redirectedMessage = "says"
[[commands]]
[[command]]
trigger = "!ping"
response = "pong"
[[commands]]
[[command]]
trigger = "somewhere"
response = "found it"
matchType = "CONTAINS"
[[commands]]
[[command]]
trigger = "A.+B"
response = "regex matched"
matchType = "REGEX"
[[commands]]
[[command]]
trigger = "answer me"
# this will @mention the user who triggered the command,
# i.e. “@author there you go”
response = "@@ there you go"
[[commands]]
[[command]]
trigger = "delet this"
actions = { delete = true }
[command.action]
delete = true
[[commands]]
[[command]]
trigger = "!restricted"
response = "access granted"
[commands.permissions]
[command.permissions]
hasOneOf = [
452034011393425409,
446668543816106004
]
[[commands]]
[[command]]
trigger = "!almostUnrestricted"
response = "access granted"
[commands.permissions]
hasNoneOf = [
452034011393425409
]
[command.permissions]
hasNoneOf = [452034011393425409]
# redirect every message that starts with !redirect to channel 555097559023222825
[[commands]]
[[command]]
trigger = "!redirect"
response = "redirected"
actions = { redirect = { target = 555097559023222825 } }
[command.action.redirect]
target = 555097559023222825
# the same, but without the original username
[[commands]]
[[command]]
trigger = "!anonRedirect"
response = "redirected"
actions = { redirect = { target = 555097559023222825, anonymous = true } }
[command.action.redirect]
target = 555097559023222825
anonymous = true

View File

@ -6,5 +6,6 @@ import io.kotlintest.specs.StringSpec
class ConfigTest : StringSpec({
"should properly parse default config" {
Config.config shouldNotBe null
Config.config.commands shouldNotBe null
}
})