add JDA activation codes with graalvm native image configures

This commit is contained in:
dalbodeule 2024-06-12 00:35:41 +09:00
parent 8d47bc0bd4
commit d398f4f1a0
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
7 changed files with 88 additions and 1 deletions

2
.gitignore vendored
View File

@ -241,4 +241,6 @@ gradle-app.setting
# Java heap dump
*.hprof
.env
# End of https://www.toptal.com/developers/gitignore/api/macos,windows,linux,kotlin,gradle,intellij

View File

@ -3,6 +3,7 @@
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$/../../src/main" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/../../src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/../../src/main/resources" type="java-resource" />
</content>
</component>
</module>

View File

@ -14,11 +14,23 @@ java {
sourceCompatibility = JavaVersion.VERSION_21
}
kotlin {
jvmToolchain(21)
}
application {
mainClass.set("${"${project.group}.${project.name}".lowercase()}.MainKt")
}
graalvmNative {
agent {
trackReflectionMetadata.set(true)
metadataCopy {
outputDirectories.add("src/main/resources/META-INF/native-image")
mergeWithExisting.set(true)
}
}
binaries {
binaries.all {
resources.autodetect()
@ -28,6 +40,9 @@ graalvmNative {
sharedLibrary.set(false)
}
}
metadataRepository {
enabled.set(true)
}
}
repositories {
@ -41,6 +56,9 @@ dependencies {
}
// https://mvnrepository.com/artifact/io.github.R2turnTrue/chzzk4j
implementation("io.github.R2turnTrue:chzzk4j:0.0.7")
implementation("ch.qos.logback:logback-classic:1.4.14")
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
kotlin("stdlib-jdk8")
}

2
inc.env Normal file
View File

@ -0,0 +1,2 @@
DISCORD_TOKEN=
GUILD_ID

6
reflect-config.json Normal file
View File

@ -0,0 +1,6 @@
[
{
"name": "net.dv8tion.jda.api.entities.Guild[]",
"unsafeAllocated": true
}
]

View File

@ -1,5 +1,15 @@
package space.mori.chzzk_bot
import io.github.cdimascio.dotenv.dotenv
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.discord.Discord
val dotenv = dotenv()
val logger: Logger = LoggerFactory.getLogger("main")
fun main(args: Array<String>) {
println(args.joinToString("/"))
val discord = Discord()
discord.enable()
}

View File

@ -0,0 +1,48 @@
package space.mori.chzzk_bot.discord
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.interactions.commands.build.Commands
import space.mori.chzzk_bot.dotenv
import space.mori.chzzk_bot.logger
import kotlin.concurrent.thread
class Discord {
lateinit var bot: JDA
var guild: Guild? = null
internal fun enable() {
try {
val thread = thread {
bot = JDABuilder.createDefault(dotenv["DISCORD_TOKEN"])
.setActivity(Activity.playing("치지직 보는중"))
.build().awaitReady()
guild = bot.getGuildById(dotenv["GUILD_ID"])
bot.updateCommands().addCommands(
Commands.slash("ping", "Pong!")
).queue()
if (guild == null) {
logger.info("No guild found!")
this.disable()
}
}
} catch(e: Exception) {
logger.info("Could not enable Discord!")
logger.debug(e.stackTraceToString())
}
}
internal fun disable() {
try {
bot.shutdown()
} catch(e: Exception) {
logger.info("Error while shutting down Discord!")
logger.debug(e.stackTraceToString())
}
}
}