feat: sample plugin

This commit is contained in:
Project_IO 2024-09-20 10:34:58 +09:00
parent 34426e5975
commit c889cbe2d8
9 changed files with 92 additions and 3 deletions

View file

@ -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

View file

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory
abstract class Plugin {
private val globalCommandHandler = CommandHandler()
private val handlerContainer = mutableListOf<ListenerAdapter>(globalCommandHandler)
private val handlerContainer = mutableListOf<ListenerAdapter>()
private val config = this.javaClass.getResourceAsStream("/plugin.json")?.let {
val raw = it.bufferedReader().readText()
val obj = Json.decodeFromString<PluginConfig>(raw)

View file

@ -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<CommandExecutor> {
return commands
}
fun register(jda: JDA) {
val guild = jda.getGuildById(guildId)
if (guildId != 0L) {

View file

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

View file

@ -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<GroovyCompile> {
options.encoding = "UTF-8"
}
processResources {
filesMatching("plugin.json") {
expand(project.properties)
}
}
shadowJar {
archiveBaseName.set(project.name)
archiveClassifier.set("")
archiveVersion.set("")
}
test {
useJUnitPlatform()
}
}

View file

@ -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() {
}
}

View file

@ -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<? super Unit> $completion) {
ev.reply("Hello, ${ev.user.globalName == null ? ev.user.name : ev.user.globalName}").queue()
return null
}
}

View file

@ -0,0 +1,5 @@
{
"name": "${project.name}",
"version": "${project.version}",
"main": "net.projecttl.plugin.sample.CorePlugin"
}

View file

@ -2,3 +2,4 @@ rootProject.name = "px32-bot"
include("px32-bot-core")
include("px32-bot-api")
include("px32-bot-func")
include("sample-plugin")