add discord hook logics

- add ApiDiscordRoutes.kt
- modify HookCommand from RegisterCommand.kt
This commit is contained in:
dalbodeule
2024-08-10 16:54:32 +09:00
parent 6d7a6beb10
commit 2d0e83efc2
7 changed files with 136 additions and 66 deletions

View File

@@ -61,7 +61,7 @@ class Discord: ListenerAdapter() {
AddCommand,
AlertCommand,
PingCommand,
RegisterCommand,
HookComand,
RemoveCommand,
UpdateCommand,
AddManagerCommand,

View File

@@ -0,0 +1,61 @@
package space.mori.chzzk_bot.chatbot.discord.commands
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.Commands
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import org.koin.java.KoinJavaComponent.inject
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
import space.mori.chzzk_bot.common.events.CoroutinesEventBus
import space.mori.chzzk_bot.common.events.DiscordRegisterEvent
import space.mori.chzzk_bot.common.services.UserService
import java.util.concurrent.ConcurrentHashMap
object HookComand: CommandInterface {
private val logger = LoggerFactory.getLogger(this::class.java)
override val name = "hook"
override val command = Commands.slash(name, "디스코드 계정과 서버를 등록합니다.")
.addOptions(OptionData(OptionType.STRING, "token", "디스코드 연동 토큰을 입력해주세요."))
// key: Token, value: ChzzkID
private val hookMap = ConcurrentHashMap<String, String>()
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
init {
dispatcher.subscribe(DiscordRegisterEvent::class) {
hookMap[it.token] = it.user
}
}
override fun run(event: SlashCommandInteractionEvent, bot: JDA) {
val token = event.getOption("token")?.asString
if(token == null) {
event.hook.sendMessage("Token은 필수 입력입니다.").queue()
return
}
val user = UserService.getUser(hookMap[token] ?: "")
if (user == null) {
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
return
}
if(event.guild == null) {
event.hook.sendMessage("이 명령어는 디스코드 서버 안에서 실행해야 합니다.").queue()
return
}
try {
UserService.updateUser(user, event.user.idLong)
UserService.updateLiveAlert(user, event.guild!!.idLong, event.channelIdLong, "")
hookMap.remove(token)
event.hook.sendMessage("등록이 완료되었습니다. `${user.username}`")
} catch(e: Exception) {
event.hook.sendMessage("에러가 발생했습니다.").queue()
logger.debug(e.stackTraceToString())
}
}
}

View File

@@ -1,54 +0,0 @@
package space.mori.chzzk_bot.chatbot.discord.commands
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.Commands
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
import space.mori.chzzk_bot.chatbot.chzzk.Connector
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
import space.mori.chzzk_bot.common.services.UserService
object RegisterCommand: CommandInterface {
private val logger = LoggerFactory.getLogger(this::class.java)
override val name = "register"
private val regex = """(?:.+chzzk\.naver\.com/)?([a-f0-9]{32})?(?:/live)?${'$'}""".toRegex()
override val command = Commands.slash(name, "치지직 계정을 등록합니다.")
override fun run(event: SlashCommandInteractionEvent, bot: JDA) {
event.hook.sendMessage("가입은 여기를 참고해주세요!\nhttps://nabot.mori.space/register").queue()
/* val chzzkID = event.getOption("chzzk_id")?.asString
if(chzzkID == null) {
event.hook.sendMessage("치지직 계정은 필수 입력입니다.").queue()
return
}
val matchResult = regex.find(chzzkID)
val matchedChzzkId = matchResult?.groups?.get(1)?.value
val chzzkChannel = matchedChzzkId?.let { Connector.getChannel(it) }
if (chzzkChannel == null) {
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
return
}
try {
val user = UserService.saveUser(chzzkChannel.channelName, chzzkChannel.channelId, event.user.idLong)
CoroutineScope(Dispatchers.Main).launch {
ChzzkHandler.addUser(chzzkChannel, user)
}
event.hook.sendMessage("등록이 완료되었습니다. `${chzzkChannel.channelId}` - `${chzzkChannel.channelName}`")
} catch(e: Exception) {
event.hook.sendMessage("에러가 발생했습니다.").queue()
logger.debug(e.stackTraceToString())
} */
}
}