Skip to content

Commit

Permalink
Merge branch 'hocon'
Browse files Browse the repository at this point in the history
  • Loading branch information
yttrian committed Feb 2, 2021
2 parents 4653790 + 61a791e commit 6fd945f
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 66 deletions.
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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")
}
```

Expand All @@ -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()
}
Expand All @@ -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).
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion koncorda/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
36 changes: 25 additions & 11 deletions koncorda/src/main/kotlin/Koncorda.kt
Original file line number Diff line number Diff line change
@@ -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<Command.Branch>()

/**
* 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)

Expand Down Expand Up @@ -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)
3 changes: 0 additions & 3 deletions koncorda/src/main/kotlin/config/DefaultKoncordaConfig.kt

This file was deleted.

31 changes: 0 additions & 31 deletions koncorda/src/main/kotlin/config/KoncordaConfig.kt

This file was deleted.

7 changes: 7 additions & 0 deletions koncorda/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -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}
}
5 changes: 4 additions & 1 deletion koncorda/src/test/kotlin/TestBot.kt
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down
7 changes: 0 additions & 7 deletions koncorda/src/test/kotlin/TestBotConfig.kt

This file was deleted.

3 changes: 3 additions & 0 deletions koncorda/src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
koncorda {
command-prefix = "/"
}

0 comments on commit 6fd945f

Please sign in to comment.