From bc4c985735cecdab56cb7f09ce4bff158a12175a Mon Sep 17 00:00:00 2001 From: Project_IO Date: Mon, 23 Sep 2024 02:18:29 +0900 Subject: [PATCH] feat: middle save --- gradle.properties | 2 +- .../p/x32/api/command/CommandHandler.kt | 11 +++++- px32-bot-core/build.gradle.kts | 4 +- .../main/kotlin/net/projecttl/p/x32/Px32.kt | 2 + .../net/projecttl/p/x32/command/Info.kt | 38 +++++++++++++++++++ .../net/projecttl/p/x32/kernel/CoreKernel.kt | 16 ++++++-- px32-bot-module/build.gradle.kts | 4 +- .../net/projecttl/p/x32/func/BundleModule.kt | 31 +++++++++++++++ .../{ => net/projecttl/p/x32/func}/Conf.kt | 19 ++++++++++ .../net/projecttl/p/x32/func/General.kt | 33 ---------------- .../projecttl/p/x32/func/command/MsgLength.kt | 2 +- .../projecttl/p/x32/func/command/MsgPurge.kt | 2 +- .../net/projecttl/p/x32/func/handler/Ready.kt | 26 +++++++++++++ .../src/main/resources/plugin.json | 4 +- 14 files changed, 146 insertions(+), 48 deletions(-) create mode 100644 px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Info.kt create mode 100644 px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/BundleModule.kt rename px32-bot-module/src/main/kotlin/{ => net/projecttl/p/x32/func}/Conf.kt (55%) delete mode 100644 px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/General.kt diff --git a/gradle.properties b/gradle.properties index e1b3cfe..009879a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ kotlin.code.style=official org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 group=net.projecttl -version=0.1.0-SNAPSHOT +version=0.2.0-SNAPSHOT ktor_version=2.3.12 log4j_version=2.23.1 diff --git a/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt b/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt index fa75d46..5613e10 100644 --- a/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt +++ b/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt @@ -8,6 +8,13 @@ import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEven import net.dv8tion.jda.api.hooks.ListenerAdapter import net.dv8tion.jda.api.interactions.commands.build.Commands +fun commandHandler(block: (CommandHandler) -> Unit): CommandHandler { + val handler = CommandHandler() + block.invoke(handler) + + return handler +} + class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() { private val commands = mutableListOf() @@ -106,10 +113,10 @@ class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() { if (command is MessageContext) { if (guild == null) { - jda.upsertCommand(Commands.message(data.name)) + jda.upsertCommand(Commands.message(data.name)).queue() println("Register Message Context Command: /${data.name}") } else { - guild.upsertCommand(Commands.message(data.name)) + guild.upsertCommand(Commands.message(data.name)).queue() println("Register '${guild.id}' Guild's Message Context Command: /${data.name}") } } diff --git a/px32-bot-core/build.gradle.kts b/px32-bot-core/build.gradle.kts index 5478c3a..7a7b5ab 100644 --- a/px32-bot-core/build.gradle.kts +++ b/px32-bot-core/build.gradle.kts @@ -14,9 +14,7 @@ repositories { dependencies { implementation(project(":${rootProject.name}-api")) - implementation(project(":px32-bot-module")) - testImplementation(platform("org.junit:junit-bom:5.10.0")) - testImplementation("org.junit.jupiter:junit-jupiter") + implementation(project(":${rootProject.name}-module")) } tasks { diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt index 23921f1..f30144c 100644 --- a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt @@ -1,6 +1,7 @@ package net.projecttl.p.x32 import net.dv8tion.jda.api.JDA +import net.projecttl.p.x32.command.Info import net.projecttl.p.x32.command.PluginCommand import net.projecttl.p.x32.command.Reload import net.projecttl.p.x32.config.Config @@ -25,6 +26,7 @@ fun main() { kernel = CoreKernel(Config.token) val handler = kernel.getCommandContainer() + handler.addCommand(Info) handler.addCommand(Reload) handler.addCommand(PluginCommand) diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Info.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Info.kt new file mode 100644 index 0000000..4f38c87 --- /dev/null +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Info.kt @@ -0,0 +1,38 @@ +package net.projecttl.p.x32.command + +import net.dv8tion.jda.api.JDAInfo +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent +import net.dv8tion.jda.api.interactions.commands.build.CommandData +import net.dv8tion.jda.internal.interactions.CommandDataImpl +import net.projecttl.p.x32.api.command.GlobalCommand +import net.projecttl.p.x32.config.DefaultConfig +import net.projecttl.p.x32.kernel.PluginLoader +import java.lang.management.ManagementFactory + +object Info : GlobalCommand { + override val data: CommandData = CommandData.fromData( + CommandDataImpl("info","봇의 정보를 표시 합니다").toData() + ) + + override suspend fun execute(ev: SlashCommandInteractionEvent) { + val rb = ManagementFactory.getRuntimeMXBean() + val r = Runtime.getRuntime() + + val size = PluginLoader.getPlugins().size + + val info = """ + Px32Bot v${DefaultConfig.version}, JDA `v${JDAInfo.VERSION}`, + `Java ${System.getProperty("java.version")}` and `Kotlin ${KotlinVersion.CURRENT}` System on `${System.getProperty("os.name")}` + + Process Started on + Bot Process Running on PID `${rb.pid}` + + Assigned `${r.maxMemory() / 1048576}MB` of Max Memories at this Bot + Using `${(r.totalMemory() - r.freeMemory()) / 1048576}MB` at this Bot + + Total $size plugin${if (size > 1) "s" else ""} loaded + """.trimIndent() + + ev.reply(info).queue() + } +} \ No newline at end of file diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt index 17b3527..35410b0 100644 --- a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt @@ -3,22 +3,31 @@ package net.projecttl.p.x32.kernel import net.dv8tion.jda.api.JDA import net.dv8tion.jda.api.JDABuilder import net.dv8tion.jda.api.hooks.ListenerAdapter +import net.dv8tion.jda.api.requests.GatewayIntent import net.projecttl.p.x32.api.Plugin import net.projecttl.p.x32.api.command.CommandHandler import net.projecttl.p.x32.config.Config -import net.projecttl.p.x32.func.General +import net.projecttl.p.x32.func.BundleModule import net.projecttl.p.x32.jda +import net.projecttl.p.x32.logger class CoreKernel(token: String) { var memLock = false private set - private val builder = JDABuilder.createDefault(token) + private val builder = JDABuilder.createDefault(token, listOf( + GatewayIntent.GUILD_PRESENCES, + GatewayIntent.GUILD_MEMBERS, + GatewayIntent.MESSAGE_CONTENT, + GatewayIntent.GUILD_VOICE_STATES, + GatewayIntent.GUILD_EMOJIS_AND_STICKERS, + GatewayIntent.SCHEDULED_EVENTS + )) private val handlers = mutableListOf() private val commandContainer = CommandHandler() private fun include() { if (Config.bundle) { - val b = General() + val b = BundleModule() PluginLoader.putModule(b.config, b) } } @@ -50,6 +59,7 @@ class CoreKernel(token: String) { } handlers.map { + logger.info("Load event listener: ${it::class.simpleName}") builder.addEventListeners(it) } builder.addEventListeners(commandContainer) diff --git a/px32-bot-module/build.gradle.kts b/px32-bot-module/build.gradle.kts index 8146809..f5e28cd 100644 --- a/px32-bot-module/build.gradle.kts +++ b/px32-bot-module/build.gradle.kts @@ -2,8 +2,8 @@ plugins { id("java") } -group = "net.projecttl" -version = "0.1.0-SNAPSHOT" +group = rootProject.group +version = rootProject.version repositories { mavenCentral() diff --git a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/BundleModule.kt b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/BundleModule.kt new file mode 100644 index 0000000..e8c6226 --- /dev/null +++ b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/BundleModule.kt @@ -0,0 +1,31 @@ +package net.projecttl.p.x32.func + +import net.projecttl.p.x32.api.Plugin +import net.projecttl.p.x32.api.command.commandHandler +import net.projecttl.p.x32.func.command.* +import net.projecttl.p.x32.func.handler.Ready + +lateinit var instance: BundleModule + +class BundleModule : Plugin() { + override fun onLoad() { + instance = this + + logger.info("Created by Project_IO") + logger.info("Hello! This is Px32's general module!") + + addHandler(Ready) + addHandler(commandHandler { handler -> + handler.addCommand(Avatar) + handler.addCommand(MsgLength) + handler.addCommand(MsgPurge) + handler.addCommand(Ping) + }) + } + + override fun destroy() { + logger.info("bye!") + } +} + + diff --git a/px32-bot-module/src/main/kotlin/Conf.kt b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/Conf.kt similarity index 55% rename from px32-bot-module/src/main/kotlin/Conf.kt rename to px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/Conf.kt index 822657b..c4f4103 100644 --- a/px32-bot-module/src/main/kotlin/Conf.kt +++ b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/Conf.kt @@ -1,14 +1,21 @@ +package net.projecttl.p.x32.func + import java.io.File import java.io.FileInputStream import java.util.* import kotlin.reflect.KProperty object Conf { + private fun useDef(): DefDel { + return DefDel() + } + private fun useConf(): ConfDel { return ConfDel() } val owner: String by useConf() + val version: String by useDef() } private class ConfDel { @@ -23,3 +30,15 @@ private class ConfDel { return props.getProperty(property.name).toString() } } + +class DefDel { + private val props = Properties() + + init { + props.load(this.javaClass.getResourceAsStream("/default.properties")) + } + + operator fun getValue(thisRef: Any?, property: KProperty<*>): String { + return props.getProperty(property.name).toString() + } +} diff --git a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/General.kt b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/General.kt deleted file mode 100644 index c4913af..0000000 --- a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/General.kt +++ /dev/null @@ -1,33 +0,0 @@ -package net.projecttl.p.x32.func - -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.MsgLength -import net.projecttl.p.x32.func.command.MsgPurge -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!") - } -} - - diff --git a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt index df29f63..973cbbc 100644 --- a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt +++ b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt @@ -10,6 +10,6 @@ object MsgLength : MessageContext { override suspend fun execute(ev: MessageContextInteractionEvent) { val target = ev.target - ev.reply("${target.jumpUrl} 메시지의 길이:\n\t${target.contentRaw.split("\\s+").size}").queue() + ev.reply("${target.jumpUrl} 메시지의 길이: ${target.contentRaw.length}").queue() } } diff --git a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgPurge.kt b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgPurge.kt index dcee12d..b09a18a 100644 --- a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgPurge.kt +++ b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/command/MsgPurge.kt @@ -1,6 +1,6 @@ package net.projecttl.p.x32.func.command -import Conf +import net.projecttl.p.x32.func.Conf 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 diff --git a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt index edf8a8a..574002a 100644 --- a/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt +++ b/px32-bot-module/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt @@ -1,10 +1,36 @@ package net.projecttl.p.x32.func.handler +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import net.dv8tion.jda.api.OnlineStatus +import net.dv8tion.jda.api.entities.Activity import net.dv8tion.jda.api.events.session.ReadyEvent import net.dv8tion.jda.api.hooks.ListenerAdapter +import net.projecttl.p.x32.func.Conf +import net.projecttl.p.x32.func.instance object Ready : ListenerAdapter() { + @OptIn(DelicateCoroutinesApi::class) override fun onReady(ev: ReadyEvent) { + val list = listOf( + Activity.playing("${ev.jda.selfUser.name} v${Conf.version}"), + Activity.listening("${ev.guildTotalCount}개의 서버와 함께 서비스 하는 중") + ) println("Logged in as ${ev.jda.selfUser.asTag}") + + GlobalScope.launch { + do { + list.forEach { act -> + try { + ev.jda.presence.setPresence(OnlineStatus.ONLINE, act) + } catch (ex: Exception) { + instance.logger.error(ex.message) + } + delay(1000 * 10L) + } + } while (true) + } } } \ No newline at end of file diff --git a/px32-bot-module/src/main/resources/plugin.json b/px32-bot-module/src/main/resources/plugin.json index b32e237..a513b5f 100644 --- a/px32-bot-module/src/main/resources/plugin.json +++ b/px32-bot-module/src/main/resources/plugin.json @@ -1,6 +1,6 @@ { - "name": "${project.name}", + "name": "bundle-module", "version": "${project.version}", - "main": "net.projecttl.p.x32.func.General" + "main": "net.projecttl.p.x32.func.BundleModule" }