Skip to content

Commit 52833a9

Browse files
committed
Add coroutines support
1 parent 6fd945f commit 52833a9

File tree

13 files changed

+58
-33
lines changed

13 files changed

+58
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies {
2626
implementation("net.dv8tion:JDA:4.2.0_227") {
2727
exclude(module = "opus-java") // optional, for if you don't plan to use voice chat
2828
}
29-
implementation("org.yttr:koncorda:0.1.2")
29+
implementation("org.yttr:koncorda:0.2.0")
3030
}
3131
```
3232

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
group = "org.yttr"
4-
version = "0.1.2"
4+
version = "0.2.0"
55

66
plugins {
77
id("tanvd.kosogor") version "1.0.10" apply true
88
id("io.gitlab.arturbosch.detekt") version "1.15.0" apply true
9-
kotlin("jvm") version "1.4.21" apply false
9+
kotlin("jvm") version "1.4.30" apply false
1010
}
1111

1212
subprojects {

gradle.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# suppress inspection "UnusedProperty" for whole file
22
kotlin.code.style=official
3+
jdaVersion=4.2.0_227
4+
hoconVersion=1.4.1
5+
coroutinesVersion=1.4.2
6+
logbackVersion=1.2.3

koncorda/build.gradle.kts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ import tanvd.kosogor.proxy.publishJar
33
group = rootProject.group
44
version = rootProject.version
55

6+
val jdaVersion: String by project
7+
val hoconVersion: String by project
8+
val coroutinesVersion: String by project
9+
val logbackVersion: String by project
10+
611
dependencies {
7-
implementation("net.dv8tion:JDA:4.2.0_227") {
12+
implementation("net.dv8tion:JDA:$jdaVersion") {
813
exclude(module = "opus-java")
914
}
10-
api("com.typesafe:config:1.4.1")
11-
testImplementation("ch.qos.logback:logback-classic:1.2.3")
15+
api("com.typesafe:config:$hoconVersion")
16+
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
17+
api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutinesVersion")
18+
testImplementation("ch.qos.logback:logback-classic:$logbackVersion")
1219
}
1320

1421
publishJar {

koncorda/src/main/kotlin/Koncorda.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package org.yttr.koncorda
22

33
import com.typesafe.config.Config
44
import com.typesafe.config.ConfigFactory
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.SupervisorJob
8+
import kotlinx.coroutines.launch
59
import net.dv8tion.jda.api.JDABuilder
610
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
711
import net.dv8tion.jda.api.hooks.ListenerAdapter
@@ -15,6 +19,9 @@ class Koncorda : ListenerAdapter() {
1519
private val discordToken = conf.getString("koncorda.discord-token")
1620
private val baseCommands = mutableListOf<Command.Branch>()
1721

22+
private val job = SupervisorJob()
23+
private val scope = CoroutineScope(Dispatchers.Default + job)
24+
1825
/**
1926
* Explicitly listed gateways intents, defaults to JDA defaults
2027
*/
@@ -36,17 +43,20 @@ class Koncorda : ListenerAdapter() {
3643
// ignore improper messages
3744
if (event.isIgnorable) return
3845

39-
// split the args
40-
val args = event.message.contentStripped.trim().split(" ")
46+
scope.launch {
47+
// split the args
48+
val args = event.message.contentStripped.trim().split(" ")
4149

42-
// find the command handler, if it exists
43-
when (val baseCommand = allBaseCommands()[args.first()]) {
44-
is Command.Leaf -> baseCommand
45-
is Command.Branch -> baseCommand[args.drop(1)]
46-
else -> null
47-
}?.let {
48-
if (it.check(event)) it(event, args) else {
49-
event.channel.sendMessage("You are not allowed to use that command.").queue()
50+
// find the command handler, if it exists
51+
when (val baseCommand = allBaseCommands()[args.first()]) {
52+
is Command.Leaf -> baseCommand
53+
is Command.Branch -> baseCommand[args.drop(1)]
54+
else -> null
55+
}?.let {
56+
val call = it.createCall(event, args)
57+
if (it.check(call)) it.handle(call) else {
58+
event.channel.sendMessage("You are not allowed to use that command.").queue()
59+
}
5060
}
5161
}
5262
}

koncorda/src/main/kotlin/command/Command.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sealed class Command(
2424
private val fallback by lazy {
2525
if (handler != null) Leaf(depth, handler) else {
2626
val autoHelpCommandHandler = object : CommandHandler {
27-
override fun CommandCall.handle() {
27+
override suspend fun CommandCall.handle() {
2828
val path = args.joinToString(" ")
2929
val validSubcommands = routes.keys.joinToString { "`$it`" }
3030
event.respond("Valid subcommands for `$path` are $validSubcommands.")
@@ -62,8 +62,11 @@ sealed class Command(
6262
private val handler: CommandHandler,
6363
private val help: Boolean = false
6464
) : Command(depth) {
65-
operator fun invoke(event: MessageReceivedEvent, args: List<String>) = with(handler) {
66-
CommandCall(event, if (help) args.take(depth) else args.drop(depth)).handle()
65+
fun createCall(event: MessageReceivedEvent, args: List<String>) =
66+
CommandCall(event, if (help) args.take(depth) else args.drop(depth))
67+
68+
suspend fun handle(call: CommandCall) = with(handler) {
69+
call.handle()
6770
}
6871

6972
override fun addCheck(addedCheck: CommandCheck) {
@@ -91,9 +94,9 @@ sealed class Command(
9194
* Set the handler.
9295
*/
9396
@CreationDsl
94-
fun Branch.leaf(arg: String, handler: CommandCall.() -> Unit) {
97+
fun Branch.leaf(arg: String, handler: suspend CommandCall.() -> Unit) {
9598
routes["$prefix$arg"] = Leaf(nextDepth, object : CommandHandler {
96-
override fun CommandCall.handle() = handler()
99+
override suspend fun CommandCall.handle() = handler()
97100
})
98101
}
99102

@@ -108,5 +111,5 @@ sealed class Command(
108111

109112
abstract fun addCheck(addedCheck: CommandCheck)
110113

111-
fun check(event: MessageReceivedEvent) = checks.all { it.check(event) }
114+
suspend fun check(call: CommandCall) = checks.all { it.check(call) }
112115
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.yttr.koncorda.command
22

3+
import kotlinx.coroutines.future.await
34
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
45

56
data class CommandCall(val event: MessageReceivedEvent, val args: List<String>) {
67
/**
78
* Respond to the event.
89
*/
9-
fun MessageReceivedEvent.respond(content: String) {
10-
if (content.isNotBlank()) channel.sendMessage(content).queue()
10+
suspend fun MessageReceivedEvent.respond(content: String) {
11+
if (content.isNotBlank()) channel.sendMessage(content).submit().await()
1112
}
1213
}

koncorda/src/main/kotlin/command/CommandHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ interface CommandHandler {
77
/**
88
* Handle the message event for a command invocation.
99
*/
10-
fun CommandCall.handle()
10+
suspend fun CommandCall.handle()
1111
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.yttr.koncorda.command.check
22

3-
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
3+
import org.yttr.koncorda.command.CommandCall
44

55
interface CommandCheck {
6-
fun check(event: MessageReceivedEvent): Boolean
6+
suspend fun check(call: CommandCall): Boolean
77
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.yttr.koncorda.command.check
22

3-
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
3+
import org.yttr.koncorda.command.CommandCall
44

55
object Impossible : CommandCheck {
6-
override fun check(event: MessageReceivedEvent): Boolean = false
6+
override suspend fun check(call: CommandCall): Boolean = false
77
}

0 commit comments

Comments
 (0)