Skip to content

Commit

Permalink
fix: plugin logger name fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
devproje committed Sep 21, 2024
1 parent e93d298 commit be4e62a
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 34 deletions.
15 changes: 6 additions & 9 deletions px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@ import org.slf4j.LoggerFactory

abstract class Plugin {
private val handlerContainer = mutableListOf<ListenerAdapter>()
private val config = this.javaClass.getResourceAsStream("/plugin.json")?.let {
val config = this.javaClass.getResourceAsStream("/plugin.json")!!.let {
val raw = it.bufferedReader().readText()
val obj = Json.decodeFromString<PluginConfig>(raw)

return@let obj
}

fun getLogger(): Logger {
return LoggerFactory.getLogger(config?.name)
}
var logger: Logger = LoggerFactory.getLogger(this::class.java)

fun getHandlers(): List<ListenerAdapter> {
return handlerContainer
}
val handlers: List<ListenerAdapter>
get() = handlerContainer

fun addHandler(listener: ListenerAdapter) {
handlerContainer.add(listener)
handlerContainer += listener
}

fun delHandler(listener: ListenerAdapter) {
handlerContainer.remove(listener)
handlerContainer -= listener
}

abstract fun onLoad()
Expand Down
2 changes: 1 addition & 1 deletion px32-bot-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {

dependencies {
implementation(project(":${rootProject.name}-api"))
implementation(project(":${rootProject.name}-func"))
implementation(project(":px32-bot-module"))
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
Expand Down
4 changes: 0 additions & 4 deletions px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import net.projecttl.p.x32.command.PluginCommand
import net.projecttl.p.x32.command.Reload
import net.projecttl.p.x32.config.Config
import net.projecttl.p.x32.config.DefaultConfig
import net.projecttl.p.x32.func.loadDefault
import net.projecttl.p.x32.func.handler.Ready
import net.projecttl.p.x32.kernel.CoreKernel
import org.jetbrains.exposed.sql.Database
import org.slf4j.Logger
Expand All @@ -26,11 +24,9 @@ fun main() {

kernel = CoreKernel(Config.token)
val handler = kernel.getCommandContainer()
kernel.addHandler(Ready)

handler.addCommand(Reload)
handler.addCommand(PluginCommand)
loadDefault(handler)

jda = kernel.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ object Config {
val token: String by useConfig()
val owner: String by useConfig()

private val bundle_func: String by useConfig()
val bundle = if (bundle_func == "1") true else if (bundle_func == "0") false else throw IllegalArgumentException("bundle_func option must be 0 or 1")

val db_driver: String by useConfig()
val db_url: String by useConfig()
val db_username: String by useConfig()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package net.projecttl.p.x32.kernel

import kotlinx.serialization.json.Json
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.projecttl.p.x32.api.Plugin
import net.projecttl.p.x32.api.command.CommandHandler
import net.projecttl.p.x32.api.model.PluginConfig
import net.projecttl.p.x32.config.Config
import net.projecttl.p.x32.func.General
import net.projecttl.p.x32.jda
import net.projecttl.p.x32.logger

class CoreKernel(token: String) {
private val builder = JDABuilder.createDefault(token)
private val handlers = mutableListOf<ListenerAdapter>()
private val commandContainer = CommandHandler()

private fun include() {
if (Config.bundle) {
val b = General()
PluginLoader.putModule(b.config, b)
}
}

fun getCommandContainer(): CommandHandler {
return commandContainer
}
Expand All @@ -30,10 +40,11 @@ class CoreKernel(token: String) {
}

fun build(): JDA {
PluginLoader.load()
include()

PluginLoader.load()
plugins().forEach { plugin ->
plugin.getHandlers().forEach { handler ->
plugin.handlers.forEach { handler ->
handlers.add(handler)
}
}
Expand Down Expand Up @@ -62,18 +73,19 @@ class CoreKernel(token: String) {
val newHandlers = mutableListOf<ListenerAdapter>()
PluginLoader.destroy()
plugins().forEach { plugin ->
plugin.getHandlers().forEach { handler ->
plugin.handlers.forEach { handler ->
if (handlers.contains(handler)) {
jda.removeEventListener(handler)
handlers.remove(handler)
}
}
}

include()
PluginLoader.load()

plugins().forEach { plugin ->
plugin.getHandlers().forEach { handler ->
plugin.handlers.forEach { handler ->
if (!handlers.contains(handler)) {
handlers.add(handler)
newHandlers.add(handler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@ object PluginLoader {
return plugins.toMap()
}

fun putModule(config: PluginConfig, plugin: Plugin) {
try {
logger.info("Load module ${config.name} v${config.version}")
plugin.onLoad()
} catch (ex: Exception) {
ex.printStackTrace()
plugin.destroy()
return
}

plugins[config] = plugin
}

fun load() {
parentDir.listFiles()?.forEach { file ->
loadPlugin(file)
}

logger.info("Loaded ${plugins.size} plugins")
}

fun destroy() {
Expand All @@ -42,6 +57,10 @@ object PluginLoader {
}

private fun loadPlugin(file: File) {
if (file.name == "px32-bot-module") {
return
}

if (!file.name.endsWith(".jar")) {
return
}
Expand Down
2 changes: 2 additions & 0 deletions px32-bot-core/src/main/resources/config.sample.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
token=<discord_bot_token>
owner=

bundle_func=1

db_driver=org.sqlite.JDBC
db_url=jdbc:sqlite:data.db
db_username=
Expand Down
12 changes: 0 additions & 12 deletions px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/Loader.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
useJUnitPlatform()
tasks {
processResources {
filesMatching("plugin.json") {
expand(project.properties)
}
}

test {
useJUnitPlatform()
}
}
25 changes: 25 additions & 0 deletions px32-bot-module/src/main/kotlin/Conf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.io.File
import java.io.FileInputStream
import java.util.*
import kotlin.reflect.KProperty

object Conf {
private fun useConf(): ConfDel {
return ConfDel()
}

val owner: String by useConf()
}

private class ConfDel {
private val props = Properties()

init {
val file = File("config.properties")
props.load(FileInputStream(file))
}

operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return props.getProperty(property.name).toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.projecttl.p.x32.func

import net.dv8tion.jda.api.JDABuilder
import net.projecttl.p.x32.api.Plugin
import net.projecttl.p.x32.api.command.CommandHandler
import net.projecttl.p.x32.func.command.Avatar
import net.projecttl.p.x32.func.command.MsgPurge
import net.projecttl.p.x32.func.command.MsgLength
import net.projecttl.p.x32.func.command.Ping
import net.projecttl.p.x32.func.handler.Ready


class General : Plugin() {
override fun onLoad() {
logger.info("Created by Project_IO")
logger.info("Hello! This is Px32's general module!")

addHandler(Ready)
addHandler(with(CommandHandler()) {
addCommand(Avatar)
addCommand(MsgLength)
addCommand(Ping)
addCommand(MsgPurge)

this
})
}

override fun destroy() {
logger.info("bye!")
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.projecttl.p.x32.func.command

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.CommandData
import net.dv8tion.jda.internal.interactions.CommandDataImpl
import net.projecttl.p.x32.api.command.GlobalCommand

object MsgPurge : GlobalCommand {
override val data = CommandData.fromData(
CommandDataImpl("purge", "n개의 메시지를 채널에서 삭제해요").apply {
addOption(OptionType.INTEGER, "n", "n개만큼 메시지가 삭제 됩니다", true)
}.toData()
)

override suspend fun execute(ev: SlashCommandInteractionEvent) {
val n = ev.getOption("n")!!.asInt
if (n !in 1..100) {
return ev.reply(":warning: 1 부터 100까지 가능해요").setEphemeral(true).queue()
}

if (ev.user.id != Conf.owner) {
ev.reply(":warning: 이 명령어는 봇 관리자만 사용 가능해요").setEphemeral(true).queue()
return
}

try {
ev.channel.iterableHistory.takeAsync(n).thenAccept(ev.channel::purgeMessages)
} catch (ex: Exception) {
ex.printStackTrace()
return ev.reply(":warning: 메시지 삭제 도중에 문제가 발생 했어요").queue()
}

ev.reply(":white_check_mark: `${n}`개의 메시지를 삭제 했어요").queue()
}
}
6 changes: 6 additions & 0 deletions px32-bot-module/src/main/resources/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "${project.name}",
"version": "${project.version}",
"main": "net.projecttl.p.x32.func.General"
}

2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rootProject.name = "px32-bot"
include("px32-bot-core")
include("px32-bot-api")
include("px32-bot-func")
include("px32-bot-module")
include("sample-plugin")

0 comments on commit be4e62a

Please sign in to comment.