mirror of
https://github.com/devproje/px32-bot.git
synced 2024-11-26 02:33:05 +00:00
feat: sample plugin
This commit is contained in:
parent
34426e5975
commit
c889cbe2d8
9 changed files with 92 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
40
sample-plugin/build.gradle.kts
Normal file
40
sample-plugin/build.gradle.kts
Normal 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()
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
5
sample-plugin/src/main/resources/plugin.json
Normal file
5
sample-plugin/src/main/resources/plugin.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "${project.name}",
|
||||
"version": "${project.version}",
|
||||
"main": "net.projecttl.plugin.sample.CorePlugin"
|
||||
}
|
|
@ -2,3 +2,4 @@ rootProject.name = "px32-bot"
|
|||
include("px32-bot-core")
|
||||
include("px32-bot-api")
|
||||
include("px32-bot-func")
|
||||
include("sample-plugin")
|
||||
|
|
Loading…
Reference in a new issue