fix: inline handler loader to load(), destroy() method in kernel

This commit is contained in:
Project_IO 2024-10-03 13:39:32 +09:00
parent b526a5ef57
commit 208084b617

View file

@ -84,14 +84,26 @@ class CoreKernel(token: String) {
})
}
private fun include() {
suspend fun reload(jda: JDA) {
if (!memLock.isLocked) {
memLock.lock()
}
destroy()
load()
handlers.filterIsInstance<CommandHandler>().forEach { h ->
h.register(jda)
}
memLock.unlock()
}
private fun load() {
if (BotConfig.bundle) {
val b = BundleModule()
loadModule(b.config, b)
}
}
private fun load() {
parentDir.listFiles()?.forEach { file ->
try {
loadPlugin(file)
@ -100,15 +112,26 @@ class CoreKernel(token: String) {
}
}
logger.info("Loaded ${plugins.size} plugins")
var cnt = 0
plugins.forEach { (_, plugin) ->
plugin.handlers.forEach {
addHandler(it)
logger.info("Load event listener: ${it::class.simpleName}")
cnt++
}
}
logger.info("Loaded ${plugins.size} plugin${if (plugins.size > 1) "s" else ""} and $cnt handler${if (cnt > 1) "s" else ""}.")
}
private fun destroy() {
val unloaded = mutableListOf<PluginConfig>()
plugins.forEach { (config, plugin) ->
logger.info("disable ${config.name} plugin...")
plugin.handlers.forEach {
delHandler(it)
}
try {
plugin.destroy()
} catch (ex: Exception) {
@ -117,39 +140,7 @@ class CoreKernel(token: String) {
}
}
unloaded.forEach {
plugins.remove(it)
}
}
suspend fun reload(jda: JDA) {
if (!memLock.isLocked) {
memLock.lock()
}
plugins.forEach { (_, plugin) ->
plugin.handlers.forEach {
delHandler(it)
}
}
destroy()
plugins = mutableMapOf()
include()
load()
plugins.forEach { (_, plugin) ->
plugin.handlers.forEach {
addHandler(it)
}
}
handlers.filterIsInstance<CommandHandler>().forEach { h ->
h.register(jda)
}
memLock.unlock()
}
private fun loadPlugin(file: File) {
@ -208,16 +199,7 @@ class CoreKernel(token: String) {
return jda
}
include()
load()
plugins.forEach { (_, plugin) ->
plugin.handlers.forEach { handler ->
logger.info("Load event listener: ${handler::class.simpleName}")
addHandler(handler)
}
}
builder.addEventListeners(commandContainer)
jda = builder.build()
isActive = true