add alert commands

This commit is contained in:
dalbodeule 2024-06-17 16:21:34 +09:00
parent 3f60348ace
commit fbb0e50379
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
5 changed files with 66 additions and 7 deletions

View File

@ -25,6 +25,7 @@ val chzzkHandler = ChzzkHandler
fun main(args: Array<String>) {
discord.enable()
chzzkHandler.enable()
chzzkHandler.runStreamInfo()
if(dotenv.get("RUN_AGENT", "false").toBoolean()) {
runBlocking {
@ -35,6 +36,7 @@ fun main(args: Array<String>) {
Runtime.getRuntime().addShutdownHook(Thread {
logger.info("Shutting down...")
chzzkHandler.stopStreamInfo()
chzzkHandler.disable()
discord.disable()
connector.dataSource.close()

View File

@ -44,6 +44,14 @@ object ChzzkHandler {
throw RuntimeException("${chzzkChannel.channelName} doesn't have handler")
}
internal fun reloadUser(chzzkChannel: ChzzkChannel, user: User) {
val handler = handlers.firstOrNull { it.channel.channelId == chzzkChannel.channelId }
if (handler != null)
handler.reloadUser(user)
else
throw RuntimeException("${chzzkChannel.channelName} doesn't have handler")
}
internal fun runStreamInfo() {
running = true
Thread {
@ -67,7 +75,7 @@ object ChzzkHandler {
class UserHandler(
val channel: ChzzkChannel,
private val logger: Logger,
private val user: User,
private var user: User,
private var _isActive: Boolean = false
) {
private lateinit var messageHandler: MessageHandler
@ -109,6 +117,10 @@ class UserHandler(
messageHandler.reloadCommand()
}
internal fun reloadUser(user: User) {
this.user = user
}
internal val isActive: Boolean
get() = _isActive

View File

@ -4,7 +4,6 @@ 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.entities.channel.concrete.TextChannel
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import space.mori.chzzk_bot.dotenv
@ -18,6 +17,7 @@ class Discord: ListenerAdapter() {
private val commands = listOf(
AddCommand,
AlertCommand,
PingCommand,
RegisterCommand,
RemoveCommand,

View File

@ -0,0 +1,44 @@
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.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.chzzk.ChzzkHandler
import space.mori.chzzk_bot.chzzk.Connector
import space.mori.chzzk_bot.discord.CommandInterface
import space.mori.chzzk_bot.services.UserService
object AlertCommand : CommandInterface {
private val logger = LoggerFactory.getLogger(this::class.java)
override val name: String = "add"
override val command = Commands.slash(name, "명령어를 추가합니다.")
.addOptions(OptionData(OptionType.CHANNEL, "channel", "알림을 보낼 채널을 입력하세요."))
.addOptions(OptionData(OptionType.STRING, "content", "표시될 텍스트를 입력하세요. 비워두면 알람이 취소됩니다."))
override fun run(event: SlashCommandInteractionEvent, bot: JDA) {
val channel = event.getOption("channel")?.asChannel
val content = event.getOption("content")?.asString
val user = UserService.getUser(event.user.idLong)
if(user == null) {
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
return
}
val chzzkChannel = Connector.getChannel(user.token)
try {
val newUser = UserService.updateLiveAlert(user.id.value, channel?.guild?.idLong ?: 0L, channel?.idLong ?: 0L, content ?: "")
try {
ChzzkHandler.reloadUser(chzzkChannel!!, newUser)
} catch (_: Exception) {}
event.hook.sendMessage("업데이트가 완료되었습니다.").queue()
} catch (e: Exception) {
event.hook.sendMessage("에러가 발생했습니다.").queue()
logger.debug(e.stackTraceToString())
}
}
}

View File

@ -45,17 +45,18 @@ object UserService {
}
}
fun updateLiveAlert(id: Int, guildId: Long, channelId: Long, alertMessage: String?) {
fun updateLiveAlert(id: Int, guildId: Long, channelId: Long, alertMessage: String?): User {
return transaction {
val updated = Users.update({ Users.id eq id }) {
it[Users.liveAlertGuild] = guildId
it[Users.liveAlertChannel] = channelId
it[Users.liveAlertMessage] = alertMessage ?: ""
it[liveAlertGuild] = guildId
it[liveAlertChannel] = channelId
it[liveAlertMessage] = alertMessage ?: ""
}
if(updated == 0) throw RuntimeException("User not found! $id")
val users = User.find { Users.id eq id }
User.find(Users.id eq id)
return@transaction users.first()
}
}
}