Skip to content

Commit 795e7ac

Browse files
committed
feat: start coroutines
1 parent 1482598 commit 795e7ac

24 files changed

+485
-558
lines changed

build.gradle.kts

+16-12
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ repositories {
1919
}
2020

2121
dependencies {
22-
api(libs.net.dv8tion.jda) {
22+
api(libs.jda) {
2323
exclude(module="opus-java")
2424
}
25-
api(libs.org.spongepowered.configurate.yaml)
26-
api(libs.org.mongodb.mongodb.driver.reactivestreams)
27-
api(libs.io.projectreactor.reactor.core)
28-
api(libs.de.huxhorn.sulky.de.huxhorn.sulky.ulid)
29-
api(libs.net.xyzsd.plurals.cldr.plural.rules)
30-
api(libs.org.yaml.snakeyaml)
31-
api(libs.org.reflections.reflections)
32-
api(libs.org.slf4j.slf4j.api)
33-
testImplementation(libs.org.junit.jupiter.junit.jupiter)
34-
testImplementation(libs.ch.qos.logback.logback.classic)
35-
compileOnly(libs.org.jetbrains.annotations)
25+
api(libs.jda.ktx)
26+
api(libs.configurate.yaml)
27+
api(libs.mongodb.coroutines)
28+
api(libs.mongodb.kotlinx)
29+
api(libs.reactor.core)
30+
api(libs.ulid)
31+
api(libs.cldr.plural.rules)
32+
api(libs.snakeyaml)
33+
api(libs.slf4j)
34+
api(libs.kotlinx.coroutines.core)
35+
api(libs.kotlinx.coroutines.reactor)
36+
testImplementation(libs.junit.jupiter)
37+
testImplementation(libs.logback)
38+
api(libs.jetbrains.annotations)
3639
implementation(kotlin("stdlib-jdk8"))
40+
implementation(kotlin("reflect"))
3741
}
3842

3943
group = "dev.qixils.quasicolon"

gradle/libs.versions.toml

+29-24
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
33

44
[versions]
5-
ch-qos-logback-logback-classic = "1.5.6"
6-
de-huxhorn-sulky-de-huxhorn-sulky-ulid = "8.3.0"
7-
io-projectreactor-reactor-core = "3.6.7"
8-
net-dv8tion-jda = "5.2.2"
9-
net-xyzsd-plurals-cldr-plural-rules = "41"
10-
org-jetbrains-annotations = "24.1.0"
11-
org-junit-jupiter-junit-jupiter = "5.11.0-M2"
12-
org-mongodb-mongodb-driver-reactivestreams = "5.1.1"
13-
org-reflections-reflections = "0.10.2"
14-
org-slf4j-slf4j-api = "2.0.13"
15-
org-spongepowered-configurate-yaml = "4.1.2"
16-
org-yaml-snakeyaml = "2.2"
5+
logback = "1.5.13"
6+
ulid = "8.3.0"
7+
reactor-core = "3.6.7"
8+
jda = "5.2.2"
9+
jda-ktx = "0.12.0"
10+
cldr-plural-rules = "41"
11+
jetbrains-annotations = "24.1.0"
12+
junit-jupiter = "5.11.0-M2"
13+
mongodb-coroutines = "5.2.1"
14+
slf4j = "2.0.13"
15+
configurate-yaml = "4.1.2"
16+
snakeyaml = "2.2"
17+
kotlinx-coroutines = "1.10.1"
18+
kotlinx-coroutines-reactor = "1.10.1"
1719

1820
[libraries]
19-
ch-qos-logback-logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "ch-qos-logback-logback-classic" }
20-
de-huxhorn-sulky-de-huxhorn-sulky-ulid = { module = "de.huxhorn.sulky:de.huxhorn.sulky.ulid", version.ref = "de-huxhorn-sulky-de-huxhorn-sulky-ulid" }
21-
io-projectreactor-reactor-core = { module = "io.projectreactor:reactor-core", version.ref = "io-projectreactor-reactor-core" }
22-
net-dv8tion-jda = { module = "net.dv8tion:JDA", version.ref = "net-dv8tion-jda" }
23-
net-xyzsd-plurals-cldr-plural-rules = { module = "net.xyzsd.plurals:cldr-plural-rules", version.ref = "net-xyzsd-plurals-cldr-plural-rules" }
24-
org-jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "org-jetbrains-annotations" }
25-
org-junit-jupiter-junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "org-junit-jupiter-junit-jupiter" }
26-
org-mongodb-mongodb-driver-reactivestreams = { module = "org.mongodb:mongodb-driver-reactivestreams", version.ref = "org-mongodb-mongodb-driver-reactivestreams" }
27-
org-reflections-reflections = { module = "org.reflections:reflections", version.ref = "org-reflections-reflections" }
28-
org-slf4j-slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "org-slf4j-slf4j-api" }
29-
org-spongepowered-configurate-yaml = { module = "org.spongepowered:configurate-yaml", version.ref = "org-spongepowered-configurate-yaml" }
30-
org-yaml-snakeyaml = { module = "org.yaml:snakeyaml", version.ref = "org-yaml-snakeyaml" }
21+
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
22+
ulid = { module = "de.huxhorn.sulky:de.huxhorn.sulky.ulid", version.ref = "ulid" }
23+
reactor-core = { module = "io.projectreactor:reactor-core", version.ref = "reactor-core" }
24+
jda = { module = "net.dv8tion:JDA", version.ref = "jda" }
25+
jda-ktx = { module = "club.minnced:jda-ktx", version.ref = "jda-ktx" }
26+
cldr-plural-rules = { module = "net.xyzsd.plurals:cldr-plural-rules", version.ref = "cldr-plural-rules" }
27+
jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "jetbrains-annotations" }
28+
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
29+
mongodb-coroutines = { module = "org.mongodb:mongodb-driver-kotlin-coroutine", version.ref = "mongodb-coroutines" }
30+
mongodb-kotlinx = { module = "org.mongodb:bson-kotlinx", version.ref = "mongodb-coroutines" }
31+
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
32+
configurate-yaml = { module = "org.spongepowered:configurate-yaml", version.ref = "configurate-yaml" }
33+
snakeyaml = { module = "org.yaml:snakeyaml", version.ref = "snakeyaml" }
34+
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
35+
kotlinx-coroutines-reactor = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactor", version.ref = "kotlinx-coroutines" }

src/main/java/dev/qixils/quasicord/CommandManager.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package dev.qixils.quasicord
77

8+
import dev.minn.jda.ktx.coroutines.await
9+
import dev.minn.jda.ktx.messages.reply_
810
import dev.qixils.quasicord.cogs.Command
911
import dev.qixils.quasicord.cogs.SlashCommand
1012
import dev.qixils.quasicord.decorators.AnnotationParser
@@ -104,7 +106,7 @@ class CommandManager(library: Quasicord) {
104106
}
105107

106108
@SubscribeEvent
107-
fun onCommandInteraction(event: GenericCommandInteractionEvent) {
109+
suspend fun onCommandInteraction(event: GenericCommandInteractionEvent) {
108110
val guildId = if (event.guild == null) event.guild!!.id else null
109111
val command = getCommand(event.fullCommandName, guildId)
110112
if (command == null) {
@@ -129,12 +131,11 @@ class CommandManager(library: Quasicord) {
129131
}
130132

131133
companion object {
132-
private fun sendEphemeral(event: IReplyCallback, text: Text) {
133-
text.asString(Context.fromInteraction(event)).subscribe { string: String? ->
134-
event.reply(
135-
string!!
136-
).setEphemeral(true).queue()
137-
}
134+
private suspend fun sendEphemeral(event: IReplyCallback, text: Text) {
135+
event.reply_(
136+
text.asString(Context.fromInteraction(event)),
137+
ephemeral = true,
138+
).await()
138139
}
139140
}
140141
}

src/main/java/dev/qixils/quasicord/Quasicord.kt

+29-33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package dev.qixils.quasicord
77

8+
import dev.minn.jda.ktx.events.listener
9+
import dev.minn.jda.ktx.jdabuilder.default
810
import dev.qixils.quasicord.commands.GuildConfigCommand
911
import dev.qixils.quasicord.commands.UserConfigCommand
1012
import dev.qixils.quasicord.db.DatabaseManager
@@ -13,11 +15,9 @@ import dev.qixils.quasicord.locale.LocaleProvider
1315
import dev.qixils.quasicord.locale.TranslationProvider
1416
import dev.qixils.quasicord.registry.core.RegistryRegistry
1517
import net.dv8tion.jda.api.JDA
16-
import net.dv8tion.jda.api.JDABuilder
1718
import net.dv8tion.jda.api.entities.Activity
1819
import net.dv8tion.jda.api.events.Event
1920
import net.dv8tion.jda.api.hooks.AnnotatedEventManager
20-
import net.dv8tion.jda.api.hooks.SubscribeEvent
2121
import net.dv8tion.jda.api.requests.GatewayIntent
2222
import net.dv8tion.jda.api.utils.MemberCachePolicy
2323
import net.dv8tion.jda.api.utils.cache.CacheFlag
@@ -147,7 +147,7 @@ open class Quasicord(
147147

148148
// load database and locale provider
149149
databaseManager = DatabaseManager(namespace, config.environment)
150-
localeProvider = LocaleProvider(defaultLocale, databaseManager)
150+
localeProvider = LocaleProvider.create(defaultLocale, databaseManager)
151151
LocaleProvider.instance = localeProvider
152152

153153
// initialize JDA and relevant data
@@ -164,39 +164,35 @@ open class Quasicord(
164164
}
165165

166166
protected open fun initJDA(activity: Activity?): JDA {
167-
val builder = JDABuilder.createDefault(config.token)
168-
.disableIntents(
169-
GatewayIntent.DIRECT_MESSAGE_TYPING,
170-
GatewayIntent.GUILD_MESSAGE_TYPING, // GatewayIntent.GUILD_INTEGRATIONS, // unused, apparently
171-
GatewayIntent.GUILD_WEBHOOKS,
172-
GatewayIntent.GUILD_INVITES,
173-
GatewayIntent.GUILD_VOICE_STATES
174-
)
175-
.enableIntents(GatewayIntent.GUILD_MEMBERS) // TODO:
176-
// 1. register cogs before initJDA() is called
177-
// 2. implement getRequiredIntents() in Cog
178-
// 3. use that result here to compute minimum required intents
179-
// 4. late-loaded cogs, if any/ever, can decline when their required intents weren't met at startup
180-
// 5. except wait, cogs need jda to be constructed (and probably should), that's a problem
181-
.enableIntents(GatewayIntent.GUILD_MESSAGES)
182-
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
183-
.setMemberCachePolicy(MemberCachePolicy.ALL)
184-
.setEventManager(AnnotatedEventManager())
185-
.disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE)
186-
if (activity != null) builder.setActivity(activity)
187-
MessageRequest.setDefaultMentions(emptySet())
188-
val jda = builder.build()
167+
val jda = default(config.token, enableCoroutines = true) {
168+
disableIntents(
169+
GatewayIntent.DIRECT_MESSAGE_TYPING,
170+
GatewayIntent.GUILD_MESSAGE_TYPING, // GatewayIntent.GUILD_INTEGRATIONS, // unused, apparently
171+
GatewayIntent.GUILD_WEBHOOKS,
172+
GatewayIntent.GUILD_INVITES,
173+
GatewayIntent.GUILD_VOICE_STATES
174+
)
175+
// TODO:
176+
// 1. register cogs before initJDA() is called
177+
// 2. implement getRequiredIntents() in Cog
178+
// 3. use that result here to compute minimum required intents
179+
// 4. late-loaded cogs, if any/ever, can decline when their required intents weren't met at startup
180+
// 5. except wait, cogs need jda to be constructed (and probably should), that's a problem
181+
enableIntents(GatewayIntent.GUILD_MEMBERS)
182+
enableIntents(GatewayIntent.GUILD_MESSAGES)
183+
enableIntents(GatewayIntent.MESSAGE_CONTENT)
184+
setMemberCachePolicy(MemberCachePolicy.ALL)
185+
setEventManager(AnnotatedEventManager())
186+
disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE)
187+
if (activity != null) setActivity(activity)
188+
MessageRequest.setDefaultMentions(emptySet())
189+
}
189190
jda.setRequiredScopes("applications.commands")
190191
jda.addEventListener(tempListenerExecutor)
191-
jda.addEventListener(object : Any() {
192-
@SubscribeEvent
193-
fun on(event: Event?) {
194-
eventDispatcher.dispatch(event!!)
195-
}
196-
})
192+
jda.listener<Event> { eventDispatcher.dispatch(it) }
197193
try {
198194
jda.awaitReady()
199-
} catch (ignored: InterruptedException) {
195+
} catch (_: InterruptedException) {
200196
}
201197
return jda
202198
}
@@ -208,7 +204,7 @@ open class Quasicord(
208204
*/
209205
// we don't expose the raw executor in a getter because objects could abuse the #onEvent method
210206
fun register(listener: TemporaryListener<*>) {
211-
tempListenerExecutor.register(Objects.requireNonNull(listener, "listener cannot be null"))
207+
tempListenerExecutor.register(listener)
212208
}
213209

214210
/**

0 commit comments

Comments
 (0)