From c889cbe2d82d8c06004874441992feb071f7d70f Mon Sep 17 00:00:00 2001 From: Project_IO Date: Fri, 20 Sep 2024 10:34:58 +0900 Subject: [PATCH] feat: sample plugin --- build.gradle.kts | 2 +- .../kotlin/net/projecttl/p/x32/api/Plugin.kt | 2 +- .../p/x32/api/command/CommandHandler.kt | 5 ++- .../net/projecttl/p/x32/kernel/CoreKernel.kt | 1 + sample-plugin/build.gradle.kts | 40 +++++++++++++++++++ .../projecttl/plugin/sample/CorePlugin.groovy | 16 ++++++++ .../plugin/sample/command/Greeting.groovy | 23 +++++++++++ sample-plugin/src/main/resources/plugin.json | 5 +++ settings.gradle.kts | 1 + 9 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 sample-plugin/build.gradle.kts create mode 100644 sample-plugin/src/main/groovy/net/projecttl/plugin/sample/CorePlugin.groovy create mode 100644 sample-plugin/src/main/groovy/net/projecttl/plugin/sample/command/Greeting.groovy create mode 100644 sample-plugin/src/main/resources/plugin.json diff --git a/build.gradle.kts b/build.gradle.kts index 3d8f226..50403ff 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { group = property("group")!! version = property("version")!! - +plugins val ktor_version: String by project val log4j_version: String by project val exposed_version: String by project diff --git a/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/Plugin.kt b/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/Plugin.kt index bdc2d67..cc4a3bc 100644 --- a/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/Plugin.kt +++ b/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/Plugin.kt @@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory abstract class Plugin { private val globalCommandHandler = CommandHandler() - private val handlerContainer = mutableListOf(globalCommandHandler) + private val handlerContainer = mutableListOf() private val config = this.javaClass.getResourceAsStream("/plugin.json")?.let { val raw = it.bufferedReader().readText() val obj = Json.decodeFromString(raw) 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 28c949b..fa75d46 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 @@ -6,7 +6,6 @@ import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionE import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent import net.dv8tion.jda.api.hooks.ListenerAdapter -import net.dv8tion.jda.api.interactions.commands.Command import net.dv8tion.jda.api.interactions.commands.build.Commands class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() { @@ -68,6 +67,10 @@ class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() { commands.remove(command) } + fun getCommands(): List { + return commands + } + fun register(jda: JDA) { val guild = jda.getGuildById(guildId) if (guildId != 0L) { 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 c3b3d5e..be88c0e 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 @@ -40,6 +40,7 @@ class CoreKernel(token: String) { logger.info("Load plugin ${c.name} v${c.version}") p.onLoad() + builder.addEventListeners(p.getCommandContainer()) p.getHandlers().map { handler -> builder.addEventListeners(handler) } diff --git a/sample-plugin/build.gradle.kts b/sample-plugin/build.gradle.kts new file mode 100644 index 0000000..27bbac1 --- /dev/null +++ b/sample-plugin/build.gradle.kts @@ -0,0 +1,40 @@ +plugins { + groovy + id("com.gradleup.shadow") version "8.3.0" +} + +group = rootProject.group +version = "0.1.0-SNAPSHOT" + +repositories { + mavenCentral() +} + +dependencies { + compileOnly(project(":px32-bot-api")) + implementation("org.apache.groovy:groovy:4.0.14") + testImplementation(platform("org.junit:junit-bom:5.10.0")) + testImplementation("org.junit.jupiter:junit-jupiter") +} + +tasks { + withType { + options.encoding = "UTF-8" + } + + processResources { + filesMatching("plugin.json") { + expand(project.properties) + } + } + + shadowJar { + archiveBaseName.set(project.name) + archiveClassifier.set("") + archiveVersion.set("") + } + + test { + useJUnitPlatform() + } +} \ No newline at end of file diff --git a/sample-plugin/src/main/groovy/net/projecttl/plugin/sample/CorePlugin.groovy b/sample-plugin/src/main/groovy/net/projecttl/plugin/sample/CorePlugin.groovy new file mode 100644 index 0000000..59455eb --- /dev/null +++ b/sample-plugin/src/main/groovy/net/projecttl/plugin/sample/CorePlugin.groovy @@ -0,0 +1,16 @@ +package net.projecttl.plugin.sample + +import net.projecttl.p.x32.api.Plugin +import net.projecttl.plugin.sample.command.Greeting + +class CorePlugin extends Plugin { + @Override + void onLoad() { + logger.info "Hello, World!" + commandContainer.addCommand new Greeting() + } + + @Override + void destroy() { + } +} diff --git a/sample-plugin/src/main/groovy/net/projecttl/plugin/sample/command/Greeting.groovy b/sample-plugin/src/main/groovy/net/projecttl/plugin/sample/command/Greeting.groovy new file mode 100644 index 0000000..7a57d72 --- /dev/null +++ b/sample-plugin/src/main/groovy/net/projecttl/plugin/sample/command/Greeting.groovy @@ -0,0 +1,23 @@ +package net.projecttl.plugin.sample.command + +import kotlin.Unit +import kotlin.coroutines.Continuation +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 + +class Greeting implements GlobalCommand { + @Override + CommandData getData() { + return CommandData.fromData( + new CommandDataImpl("greeting", "greeting for user!").toData() + ) + } + + @Override + Object execute(SlashCommandInteractionEvent ev, Continuation $completion) { + ev.reply("Hello, ${ev.user.globalName == null ? ev.user.name : ev.user.globalName}").queue() + return null + } +} diff --git a/sample-plugin/src/main/resources/plugin.json b/sample-plugin/src/main/resources/plugin.json new file mode 100644 index 0000000..e20a501 --- /dev/null +++ b/sample-plugin/src/main/resources/plugin.json @@ -0,0 +1,5 @@ +{ + "name": "${project.name}", + "version": "${project.version}", + "main": "net.projecttl.plugin.sample.CorePlugin" +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 8c27b04..f4c99ca 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,3 +2,4 @@ rootProject.name = "px32-bot" include("px32-bot-core") include("px32-bot-api") include("px32-bot-func") +include("sample-plugin")