mirror of
https://github.com/devproje/px32-bot.git
synced 2024-11-26 10:43:05 +00:00
fix: command register function fixed
This commit is contained in:
parent
0e8fdee886
commit
34426e5975
8 changed files with 76 additions and 30 deletions
|
@ -92,35 +92,21 @@ class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() {
|
|||
|
||||
if (command is UserContext) {
|
||||
if (guild == null) {
|
||||
jda.updateCommands().addCommands(
|
||||
Commands.context(Command.Type.USER, data.name),
|
||||
Commands.message(data.name)
|
||||
).queue()
|
||||
jda.upsertCommand(Commands.user(data.name)).queue()
|
||||
|
||||
println("Register User Context Command: /${data.name}")
|
||||
} else {
|
||||
guild.updateCommands().addCommands(
|
||||
Commands.context(Command.Type.USER, data.name),
|
||||
Commands.message(data.name)
|
||||
).queue()
|
||||
guild.upsertCommand(Commands.user(data.name)).queue()
|
||||
println("Register '${guild.id}' Guild's User Context Command: /${data.name}")
|
||||
}
|
||||
}
|
||||
|
||||
if (command is MessageContext) {
|
||||
if (guild == null) {
|
||||
jda.updateCommands().addCommands(
|
||||
Commands.context(Command.Type.MESSAGE, data.name),
|
||||
Commands.message(data.name)
|
||||
)
|
||||
|
||||
jda.upsertCommand(Commands.message(data.name))
|
||||
println("Register Message Context Command: /${data.name}")
|
||||
} else {
|
||||
guild.updateCommands().addCommands(
|
||||
Commands.context(Command.Type.MESSAGE, data.name),
|
||||
Commands.message(data.name)
|
||||
)
|
||||
|
||||
guild.upsertCommand(Commands.message(data.name))
|
||||
println("Register '${guild.id}' Guild's Message Context Command: /${data.name}")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,25 @@
|
|||
package net.projecttl.p.x32
|
||||
|
||||
import net.dv8tion.jda.api.JDA
|
||||
import net.projecttl.p.x32.command.Reload
|
||||
import net.projecttl.p.x32.config.DefaultConfig
|
||||
import net.projecttl.p.x32.func.loadDefault
|
||||
import net.projecttl.p.x32.handler.Ready
|
||||
import net.projecttl.p.x32.func.handler.Ready
|
||||
import net.projecttl.p.x32.kernel.CoreKernel
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
lateinit var jda: JDA
|
||||
lateinit var kernel: CoreKernel
|
||||
val logger: Logger = LoggerFactory.getLogger(Px32::class.java)
|
||||
|
||||
fun main() {
|
||||
println("Px32 version v${DefaultConfig.version}")
|
||||
val kernel = CoreKernel(System.getenv("TOKEN"))
|
||||
kernel = CoreKernel(System.getenv("TOKEN"))
|
||||
val handler = kernel.getGlobalCommandHandler()
|
||||
kernel.addHandler(Ready)
|
||||
|
||||
val handler = kernel.getGlobalCommandHandler()
|
||||
handler.addCommand(Reload)
|
||||
loadDefault(handler)
|
||||
|
||||
jda = kernel.build()
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package net.projecttl.p.x32.command
|
||||
|
||||
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.kernel
|
||||
|
||||
object Reload : GlobalCommand {
|
||||
override val data = CommandData.fromData(CommandDataImpl("reload", "플러그인을 다시 불러 옵니다").toData())
|
||||
|
||||
override suspend fun execute(ev: SlashCommandInteractionEvent) {
|
||||
try {
|
||||
kernel.reload()
|
||||
} catch (ex: Exception) {
|
||||
ex.printStackTrace()
|
||||
ev.reply(":warning: 플러그인을 다시 불러오는 도중에 오류가 발생 했어요. 자세한 내용은 콘솔을 확인해 주세요!").queue()
|
||||
return
|
||||
}
|
||||
|
||||
ev.reply(":white_check_mark: 플러그인을 다시 불러 왔어요!\n불러온 플러그인 수: ${kernel.plugins().size}").queue()
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ 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.projecttl.p.x32.api.Plugin
|
||||
import net.projecttl.p.x32.api.command.CommandHandler
|
||||
import net.projecttl.p.x32.logger
|
||||
|
||||
|
@ -23,6 +24,10 @@ class CoreKernel(token: String) {
|
|||
handlers.remove(handler)
|
||||
}
|
||||
|
||||
fun plugins(): List<Plugin> {
|
||||
return PluginLoader.getPlugins().map { it.value }
|
||||
}
|
||||
|
||||
fun build(): JDA {
|
||||
handlers.map {
|
||||
builder.addEventListeners(it)
|
||||
|
@ -54,4 +59,17 @@ class CoreKernel(token: String) {
|
|||
|
||||
return jda
|
||||
}
|
||||
}
|
||||
|
||||
fun reload() {
|
||||
val plugins = PluginLoader.getPlugins()
|
||||
plugins.forEach { (c, p) ->
|
||||
logger.info("Reload plugin ${c.name} v${c.version}")
|
||||
p.destroy()
|
||||
}
|
||||
|
||||
PluginLoader.load()
|
||||
plugins.forEach { (_, p) ->
|
||||
p.onLoad()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,10 @@ object PluginLoader {
|
|||
}
|
||||
}
|
||||
|
||||
fun getPlugins(): Map<PluginConfig, Plugin> {
|
||||
return plugins.toMap()
|
||||
}
|
||||
|
||||
fun load() {
|
||||
parentDir.listFiles()?.forEach { file ->
|
||||
if (file.name.endsWith(".jar")) {
|
||||
|
@ -45,10 +49,6 @@ object PluginLoader {
|
|||
}
|
||||
}
|
||||
|
||||
fun getPlugins(): Map<PluginConfig, Plugin> {
|
||||
return plugins.toMap()
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
plugins.forEach { (config, plugin) ->
|
||||
logger.info("disable ${config.name} plugin...")
|
||||
|
@ -61,4 +61,4 @@ object PluginLoader {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ 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)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package net.projecttl.p.x32.func.command
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData
|
||||
import net.dv8tion.jda.internal.interactions.CommandDataImpl
|
||||
import net.projecttl.p.x32.api.command.MessageContext
|
||||
|
||||
object MsgLength : MessageContext {
|
||||
override val data = CommandData.fromData(CommandDataImpl("length", "메시지의 길이를 확인 합니다.").toData())
|
||||
|
||||
override suspend fun execute(ev: MessageContextInteractionEvent) {
|
||||
val target = ev.target
|
||||
ev.reply("${target.jumpUrl} 메시지의 길이:\n\t${target.contentRaw.split("\\s+").size}").queue()
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
package net.projecttl.p.x32.handler
|
||||
package net.projecttl.p.x32.func.handler
|
||||
|
||||
import net.dv8tion.jda.api.events.session.ReadyEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import net.projecttl.p.x32.logger
|
||||
|
||||
object Ready : ListenerAdapter() {
|
||||
override fun onReady(ev: ReadyEvent) {
|
||||
logger.info("Logged in as ${ev.jda.selfUser.asTag}")
|
||||
println("Logged in as ${ev.jda.selfUser.asTag}")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue