feat: middle save

This commit is contained in:
Project_IO 2024-09-23 02:18:29 +09:00
parent e9a57c9f6a
commit bc4c985735
14 changed files with 146 additions and 48 deletions

View file

@ -2,7 +2,7 @@ kotlin.code.style=official
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8
group=net.projecttl group=net.projecttl
version=0.1.0-SNAPSHOT version=0.2.0-SNAPSHOT
ktor_version=2.3.12 ktor_version=2.3.12
log4j_version=2.23.1 log4j_version=2.23.1

View file

@ -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.hooks.ListenerAdapter
import net.dv8tion.jda.api.interactions.commands.build.Commands 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() { class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() {
private val commands = mutableListOf<CommandExecutor>() private val commands = mutableListOf<CommandExecutor>()
@ -106,10 +113,10 @@ class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() {
if (command is MessageContext) { if (command is MessageContext) {
if (guild == null) { if (guild == null) {
jda.upsertCommand(Commands.message(data.name)) jda.upsertCommand(Commands.message(data.name)).queue()
println("Register Message Context Command: /${data.name}") println("Register Message Context Command: /${data.name}")
} else { } 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}") println("Register '${guild.id}' Guild's Message Context Command: /${data.name}")
} }
} }

View file

@ -14,9 +14,7 @@ repositories {
dependencies { dependencies {
implementation(project(":${rootProject.name}-api")) implementation(project(":${rootProject.name}-api"))
implementation(project(":px32-bot-module")) implementation(project(":${rootProject.name}-module"))
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
} }
tasks { tasks {

View file

@ -1,6 +1,7 @@
package net.projecttl.p.x32 package net.projecttl.p.x32
import net.dv8tion.jda.api.JDA 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.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
@ -25,6 +26,7 @@ fun main() {
kernel = CoreKernel(Config.token) kernel = CoreKernel(Config.token)
val handler = kernel.getCommandContainer() val handler = kernel.getCommandContainer()
handler.addCommand(Info)
handler.addCommand(Reload) handler.addCommand(Reload)
handler.addCommand(PluginCommand) handler.addCommand(PluginCommand)

View file

@ -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 <t:${(System.currentTimeMillis() - rb.uptime)/ 1000L}:R>
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()
}
}

View file

@ -3,22 +3,31 @@ package net.projecttl.p.x32.kernel
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.dv8tion.jda.api.requests.GatewayIntent
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.config.Config 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.jda
import net.projecttl.p.x32.logger
class CoreKernel(token: String) { class CoreKernel(token: String) {
var memLock = false var memLock = false
private set 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<ListenerAdapter>() private val handlers = mutableListOf<ListenerAdapter>()
private val commandContainer = CommandHandler() private val commandContainer = CommandHandler()
private fun include() { private fun include() {
if (Config.bundle) { if (Config.bundle) {
val b = General() val b = BundleModule()
PluginLoader.putModule(b.config, b) PluginLoader.putModule(b.config, b)
} }
} }
@ -50,6 +59,7 @@ class CoreKernel(token: String) {
} }
handlers.map { handlers.map {
logger.info("Load event listener: ${it::class.simpleName}")
builder.addEventListeners(it) builder.addEventListeners(it)
} }
builder.addEventListeners(commandContainer) builder.addEventListeners(commandContainer)

View file

@ -2,8 +2,8 @@ plugins {
id("java") id("java")
} }
group = "net.projecttl" group = rootProject.group
version = "0.1.0-SNAPSHOT" version = rootProject.version
repositories { repositories {
mavenCentral() mavenCentral()

View file

@ -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!")
}
}

View file

@ -1,14 +1,21 @@
package net.projecttl.p.x32.func
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.util.* import java.util.*
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
object Conf { object Conf {
private fun useDef(): DefDel {
return DefDel()
}
private fun useConf(): ConfDel { private fun useConf(): ConfDel {
return ConfDel() return ConfDel()
} }
val owner: String by useConf() val owner: String by useConf()
val version: String by useDef()
} }
private class ConfDel { private class ConfDel {
@ -23,3 +30,15 @@ private class ConfDel {
return props.getProperty(property.name).toString() 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()
}
}

View file

@ -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!")
}
}

View file

@ -10,6 +10,6 @@ object MsgLength : MessageContext {
override suspend fun execute(ev: MessageContextInteractionEvent) { override suspend fun execute(ev: MessageContextInteractionEvent) {
val target = ev.target val target = ev.target
ev.reply("${target.jumpUrl} 메시지의 길이:\n\t${target.contentRaw.split("\\s+").size}").queue() ev.reply("${target.jumpUrl} 메시지의 길이: ${target.contentRaw.length}").queue()
} }
} }

View file

@ -1,6 +1,6 @@
package net.projecttl.p.x32.func.command 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.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.CommandData import net.dv8tion.jda.api.interactions.commands.build.CommandData

View file

@ -1,10 +1,36 @@
package net.projecttl.p.x32.func.handler 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.events.session.ReadyEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.projecttl.p.x32.func.Conf
import net.projecttl.p.x32.func.instance
object Ready : ListenerAdapter() { object Ready : ListenerAdapter() {
@OptIn(DelicateCoroutinesApi::class)
override fun onReady(ev: ReadyEvent) { 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}") 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)
}
} }
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "${project.name}", "name": "bundle-module",
"version": "${project.version}", "version": "${project.version}",
"main": "net.projecttl.p.x32.func.General" "main": "net.projecttl.p.x32.func.BundleModule"
} }