diff --git a/README.md b/README.md index 79b3257..15d995a 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,20 @@ # Koncorda +![Bintray](https://img.shields.io/bintray/v/yttrian/koncorda/koncorda?label=koncorda) +![Bintray (latest)](https://img.shields.io/bintray/dt/yttrian/koncorda/koncorda) + A small DSL for quickly creating a command bot with [JDA](https://github.com/DV8FromTheWorld/JDA). -Inspired by [Ktor](https://github.com/ktorio/ktor). +Inspired by the simplicity [Ktor](https://github.com/ktorio/ktor). # Installation -Add the JCenter repository, the latest Koncorda release, JDA, and an SLF4J implemtnation to your `build.gradle.kts`. +Add the JCenter repository, the latest Koncorda release, JDA, and an SLF4J implementation to your `build.gradle.kts`. While Koncorda is intended to be somewhat opinionated, you are free to chose your SLF4J implementation. Logback is recommended for the sole reason that it's what the Ktor project generator suggests. JDA is not automatically included with Koncorda. This allows you to update to newer *non-breaking* versions of it -without needing Koncorda to be updated as well. +without needing Koncorda to be updated as well. As well as being able to chose to exclude "opus-java" if you want to. ```kotlin repositories { @@ -23,7 +26,7 @@ dependencies { implementation("net.dv8tion:JDA:4.2.0_227") { exclude(module = "opus-java") // optional, for if you don't plan to use voice chat } - implementation("org.yttr:koncorda:0.1.1") + implementation("org.yttr:koncorda:0.1.2") } ``` @@ -35,9 +38,7 @@ Using Koncorda is easy, especially with the `koncorda` entrypoint. fun main() { koncorda { commmands { - tail("hello") { - event.respond("Hello world!") - } + tail("hello") { event.respond("Hello world!") } } }.start() } @@ -46,8 +47,22 @@ fun main() { DSLs like `command` make adding new functionality as easy as possible. The above example creates a bot that responds "Hello world!" to `!hello`. -Extending the configuration is possible by implementing KoncordaConfig. By default, it defines basic needs for getting -a bot running like `DISCORD_TOKEN` and `COMMAND_PREFIX` which by default come from the environment. For more info, check -out the [konfy](https://github.com/TanVD/konfy) project. +Extending the configuration is possible by creating an `application.conf` HOCON file. By default, Koncorda uses what +is defined in [reference.conf](koncorda/src/main/resources/reference.conf), but this can be overridden. + +```hocon +koncorda { + // The Discord bot token, comes from the environmental variable DISCORD_TOKEN + discord-token = ${DISCORD_TOKEN} + // The command prefix, defaults to ! or the environmental variable COMMAND_PREFIX + command-prefix = "!" + command-prefix = ${?COMMAND_PREFIX} +} + +your-bot { + some-option = "some value" +} +``` -To see what a more complex usage looks like, check out the TestBot under the tests source. +To see what a more complex usage of Koncorda looks like, check out the TestBot under the +[tests source](koncorda/src/test). diff --git a/build.gradle.kts b/build.gradle.kts index e843141..15e6dd3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile group = "org.yttr" -version = "0.1.1" +version = "0.1.2" plugins { id("tanvd.kosogor") version "1.0.10" apply true diff --git a/koncorda/build.gradle.kts b/koncorda/build.gradle.kts index f73bf4c..34bc55e 100644 --- a/koncorda/build.gradle.kts +++ b/koncorda/build.gradle.kts @@ -7,7 +7,7 @@ dependencies { implementation("net.dv8tion:JDA:4.2.0_227") { exclude(module = "opus-java") } - api("tanvd.konfy:konfy:0.1.18") + api("com.typesafe:config:1.4.1") testImplementation("ch.qos.logback:logback-classic:1.2.3") } diff --git a/koncorda/src/main/kotlin/Koncorda.kt b/koncorda/src/main/kotlin/Koncorda.kt index d5eb13d..1cb9576 100644 --- a/koncorda/src/main/kotlin/Koncorda.kt +++ b/koncorda/src/main/kotlin/Koncorda.kt @@ -1,28 +1,34 @@ package org.yttr.koncorda +import com.typesafe.config.Config +import com.typesafe.config.ConfigFactory import net.dv8tion.jda.api.JDABuilder import net.dv8tion.jda.api.events.message.MessageReceivedEvent import net.dv8tion.jda.api.hooks.ListenerAdapter +import net.dv8tion.jda.api.requests.GatewayIntent import org.yttr.koncorda.command.Command -import org.yttr.koncorda.config.DefaultKoncordaConfig -import org.yttr.koncorda.config.KoncordaConfig -import tanvd.konfy.ConfigView /** * The Discord bot application, with useful DSLs for setting up different features. */ -class Koncorda(val config: KoncordaConfig) : ConfigView, ListenerAdapter() { +class Koncorda : ListenerAdapter() { + private val discordToken = conf.getString("koncorda.discord-token") private val baseCommands = mutableListOf() + /** + * Explicitly listed gateways intents, defaults to JDA defaults + */ + var gatewayIntents = GatewayIntent.getIntents(GatewayIntent.DEFAULT).toList() + /** * Complete the JDA builder and start the bot * @param lowMemory Whether or not to use JDA's low memory profile */ fun start(lowMemory: Boolean = false) = if (lowMemory) { - JDABuilder.createLight(config.discordToken) + JDABuilder.createLight(discordToken) } else { - JDABuilder.createDefault(config.discordToken) - }.addEventListeners(this).setEnabledIntents(config.gatewayIntents).build() + JDABuilder.createDefault(discordToken) + }.addEventListeners(this).setEnabledIntents(gatewayIntents).build() internal fun addBaseCommand(command: Command.Branch) = baseCommands.add(command) @@ -53,16 +59,24 @@ class Koncorda(val config: KoncordaConfig) : ConfigView, ListenerAdapter() { private val MessageReceivedEvent.isIgnorable get() = author.isBot || isWebhookMessage + + companion object { + /** + * Access to configuration defined in application.conf HOCON + */ + val conf: Config = ConfigFactory.load() + } } /** * Commands DSL, an easy way of registering command handlers and the path to them with Koncorda. */ -fun Koncorda.commands(prefix: String = config.commandPrefix, build: Command.Branch.() -> Unit) = - addBaseCommand(Command.Branch(prefix = prefix).apply(build)) +fun Koncorda.commands( + prefix: String = Koncorda.conf.getString("koncorda.command-prefix"), + build: Command.Branch.() -> Unit +) = addBaseCommand(Command.Branch(prefix = prefix).apply(build)) /** * The simplest way to start building a bot with Koncorda. Make sure to add .start() to the end! */ -fun koncorda(config: KoncordaConfig = DefaultKoncordaConfig, init: Koncorda.() -> Unit) = - Koncorda(config).apply(init) +fun koncorda(init: Koncorda.() -> Unit) = Koncorda().apply(init) diff --git a/koncorda/src/main/kotlin/config/DefaultKoncordaConfig.kt b/koncorda/src/main/kotlin/config/DefaultKoncordaConfig.kt deleted file mode 100644 index 670c2ef..0000000 --- a/koncorda/src/main/kotlin/config/DefaultKoncordaConfig.kt +++ /dev/null @@ -1,3 +0,0 @@ -package org.yttr.koncorda.config - -object DefaultKoncordaConfig : KoncordaConfig() diff --git a/koncorda/src/main/kotlin/config/KoncordaConfig.kt b/koncorda/src/main/kotlin/config/KoncordaConfig.kt deleted file mode 100644 index 224996a..0000000 --- a/koncorda/src/main/kotlin/config/KoncordaConfig.kt +++ /dev/null @@ -1,31 +0,0 @@ -package org.yttr.koncorda.config - -import net.dv8tion.jda.api.requests.GatewayIntent -import tanvd.konfy.ConfigChain -import tanvd.konfy.ConfigView -import tanvd.konfy.provided -import tanvd.konfy.provider.ConfigProvider -import tanvd.konfy.provider.EnvVarProvider - -/** - * Defines all needed configuration properties - */ -abstract class KoncordaConfig(private val chain: ConfigChain = ConfigChain(EnvVarProvider())) : ConfigView { - override val provider: ConfigProvider - get() = chain - - /** - * The Discord bot token - */ - val discordToken: String by provided("DISCORD_TOKEN") - - /** - * Explicitly listed gateways intents, defaults to JDA defaults - */ - open val gatewayIntents: List = GatewayIntent.getIntents(GatewayIntent.DEFAULT).toList() - - /** - * The prefix for commands like !help if ! - */ - open val commandPrefix: String by provided("COMMAND_PREFIX", "!") -} diff --git a/koncorda/src/main/resources/reference.conf b/koncorda/src/main/resources/reference.conf new file mode 100644 index 0000000..68cfabf --- /dev/null +++ b/koncorda/src/main/resources/reference.conf @@ -0,0 +1,7 @@ +koncorda { + // The Discord bot token, comes from the environmental variable DISCORD_TOKEN + discord-token = ${DISCORD_TOKEN} + // The command prefix, defaults to ! or the environmental variable COMMAND_PREFIX + command-prefix = "!" + command-prefix = ${?COMMAND_PREFIX} +} diff --git a/koncorda/src/test/kotlin/TestBot.kt b/koncorda/src/test/kotlin/TestBot.kt index 0adcec0..fe1191e 100644 --- a/koncorda/src/test/kotlin/TestBot.kt +++ b/koncorda/src/test/kotlin/TestBot.kt @@ -1,11 +1,14 @@ import command.HelpCommand import command.TestCommand +import net.dv8tion.jda.api.requests.GatewayIntent import org.yttr.koncorda.command.check.Impossible import org.yttr.koncorda.commands import org.yttr.koncorda.koncorda fun main() { - koncorda(TestBotConfig) { + koncorda { + gatewayIntents = listOf(GatewayIntent.GUILD_MESSAGES) + commands { leaf("help", HelpCommand) branch("test") { diff --git a/koncorda/src/test/kotlin/TestBotConfig.kt b/koncorda/src/test/kotlin/TestBotConfig.kt deleted file mode 100644 index 4b77a08..0000000 --- a/koncorda/src/test/kotlin/TestBotConfig.kt +++ /dev/null @@ -1,7 +0,0 @@ -import net.dv8tion.jda.api.requests.GatewayIntent -import org.yttr.koncorda.config.KoncordaConfig - -object TestBotConfig : KoncordaConfig() { - override val gatewayIntents: List - get() = listOf(GatewayIntent.GUILD_MESSAGES) -} diff --git a/koncorda/src/test/resources/application.conf b/koncorda/src/test/resources/application.conf new file mode 100644 index 0000000..66b35c0 --- /dev/null +++ b/koncorda/src/test/resources/application.conf @@ -0,0 +1,3 @@ +koncorda { + command-prefix = "/" +}