mirror of
https://github.com/devproje/px32-bot.git
synced 2024-11-26 10:43:05 +00:00
fix: plugin logger name fixed
This commit is contained in:
parent
e93d2988f9
commit
be4e62ab7e
18 changed files with 160 additions and 34 deletions
|
@ -9,27 +9,24 @@ import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
abstract class Plugin {
|
abstract class Plugin {
|
||||||
private val handlerContainer = mutableListOf<ListenerAdapter>()
|
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 raw = it.bufferedReader().readText()
|
||||||
val obj = Json.decodeFromString<PluginConfig>(raw)
|
val obj = Json.decodeFromString<PluginConfig>(raw)
|
||||||
|
|
||||||
return@let obj
|
return@let obj
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLogger(): Logger {
|
var logger: Logger = LoggerFactory.getLogger(this::class.java)
|
||||||
return LoggerFactory.getLogger(config?.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getHandlers(): List<ListenerAdapter> {
|
val handlers: List<ListenerAdapter>
|
||||||
return handlerContainer
|
get() = handlerContainer
|
||||||
}
|
|
||||||
|
|
||||||
fun addHandler(listener: ListenerAdapter) {
|
fun addHandler(listener: ListenerAdapter) {
|
||||||
handlerContainer.add(listener)
|
handlerContainer += listener
|
||||||
}
|
}
|
||||||
|
|
||||||
fun delHandler(listener: ListenerAdapter) {
|
fun delHandler(listener: ListenerAdapter) {
|
||||||
handlerContainer.remove(listener)
|
handlerContainer -= listener
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract fun onLoad()
|
abstract fun onLoad()
|
||||||
|
|
|
@ -14,7 +14,7 @@ repositories {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(project(":${rootProject.name}-api"))
|
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(platform("org.junit:junit-bom:5.10.0"))
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ import net.projecttl.p.x32.command.PluginCommand
|
||||||
import net.projecttl.p.x32.command.Reload
|
import net.projecttl.p.x32.command.Reload
|
||||||
import net.projecttl.p.x32.config.Config
|
import net.projecttl.p.x32.config.Config
|
||||||
import net.projecttl.p.x32.config.DefaultConfig
|
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 net.projecttl.p.x32.kernel.CoreKernel
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.Database
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
|
@ -26,11 +24,9 @@ fun main() {
|
||||||
|
|
||||||
kernel = CoreKernel(Config.token)
|
kernel = CoreKernel(Config.token)
|
||||||
val handler = kernel.getCommandContainer()
|
val handler = kernel.getCommandContainer()
|
||||||
kernel.addHandler(Ready)
|
|
||||||
|
|
||||||
handler.addCommand(Reload)
|
handler.addCommand(Reload)
|
||||||
handler.addCommand(PluginCommand)
|
handler.addCommand(PluginCommand)
|
||||||
loadDefault(handler)
|
|
||||||
|
|
||||||
jda = kernel.build()
|
jda = kernel.build()
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,9 @@ object Config {
|
||||||
val token: String by useConfig()
|
val token: String by useConfig()
|
||||||
val owner: 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_driver: String by useConfig()
|
||||||
val db_url: String by useConfig()
|
val db_url: String by useConfig()
|
||||||
val db_username: String by useConfig()
|
val db_username: String by useConfig()
|
||||||
|
|
|
@ -1,18 +1,28 @@
|
||||||
package net.projecttl.p.x32.kernel
|
package net.projecttl.p.x32.kernel
|
||||||
|
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.JDABuilder
|
import net.dv8tion.jda.api.JDABuilder
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||||
import net.projecttl.p.x32.api.Plugin
|
import net.projecttl.p.x32.api.Plugin
|
||||||
import net.projecttl.p.x32.api.command.CommandHandler
|
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.jda
|
||||||
import net.projecttl.p.x32.logger
|
|
||||||
|
|
||||||
class CoreKernel(token: String) {
|
class CoreKernel(token: String) {
|
||||||
private val builder = JDABuilder.createDefault(token)
|
private val builder = JDABuilder.createDefault(token)
|
||||||
private val handlers = mutableListOf<ListenerAdapter>()
|
private val handlers = mutableListOf<ListenerAdapter>()
|
||||||
private val commandContainer = CommandHandler()
|
private val commandContainer = CommandHandler()
|
||||||
|
|
||||||
|
private fun include() {
|
||||||
|
if (Config.bundle) {
|
||||||
|
val b = General()
|
||||||
|
PluginLoader.putModule(b.config, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun getCommandContainer(): CommandHandler {
|
fun getCommandContainer(): CommandHandler {
|
||||||
return commandContainer
|
return commandContainer
|
||||||
}
|
}
|
||||||
|
@ -30,10 +40,11 @@ class CoreKernel(token: String) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun build(): JDA {
|
fun build(): JDA {
|
||||||
PluginLoader.load()
|
include()
|
||||||
|
|
||||||
|
PluginLoader.load()
|
||||||
plugins().forEach { plugin ->
|
plugins().forEach { plugin ->
|
||||||
plugin.getHandlers().forEach { handler ->
|
plugin.handlers.forEach { handler ->
|
||||||
handlers.add(handler)
|
handlers.add(handler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,7 +73,7 @@ class CoreKernel(token: String) {
|
||||||
val newHandlers = mutableListOf<ListenerAdapter>()
|
val newHandlers = mutableListOf<ListenerAdapter>()
|
||||||
PluginLoader.destroy()
|
PluginLoader.destroy()
|
||||||
plugins().forEach { plugin ->
|
plugins().forEach { plugin ->
|
||||||
plugin.getHandlers().forEach { handler ->
|
plugin.handlers.forEach { handler ->
|
||||||
if (handlers.contains(handler)) {
|
if (handlers.contains(handler)) {
|
||||||
jda.removeEventListener(handler)
|
jda.removeEventListener(handler)
|
||||||
handlers.remove(handler)
|
handlers.remove(handler)
|
||||||
|
@ -70,10 +81,11 @@ class CoreKernel(token: String) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
include()
|
||||||
PluginLoader.load()
|
PluginLoader.load()
|
||||||
|
|
||||||
plugins().forEach { plugin ->
|
plugins().forEach { plugin ->
|
||||||
plugin.getHandlers().forEach { handler ->
|
plugin.handlers.forEach { handler ->
|
||||||
if (!handlers.contains(handler)) {
|
if (!handlers.contains(handler)) {
|
||||||
handlers.add(handler)
|
handlers.add(handler)
|
||||||
newHandlers.add(handler)
|
newHandlers.add(handler)
|
||||||
|
|
|
@ -22,10 +22,25 @@ object PluginLoader {
|
||||||
return plugins.toMap()
|
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() {
|
fun load() {
|
||||||
parentDir.listFiles()?.forEach { file ->
|
parentDir.listFiles()?.forEach { file ->
|
||||||
loadPlugin(file)
|
loadPlugin(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info("Loaded ${plugins.size} plugins")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun destroy() {
|
fun destroy() {
|
||||||
|
@ -42,6 +57,10 @@ object PluginLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadPlugin(file: File) {
|
private fun loadPlugin(file: File) {
|
||||||
|
if (file.name == "px32-bot-module") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!file.name.endsWith(".jar")) {
|
if (!file.name.endsWith(".jar")) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
token=<discord_bot_token>
|
token=<discord_bot_token>
|
||||||
owner=
|
owner=
|
||||||
|
|
||||||
|
bundle_func=1
|
||||||
|
|
||||||
db_driver=org.sqlite.JDBC
|
db_driver=org.sqlite.JDBC
|
||||||
db_url=jdbc:sqlite:data.db
|
db_url=jdbc:sqlite:data.db
|
||||||
db_username=
|
db_username=
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
package net.projecttl.p.x32.func
|
|
||||||
|
|
||||||
import net.projecttl.p.x32.api.command.CommandHandler
|
|
||||||
import net.projecttl.p.x32.func.command.Avatar
|
|
||||||
import net.projecttl.p.x32.func.command.MsgLength
|
|
||||||
import net.projecttl.p.x32.func.command.Ping
|
|
||||||
|
|
||||||
fun loadDefault(handler: CommandHandler) = with(handler) {
|
|
||||||
addCommand(Avatar)
|
|
||||||
addCommand(MsgLength)
|
|
||||||
addCommand(Ping)
|
|
||||||
}
|
|
|
@ -15,6 +15,14 @@ dependencies {
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.test {
|
tasks {
|
||||||
useJUnitPlatform()
|
processResources {
|
||||||
|
filesMatching("plugin.json") {
|
||||||
|
expand(project.properties)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
}
|
}
|
25
px32-bot-module/src/main/kotlin/Conf.kt
Normal file
25
px32-bot-module/src/main/kotlin/Conf.kt
Normal file
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -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!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
px32-bot-module/src/main/resources/plugin.json
Normal file
6
px32-bot-module/src/main/resources/plugin.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"name": "${project.name}",
|
||||||
|
"version": "${project.version}",
|
||||||
|
"main": "net.projecttl.p.x32.func.General"
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
rootProject.name = "px32-bot"
|
rootProject.name = "px32-bot"
|
||||||
include("px32-bot-core")
|
include("px32-bot-core")
|
||||||
include("px32-bot-api")
|
include("px32-bot-api")
|
||||||
include("px32-bot-func")
|
include("px32-bot-module")
|
||||||
include("sample-plugin")
|
include("sample-plugin")
|
||||||
|
|
Loading…
Reference in a new issue