add ping command, chzzk Connector.kt

This commit is contained in:
dalbodeule
2024-06-12 19:52:58 +09:00
parent 315a61aecf
commit 294bf04a50
9 changed files with 148 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import kotlinx.coroutines.runBlocking
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.discord.Discord
import space.mori.chzzk_bot.chzzk.Connector as ChzzkConnector
import java.util.concurrent.TimeUnit
val dotenv = dotenv()
@@ -15,6 +16,8 @@ fun main(args: Array<String>) {
val discord = Discord()
Connector
ChzzkConnector
discord.enable()
if(dotenv.get("RUN_AGENT", "false").toBoolean()) {

View File

@@ -0,0 +1,14 @@
package space.mori.chzzk_bot.chzzk
import io.github.cdimascio.dotenv.dotenv
import xyz.r2turntrue.chzzk4j.Chzzk
import xyz.r2turntrue.chzzk4j.ChzzkBuilder
object Connector {
private val dotenv = dotenv()
val chzzk: Chzzk = ChzzkBuilder()
.withAuthorization(dotenv["NID_AUT"], dotenv["NID_SES"])
.build()
fun getChannel(channelId: String) = chzzk.getChannel(channelId)
}

View File

@@ -0,0 +1,35 @@
package space.mori.chzzk_bot.discord
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.build.CommandData
import org.reflections.Reflections
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Command
interface CommandInterface {
val name: String
fun run(event: SlashCommandInteractionEvent, bot: JDA): Unit
val command: CommandData
}
fun getCommands(): List<CommandInterface> {
val commandList = mutableListOf<CommandInterface>()
val packageName = "space.mori.chzzk_bot.discord.commands"
val reflections = Reflections(packageName)
val annotatedClasses = reflections.getTypesAnnotatedWith(Command::class.java)
for(clazz in annotatedClasses) {
val obj = clazz.kotlin.objectInstance
if(obj is CommandInterface) {
commandList.add(obj)
} else {
throw IllegalStateException("${clazz.name} is not a CommandInterface")
}
}
return commandList.toList()
}

View File

@@ -4,37 +4,49 @@ 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 net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
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
class Discord: ListenerAdapter() {
private lateinit var bot: JDA
private var guild: Guild? = null
private val commands = getCommands()
override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {
event.deferReply().queue()
commands.find { it.name == event.name }?.run(event, bot)
}
internal fun enable() {
try {
val thread = thread {
val thread = Thread {
try {
bot = JDABuilder.createDefault(dotenv["DISCORD_TOKEN"])
.setActivity(Activity.playing("치지직 보는중"))
.addEventListeners(this)
.build().awaitReady()
guild = bot.getGuildById(dotenv["GUILD_ID"])
bot.updateCommands().addCommands(
Commands.slash("ping", "Pong!")
).queue()
bot.updateCommands()
.addCommands(* commands.map { it.command }.toTypedArray())
.onSuccess { logger.info("Command update success!") }
.queue()
if (guild == null) {
logger.info("No guild found!")
this.disable()
}
} catch (e: Exception) {
logger.info("Could not enable Discord!")
logger.debug(e.stackTraceToString())
}
} catch(e: Exception) {
logger.info("Could not enable Discord!")
logger.debug(e.stackTraceToString())
}
thread.start()
}
internal fun disable() {

View File

@@ -0,0 +1,17 @@
package space.mori.chzzk_bot.discord.commands
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.build.Commands
import space.mori.chzzk_bot.discord.Command
import space.mori.chzzk_bot.discord.CommandInterface
@Command()
object Ping: CommandInterface {
override val command = Commands.slash("ping", "봇이 살아있을까요?")
override val name = "ping"
override fun run(event: SlashCommandInteractionEvent, bot: JDA) {
event.hook.sendMessage("${event.user.asMention} Pong!").queue()
}
}