Skip to content

Commit

Permalink
stream info add
Browse files Browse the repository at this point in the history
  • Loading branch information
dalbodeule committed Jun 17, 2024
1 parent 09bb485 commit 3f60348
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/main/kotlin/space/mori/chzzk_bot/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ val dotenv = dotenv {
}
val logger: Logger = LoggerFactory.getLogger("main")

fun main(args: Array<String>) {
val discord = Discord()
val discord = Discord()

val connector = Connector
val chzzkConnector = ChzzkConnector
val chzzkHandler = ChzzkHandler
val connector = Connector
val chzzkConnector = ChzzkConnector
val chzzkHandler = ChzzkHandler

fun main(args: Array<String>) {
discord.enable()
chzzkHandler.enable()

Expand Down
61 changes: 60 additions & 1 deletion src/main/kotlin/space/mori/chzzk_bot/chzzk/ChzzkHandler.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package space.mori.chzzk_bot.chzzk

import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.chzzk.Connector.chzzk
import space.mori.chzzk_bot.discord
import space.mori.chzzk_bot.models.User
import space.mori.chzzk_bot.services.UserService
import xyz.r2turntrue.chzzk4j.chat.ChatEventListener
import xyz.r2turntrue.chzzk4j.chat.ChatMessage
import xyz.r2turntrue.chzzk4j.chat.ChzzkChat
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
import java.lang.Exception
import java.time.Instant

object ChzzkHandler {
private val handlers = mutableListOf<UserHandler>()
private val logger = LoggerFactory.getLogger(this::class.java)
@Volatile private var running: Boolean = false

internal fun addUser(chzzkChannel: ChzzkChannel, user: User) {
handlers.add(UserHandler(chzzkChannel, logger, user))
Expand All @@ -38,10 +43,32 @@ object ChzzkHandler {
else
throw RuntimeException("${chzzkChannel.channelName} doesn't have handler")
}

internal fun runStreamInfo() {
running = true
Thread {
while(running) {
handlers.forEach {
if (!running) return@forEach
val streamInfo = getStreamInfo(it.channel.channelId)
if(streamInfo.content.status == "OPEN" && !it.isActive) it.isActive(true, streamInfo)
if(streamInfo.content.status == "CLOSED" && it.isActive) it.isActive(false, streamInfo)
Thread.sleep(3000)
}
}
}.start()
}

internal fun stopStreamInfo() {
running = false
}
}

class UserHandler(
val channel: ChzzkChannel, private val logger: Logger, private val user: User
val channel: ChzzkChannel,
private val logger: Logger,
private val user: User,
private var _isActive: Boolean = false
) {
private lateinit var messageHandler: MessageHandler

Expand Down Expand Up @@ -81,4 +108,36 @@ class UserHandler(
internal fun reloadCommand() {
messageHandler.reloadCommand()
}

internal val isActive: Boolean
get() = _isActive

internal fun isActive(value: Boolean, status: IData<IStreamInfo>) {
_isActive = value
if(value) {
logger.info("${user.username} is live.")
if(user.liveAlertMessage != "" && user.liveAlertGuild != null && user.liveAlertChannel != null) {
val channel = discord.getChannel(user.liveAlertGuild!!, user.liveAlertGuild!!) ?: throw RuntimeException("${user.liveAlertChannel} is not valid.")

val embed = EmbedBuilder()
embed.setTitle(status.content.liveTitle, "https://chzzk.naver.com/live/${user.token}")
embed.setDescription("${user.username} 님이 방송을 시작했습니다.")
embed.setUrl(status.content.channel.channelImageUrl)
embed.setTimestamp(Instant.now())
embed.setAuthor(user.username, "https://chzzk.naver.com/live/${user.token}", status.content.channel.channelImageUrl)
embed.addField("카테고리", status.content.liveCategoryValue, true)
embed.addField("태그", status.content.tags.joinToString(", "), true)
embed.setImage(status.content.liveImageUrl)

channel.sendMessage(
MessageCreateBuilder()
.setContent(user.liveAlertMessage)
.setEmbeds(embed.build())
.build()
).queue()
}
} else {
logger.info("${user.username} is offline.")
}
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/space/mori/chzzk_bot/discord/Discord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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
Expand Down Expand Up @@ -69,4 +70,7 @@ class Discord: ListenerAdapter() {
logger.debug(e.stackTraceToString())
}
}
}

internal fun getChannel(guildId: Long, channelId: Long) =
bot.getGuildById(guildId)?.getTextChannelById(channelId)
}
6 changes: 6 additions & 0 deletions src/main/kotlin/space/mori/chzzk_bot/models/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ object Users: IntIdTable("users") {
val username = varchar("username", 255)
val token = varchar("token", 64)
val discord = long("discord")
val liveAlertGuild = long("live_alert_guild").nullable()
val liveAlertChannel = long("live_alert_channel").nullable()
val liveAlertMessage = text("live_alert_message").nullable()
}

class User(id: EntityID<Int>) : IntEntity(id) {
Expand All @@ -18,4 +21,7 @@ class User(id: EntityID<Int>) : IntEntity(id) {
var username by Users.username
var token by Users.token
var discord by Users.discord
var liveAlertGuild by Users.liveAlertGuild
var liveAlertChannel by Users.liveAlertChannel
var liveAlertMessage by Users.liveAlertMessage
}
15 changes: 15 additions & 0 deletions src/main/kotlin/space/mori/chzzk_bot/services/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package space.mori.chzzk_bot.services

import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import space.mori.chzzk_bot.models.User
import space.mori.chzzk_bot.models.Users

Expand Down Expand Up @@ -43,4 +44,18 @@ object UserService {
User.all().toList()
}
}

fun updateLiveAlert(id: Int, guildId: Long, channelId: Long, alertMessage: String?) {
return transaction {
val updated = Users.update({ Users.id eq id }) {
it[Users.liveAlertGuild] = guildId
it[Users.liveAlertChannel] = channelId
it[Users.liveAlertMessage] = alertMessage ?: ""
}

if(updated == 0) throw RuntimeException("User not found! $id")

User.find(Users.id eq id)
}
}
}

0 comments on commit 3f60348

Please sign in to comment.