mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
add alert commands
This commit is contained in:
parent
3f60348ace
commit
fbb0e50379
@ -25,6 +25,7 @@ val chzzkHandler = ChzzkHandler
|
|||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
discord.enable()
|
discord.enable()
|
||||||
chzzkHandler.enable()
|
chzzkHandler.enable()
|
||||||
|
chzzkHandler.runStreamInfo()
|
||||||
|
|
||||||
if(dotenv.get("RUN_AGENT", "false").toBoolean()) {
|
if(dotenv.get("RUN_AGENT", "false").toBoolean()) {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
@ -35,6 +36,7 @@ fun main(args: Array<String>) {
|
|||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(Thread {
|
Runtime.getRuntime().addShutdownHook(Thread {
|
||||||
logger.info("Shutting down...")
|
logger.info("Shutting down...")
|
||||||
|
chzzkHandler.stopStreamInfo()
|
||||||
chzzkHandler.disable()
|
chzzkHandler.disable()
|
||||||
discord.disable()
|
discord.disable()
|
||||||
connector.dataSource.close()
|
connector.dataSource.close()
|
||||||
|
@ -44,6 +44,14 @@ object ChzzkHandler {
|
|||||||
throw RuntimeException("${chzzkChannel.channelName} doesn't have handler")
|
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() {
|
internal fun runStreamInfo() {
|
||||||
running = true
|
running = true
|
||||||
Thread {
|
Thread {
|
||||||
@ -67,7 +75,7 @@ object ChzzkHandler {
|
|||||||
class UserHandler(
|
class UserHandler(
|
||||||
val channel: ChzzkChannel,
|
val channel: ChzzkChannel,
|
||||||
private val logger: Logger,
|
private val logger: Logger,
|
||||||
private val user: User,
|
private var user: User,
|
||||||
private var _isActive: Boolean = false
|
private var _isActive: Boolean = false
|
||||||
) {
|
) {
|
||||||
private lateinit var messageHandler: MessageHandler
|
private lateinit var messageHandler: MessageHandler
|
||||||
@ -109,6 +117,10 @@ class UserHandler(
|
|||||||
messageHandler.reloadCommand()
|
messageHandler.reloadCommand()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun reloadUser(user: User) {
|
||||||
|
this.user = user
|
||||||
|
}
|
||||||
|
|
||||||
internal val isActive: Boolean
|
internal val isActive: Boolean
|
||||||
get() = _isActive
|
get() = _isActive
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import net.dv8tion.jda.api.JDA
|
|||||||
import net.dv8tion.jda.api.JDABuilder
|
import net.dv8tion.jda.api.JDABuilder
|
||||||
import net.dv8tion.jda.api.entities.Activity
|
import net.dv8tion.jda.api.entities.Activity
|
||||||
import net.dv8tion.jda.api.entities.Guild
|
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.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||||
import space.mori.chzzk_bot.dotenv
|
import space.mori.chzzk_bot.dotenv
|
||||||
@ -18,6 +17,7 @@ class Discord: ListenerAdapter() {
|
|||||||
|
|
||||||
private val commands = listOf(
|
private val commands = listOf(
|
||||||
AddCommand,
|
AddCommand,
|
||||||
|
AlertCommand,
|
||||||
PingCommand,
|
PingCommand,
|
||||||
RegisterCommand,
|
RegisterCommand,
|
||||||
RemoveCommand,
|
RemoveCommand,
|
||||||
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
return transaction {
|
||||||
val updated = Users.update({ Users.id eq id }) {
|
val updated = Users.update({ Users.id eq id }) {
|
||||||
it[Users.liveAlertGuild] = guildId
|
it[liveAlertGuild] = guildId
|
||||||
it[Users.liveAlertChannel] = channelId
|
it[liveAlertChannel] = channelId
|
||||||
it[Users.liveAlertMessage] = alertMessage ?: ""
|
it[liveAlertMessage] = alertMessage ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if(updated == 0) throw RuntimeException("User not found! $id")
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user