Skip to content

Commit

Permalink
feat: annotated coroutine event manager
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils committed Jan 2, 2025
1 parent b395d74 commit b5e5f30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.qixils.quasicord

import dev.minn.jda.ktx.events.CoroutineEventListener
import dev.minn.jda.ktx.events.CoroutineEventManager
import dev.minn.jda.ktx.events.getDefaultScope
import kotlinx.coroutines.CoroutineScope
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.hooks.EventListener
import net.dv8tion.jda.api.hooks.SubscribeEvent
import kotlin.reflect.full.callSuspend
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.hasAnnotation
import kotlin.time.Duration

class AnnotatedCoroutineEventManager(
scope: CoroutineScope = getDefaultScope(),
/** Timeout [Duration] each event listener is allowed to run. Set to [Duration.INFINITE] for no timeout. Default: [Duration.INFINITE] */
timeout: Duration = Duration.INFINITE,
) : CoroutineEventManager(scope, timeout) {
override fun register(listener: Any) {
when (listener) {
is EventListener, is CoroutineEventListener -> listeners.add(listener)
else -> listeners.addAll(discover(listener))
}
}

fun discover(listener: Any): List<CoroutineEventListener> = listener::class
.declaredFunctions
.filter {
it.hasAnnotation<SubscribeEvent>()
&& it.parameters.size == 1
&& GenericEvent::class.java.isAssignableFrom(it.parameters[0].javaClass)
}
.map {
val inputClass = it.parameters[0].javaClass
CoroutineEventListener { event ->
if (inputClass.isAssignableFrom(event.javaClass))
it.callSuspend(event)
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/dev/qixils/quasicord/Quasicord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import dev.qixils.quasicord.registry.core.RegistryRegistry
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.events.Event
import net.dv8tion.jda.api.hooks.AnnotatedEventManager
import net.dv8tion.jda.api.requests.GatewayIntent
import net.dv8tion.jda.api.utils.MemberCachePolicy
import net.dv8tion.jda.api.utils.cache.CacheFlag
Expand Down Expand Up @@ -164,7 +163,7 @@ open class Quasicord(
}

protected open fun initJDA(activity: Activity?): JDA {
val jda = default(config.token, enableCoroutines = true) {
val jda = default(config.token) {
disableIntents(
GatewayIntent.DIRECT_MESSAGE_TYPING,
GatewayIntent.GUILD_MESSAGE_TYPING, // GatewayIntent.GUILD_INTEGRATIONS, // unused, apparently
Expand All @@ -182,7 +181,7 @@ open class Quasicord(
enableIntents(GatewayIntent.GUILD_MESSAGES)
enableIntents(GatewayIntent.MESSAGE_CONTENT)
setMemberCachePolicy(MemberCachePolicy.ALL)
setEventManager(AnnotatedEventManager())
setEventManager(AnnotatedCoroutineEventManager())
disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE)
if (activity != null) setActivity(activity)
MessageRequest.setDefaultMentions(emptySet())
Expand Down

0 comments on commit b5e5f30

Please sign in to comment.