mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-07 12:51:13 +00:00
Compare commits
84 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2f5c4293c0 | ||
|
0317c80ccf | ||
|
8d50d1c3aa | ||
|
de30e632e5 | ||
|
5d0fe98c66 | ||
|
608ba49a71 | ||
|
5a46e62a61 | ||
|
833bfbd46d | ||
|
76ffebc157 | ||
|
e8bee6ff23 | ||
|
60523f992b | ||
|
a10579ea6b | ||
|
5dcbbdeb80 | ||
|
bedd5406a0 | ||
|
a50a3f21a3 | ||
|
c9ad739d05 | ||
|
d329b8bdb3 | ||
|
816c82e57b | ||
|
debbd61aa7 | ||
|
3e3a283a16 | ||
|
c5f8ce7528 | ||
|
7df2b68a5f | ||
|
778fe8df34 | ||
|
4c5fa5742f | ||
|
4f589780b8 | ||
|
d5dc7a61c9 | ||
|
9b046d38b9 | ||
|
8e2c0243bb | ||
|
75da1c1576 | ||
|
fdd8eeda23 | ||
|
cc23ac03e7 | ||
|
240503a4d5 | ||
|
0a4e8193bb | ||
|
59b90f2bcf | ||
|
47228394d5 | ||
|
04f6b14daa | ||
|
6da0662e2a | ||
|
5c93476c34 | ||
|
514ab14c3c | ||
|
13ce148fc5 | ||
|
4025cbceec | ||
|
b77a3d02c5 | ||
|
4d63022130 | ||
|
aad10a0f44 | ||
|
5a050f9d5c | ||
|
2f73204c6a | ||
|
37b5fda691 | ||
|
4f31d87b3b | ||
|
590c1203bd | ||
|
cc81e6d722 | ||
|
f7953778e1 | ||
|
b803aeca1d | ||
|
91573a4048 | ||
|
dc81bb09f2 | ||
|
60319bc6fa | ||
|
1be1b69425 | ||
|
5b19331890 | ||
|
d323bf28db | ||
|
b3da9db627 | ||
|
f7c68a56bc | ||
|
39237a35c9 | ||
|
55f6f5f94d | ||
|
65fb2ac3e3 | ||
|
e4a2d28b3c | ||
|
01778b801a | ||
|
8e9382cb3a | ||
|
070df6b68b | ||
|
65d491cc8e | ||
|
df0c301079 | ||
|
6b533bcee9 | ||
|
2989816a1a | ||
|
75b983a02b | ||
|
b92b11bf06 | ||
|
180dbc85bf | ||
|
53cefe5813 | ||
|
99ec9ba7a0 | ||
|
85ad7fe5ad | ||
|
a9ee40e936 | ||
|
da13e8b834 | ||
|
65ff475d0a | ||
|
e8baa393cf | ||
|
8365fa1767 | ||
|
e16a8b6fc0 | ||
|
72f98b024b |
2
.idea/.gitignore
generated
vendored
2
.idea/.gitignore
generated
vendored
@@ -6,3 +6,5 @@
|
|||||||
# Datasource local storage ignored files
|
# Datasource local storage ignored files
|
||||||
/dataSources/
|
/dataSources/
|
||||||
/dataSources.local.xml
|
/dataSources.local.xml
|
||||||
|
discord.xml
|
||||||
|
inspectionProfiles/Project_Default.xml
|
6
.idea/ktor.xml
generated
Normal file
6
.idea/ktor.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="KtorOptions">
|
||||||
|
<option name="updateOpenAPI" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
3
.idea/sqldialects.xml
generated
3
.idea/sqldialects.xml
generated
@@ -3,7 +3,4 @@
|
|||||||
<component name="SqlDialectMappings">
|
<component name="SqlDialectMappings">
|
||||||
<file url="PROJECT" dialect="MariaDB" />
|
<file url="PROJECT" dialect="MariaDB" />
|
||||||
</component>
|
</component>
|
||||||
<component name="SqlResolveMappings">
|
|
||||||
<file url="file://$PROJECT_DIR$/src/main/kotlin/space/mori/chzzk_bot/models/Command.kt" scope="{"node":{ "@negative":"1", "group":{ "@kind":"root", "node":{ "@negative":"1" } } }}" />
|
|
||||||
</component>
|
|
||||||
</project>
|
</project>
|
@@ -32,6 +32,15 @@
|
|||||||
- [x] !명령어추가 \[명령어] \[내용]
|
- [x] !명령어추가 \[명령어] \[내용]
|
||||||
- [x] !명령어수정 \[명령어] \[내용]
|
- [x] !명령어수정 \[명령어] \[내용]
|
||||||
- [x] !명령어삭제 \[명령어]
|
- [x] !명령어삭제 \[명령어]
|
||||||
|
### 타이머 명령어 (on Chzzk chat, 매니저/스트리머 전용)
|
||||||
|
- [x] !시간 \[숫자: 분]
|
||||||
|
- [x] !시간 업타임
|
||||||
|
- [x] !시간 삭제
|
||||||
|
### 플레이리스트 명령어 (on Chzzk chat)
|
||||||
|
- [x] !노래추가 \[유튜브 주소]
|
||||||
|
- [x] !노래목록
|
||||||
|
- [ ] !노래삭제 \[번호]
|
||||||
|
- [ ] !노래설정 \[내용] \[켜기/끄기]
|
||||||
|
|
||||||
### Envs
|
### Envs
|
||||||
- DISCORD_TOKEN
|
- DISCORD_TOKEN
|
||||||
|
@@ -5,7 +5,6 @@ plugins {
|
|||||||
id("application")
|
id("application")
|
||||||
kotlin("jvm") version kotlinVersion
|
kotlin("jvm") version kotlinVersion
|
||||||
kotlin("plugin.jpa") version kotlinVersion
|
kotlin("plugin.jpa") version kotlinVersion
|
||||||
id("org.graalvm.buildtools.native") version "0.10.2"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "${project.group}"
|
group = "${project.group}"
|
||||||
@@ -28,28 +27,9 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// https://mvnrepository.com/artifact/net.dv8tion/JDA
|
|
||||||
implementation("net.dv8tion:JDA:5.0.1") {
|
|
||||||
exclude(module = "opus-java")
|
|
||||||
}
|
|
||||||
// https://mvnrepository.com/artifact/io.github.R2turnTrue/chzzk4j
|
|
||||||
implementation("io.github.R2turnTrue:chzzk4j:0.0.9")
|
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
|
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
|
||||||
implementation("ch.qos.logback:logback-classic:1.5.6")
|
implementation("ch.qos.logback:logback-classic:1.5.6")
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-core
|
|
||||||
implementation("org.jetbrains.exposed:exposed-core:0.52.0")
|
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-dao
|
|
||||||
implementation("org.jetbrains.exposed:exposed-dao:0.52.0")
|
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-jdbc
|
|
||||||
implementation("org.jetbrains.exposed:exposed-jdbc:0.52.0")
|
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-kotlin-datetime
|
|
||||||
implementation("org.jetbrains.exposed:exposed-java-time:0.52.0")
|
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/com.zaxxer/HikariCP
|
|
||||||
implementation("com.zaxxer:HikariCP:5.1.0")
|
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core
|
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect
|
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect
|
||||||
@@ -58,13 +38,17 @@ dependencies {
|
|||||||
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||||
implementation("com.google.code.gson:gson:2.11.0")
|
implementation("com.google.code.gson:gson:2.11.0")
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
|
|
||||||
implementation("org.mariadb.jdbc:mariadb-java-client:3.4.1")
|
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/io.github.cdimascio/dotenv-kotlin
|
// https://mvnrepository.com/artifact/io.github.cdimascio/dotenv-kotlin
|
||||||
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
|
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/io.insert-koin/koin-core
|
||||||
|
implementation("io.insert-koin:koin-core:4.0.0-RC1")
|
||||||
|
|
||||||
kotlin("stdlib")
|
kotlin("stdlib")
|
||||||
|
|
||||||
|
listOf(project(":common"), project(":chatbot"), project(":webserver")).forEach {
|
||||||
|
implementation(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<Jar> {
|
tasks.withType<Jar> {
|
||||||
|
53
chatbot/build.gradle.kts
Normal file
53
chatbot/build.gradle.kts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm")
|
||||||
|
}
|
||||||
|
|
||||||
|
group = project.rootProject.group
|
||||||
|
version = project.rootProject.version
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// https://mvnrepository.com/artifact/net.dv8tion/JDA
|
||||||
|
api("net.dv8tion:JDA:5.0.1") {
|
||||||
|
exclude(module = "opus-java")
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/io.github.R2turnTrue/chzzk4j
|
||||||
|
implementation("io.github.R2turnTrue:chzzk4j:0.0.9")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
|
||||||
|
implementation("ch.qos.logback:logback-classic:1.5.6")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect
|
||||||
|
implementation("org.jetbrains.kotlin:kotlin-reflect:2.0.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||||
|
implementation("com.google.code.gson:gson:2.11.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/io.github.cdimascio/dotenv-kotlin
|
||||||
|
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/io.insert-koin/koin-core
|
||||||
|
implementation("io.insert-koin:koin-core:4.0.0-RC1")
|
||||||
|
|
||||||
|
testImplementation(kotlin("test"))
|
||||||
|
|
||||||
|
listOf(project(":common")).forEach {
|
||||||
|
implementation(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
kotlin {
|
||||||
|
jvmToolchain(21)
|
||||||
|
}
|
@@ -1,37 +1,51 @@
|
|||||||
package space.mori.chzzk_bot.chzzk
|
package space.mori.chzzk_bot.chatbot.chzzk
|
||||||
|
|
||||||
import net.dv8tion.jda.api.EmbedBuilder
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.Connector.chzzk
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector.chzzk
|
||||||
import space.mori.chzzk_bot.discord
|
import space.mori.chzzk_bot.chatbot.discord.Discord
|
||||||
import space.mori.chzzk_bot.models.User
|
import space.mori.chzzk_bot.common.events.*
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
import space.mori.chzzk_bot.common.services.LiveStatusService
|
||||||
|
import space.mori.chzzk_bot.common.services.TimerConfigService
|
||||||
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
import space.mori.chzzk_bot.common.utils.*
|
||||||
import xyz.r2turntrue.chzzk4j.chat.ChatEventListener
|
import xyz.r2turntrue.chzzk4j.chat.ChatEventListener
|
||||||
import xyz.r2turntrue.chzzk4j.chat.ChatMessage
|
import xyz.r2turntrue.chzzk4j.chat.ChatMessage
|
||||||
import xyz.r2turntrue.chzzk4j.chat.ChzzkChat
|
import xyz.r2turntrue.chzzk4j.chat.ChzzkChat
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
import java.net.SocketTimeoutException
|
import java.net.SocketTimeoutException
|
||||||
import java.time.Instant
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
object ChzzkHandler {
|
object ChzzkHandler {
|
||||||
private val handlers = mutableListOf<UserHandler>()
|
private val handlers = mutableListOf<UserHandler>()
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
|
lateinit var botUid: String
|
||||||
@Volatile private var running: Boolean = false
|
@Volatile private var running: Boolean = false
|
||||||
|
|
||||||
internal fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
||||||
handlers.add(UserHandler(chzzkChannel, logger, user))
|
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = null))
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun enable() {
|
fun enable() {
|
||||||
|
botUid = chzzk.loggedUser.userId
|
||||||
UserService.getAllUsers().map {
|
UserService.getAllUsers().map {
|
||||||
chzzk.getChannel(it.token)?.let { token -> addUser(token, it)}
|
chzzk.getChannel(it.token)?.let { token -> addUser(token, it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
handlers.forEach { handler ->
|
||||||
|
val streamInfo = getStreamInfo(handler.listener.channelId)
|
||||||
|
if (streamInfo.content?.status == "OPEN") handler.isActive(true, streamInfo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun disable() {
|
fun disable() {
|
||||||
handlers.forEach { handler ->
|
handlers.forEach { handler ->
|
||||||
handler.disable()
|
handler.disable()
|
||||||
}
|
}
|
||||||
@@ -53,7 +67,7 @@ object ChzzkHandler {
|
|||||||
throw RuntimeException("${chzzkChannel.channelName} doesn't have handler")
|
throw RuntimeException("${chzzkChannel.channelName} doesn't have handler")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun runStreamInfo() {
|
fun runStreamInfo() {
|
||||||
running = true
|
running = true
|
||||||
val thread = Thread({
|
val thread = Thread({
|
||||||
while(running) {
|
while(running) {
|
||||||
@@ -61,8 +75,8 @@ object ChzzkHandler {
|
|||||||
if (!running) return@forEach
|
if (!running) return@forEach
|
||||||
try {
|
try {
|
||||||
val streamInfo = getStreamInfo(it.channel.channelId)
|
val streamInfo = getStreamInfo(it.channel.channelId)
|
||||||
if (streamInfo.content.status == "OPEN" && !it.isActive) it.isActive(true, streamInfo)
|
if (streamInfo.content?.status == "OPEN" && !it.isActive) it.isActive(true, streamInfo)
|
||||||
if (streamInfo.content.status == "CLOSE" && it.isActive) it.isActive(false, streamInfo)
|
if (streamInfo.content?.status == "CLOSE" && it.isActive) it.isActive(false, streamInfo)
|
||||||
} catch(e: SocketTimeoutException) {
|
} catch(e: SocketTimeoutException) {
|
||||||
logger.info("Timeout: ${it.channel.channelName} / ${e.stackTraceToString()}")
|
logger.info("Timeout: ${it.channel.channelName} / ${e.stackTraceToString()}")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -78,25 +92,31 @@ object ChzzkHandler {
|
|||||||
thread.start()
|
thread.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun stopStreamInfo() {
|
fun stopStreamInfo() {
|
||||||
running = false
|
running = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UserHandler(
|
class UserHandler(
|
||||||
val channel: ChzzkChannel,
|
val channel: ChzzkChannel,
|
||||||
private val logger: Logger,
|
val logger: Logger,
|
||||||
private var user: User,
|
private var user: User,
|
||||||
private var _isActive: Boolean = false
|
var streamStartTime: LocalDateTime?,
|
||||||
) {
|
) {
|
||||||
private lateinit var messageHandler: MessageHandler
|
private lateinit var messageHandler: MessageHandler
|
||||||
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
private var _isActive: Boolean
|
||||||
|
get() = LiveStatusService.getLiveStatus(user)?.status ?: false
|
||||||
|
set(value) {
|
||||||
|
LiveStatusService.updateOrCreate(user, value)
|
||||||
|
}
|
||||||
|
|
||||||
private var listener: ChzzkChat = chzzk.chat(channel.channelId)
|
var listener: ChzzkChat = chzzk.chat(channel.channelId)
|
||||||
.withAutoReconnect(true)
|
.withAutoReconnect(true)
|
||||||
.withChatListener(object : ChatEventListener {
|
.withChatListener(object : ChatEventListener {
|
||||||
override fun onConnect(chat: ChzzkChat, isReconnecting: Boolean) {
|
override fun onConnect(chat: ChzzkChat, isReconnecting: Boolean) {
|
||||||
logger.info("ChzzkChat connected. ${channel.channelName} - ${channel.channelId} / reconnected: $isReconnecting")
|
logger.info("ChzzkChat connected. ${channel.channelName} - ${channel.channelId} / reconnected: $isReconnecting")
|
||||||
messageHandler = MessageHandler(channel, logger, chat)
|
messageHandler = MessageHandler(this@UserHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onError(ex: Exception) {
|
override fun onError(ex: Exception) {
|
||||||
@@ -131,39 +151,69 @@ class UserHandler(
|
|||||||
internal val isActive: Boolean
|
internal val isActive: Boolean
|
||||||
get() = _isActive
|
get() = _isActive
|
||||||
|
|
||||||
internal fun isActive(value: Boolean, status: IData<IStreamInfo>) {
|
internal fun isActive(value: Boolean, status: IData<IStreamInfo?>) {
|
||||||
_isActive = value
|
|
||||||
if(value) {
|
if(value) {
|
||||||
logger.info("${user.username} is live.")
|
logger.info("${user.username} is live.")
|
||||||
|
|
||||||
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
|
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
|
||||||
listener.connectAsync()
|
listener.connectBlocking()
|
||||||
|
|
||||||
if(user.liveAlertMessage != "" && user.liveAlertGuild != null && user.liveAlertChannel != null) {
|
streamStartTime = status.content?.openDate?.let { convertChzzkDateToLocalDateTime(it) }
|
||||||
val channel = discord.getChannel(user.liveAlertGuild!!, user.liveAlertChannel!!) ?: throw RuntimeException("${user.liveAlertChannel} is not valid.")
|
|
||||||
|
|
||||||
val embed = EmbedBuilder()
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
embed.setTitle(status.content.liveTitle, "https://chzzk.naver.com/live/${user.token}")
|
if(!_isActive) {
|
||||||
embed.setDescription("${user.username} 님이 방송을 시작했습니다.")
|
when(TimerConfigService.getConfig(UserService.getUser(channel.channelId)!!)?.option) {
|
||||||
embed.setUrl(status.content.channel.channelImageUrl)
|
TimerType.UPTIME.value -> dispatcher.post(
|
||||||
embed.setTimestamp(Instant.now())
|
TimerEvent(
|
||||||
embed.setAuthor(user.username, "https://chzzk.naver.com/live/${user.token}", status.content.channel.channelImageUrl)
|
channel.channelId,
|
||||||
embed.addField("카테고리", status.content.liveCategoryValue, true)
|
TimerType.UPTIME,
|
||||||
embed.addField("태그", status.content.tags.joinToString(", "), true)
|
getUptime(streamStartTime!!)
|
||||||
embed.setImage(status.content.liveImageUrl.replace("{type}", "1080"))
|
)
|
||||||
|
)
|
||||||
|
|
||||||
channel.sendMessage(
|
else -> dispatcher.post(
|
||||||
MessageCreateBuilder()
|
TimerEvent(
|
||||||
.setContent(user.liveAlertMessage)
|
channel.channelId,
|
||||||
.setEmbeds(embed.build())
|
TimerType.REMOVE,
|
||||||
.build()
|
""
|
||||||
).queue()
|
)
|
||||||
|
)
|
||||||
listener.sendChat("${user.username} 님의 방송이 감지되었습니다.")
|
}
|
||||||
|
delay(5000L)
|
||||||
|
try {
|
||||||
|
listener.sendChat("${user.username} 님! 오늘도 열심히 방송하세요!")
|
||||||
|
Discord.sendDiscord(user, status)
|
||||||
|
} catch(e: Exception) {
|
||||||
|
logger.info("Stream on logic has some error: ${e.stackTraceToString()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.info("${user.username} is offline.")
|
logger.info("${user.username} is offline.")
|
||||||
|
streamStartTime = null
|
||||||
listener.closeAsync()
|
listener.closeAsync()
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
val events = listOf(
|
||||||
|
TimerEvent(
|
||||||
|
channel.channelId,
|
||||||
|
TimerType.STREAM_OFF,
|
||||||
|
null
|
||||||
|
),
|
||||||
|
SongEvent(
|
||||||
|
channel.channelId,
|
||||||
|
SongType.STREAM_OFF,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
)
|
||||||
|
events.forEach { dispatcher.post(it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
_isActive = value
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,10 +1,14 @@
|
|||||||
package space.mori.chzzk_bot.chzzk
|
package space.mori.chzzk_bot.chatbot.chzzk
|
||||||
|
|
||||||
|
import io.github.cdimascio.dotenv.dotenv
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import xyz.r2turntrue.chzzk4j.Chzzk
|
import xyz.r2turntrue.chzzk4j.Chzzk
|
||||||
import xyz.r2turntrue.chzzk4j.ChzzkBuilder
|
import xyz.r2turntrue.chzzk4j.ChzzkBuilder
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
||||||
import space.mori.chzzk_bot.dotenv
|
|
||||||
|
val dotenv = dotenv {
|
||||||
|
ignoreIfMissing = true
|
||||||
|
}
|
||||||
|
|
||||||
object Connector {
|
object Connector {
|
||||||
val chzzk: Chzzk = ChzzkBuilder()
|
val chzzk: Chzzk = ChzzkBuilder()
|
@@ -0,0 +1,381 @@
|
|||||||
|
package space.mori.chzzk_bot.chatbot.chzzk
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
|
import space.mori.chzzk_bot.chatbot.discord.Discord.Companion.bot
|
||||||
|
import space.mori.chzzk_bot.common.events.*
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
import space.mori.chzzk_bot.common.services.*
|
||||||
|
import space.mori.chzzk_bot.common.utils.getFollowDate
|
||||||
|
import space.mori.chzzk_bot.common.utils.getUptime
|
||||||
|
import space.mori.chzzk_bot.common.utils.getYoutubeVideo
|
||||||
|
import xyz.r2turntrue.chzzk4j.chat.ChatMessage
|
||||||
|
import xyz.r2turntrue.chzzk4j.chat.ChzzkChat
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
|
||||||
|
class MessageHandler(
|
||||||
|
private val handler: UserHandler
|
||||||
|
) {
|
||||||
|
private val commands = mutableMapOf<String, (msg: ChatMessage, user: User) -> Unit>()
|
||||||
|
|
||||||
|
private val counterPattern = Regex("<counter:([^>]+)>")
|
||||||
|
private val personalCounterPattern = Regex("<counter_personal:([^>]+)>")
|
||||||
|
private val dailyCounterPattern = Regex("<daily_counter:([^>]+)>")
|
||||||
|
private val namePattern = Regex("<name>")
|
||||||
|
private val followPattern = Regex("<following>")
|
||||||
|
private val daysPattern = """<days:(\d{4})-(\d{2})-(\d{2})>""".toRegex()
|
||||||
|
|
||||||
|
private val channel = handler.channel
|
||||||
|
private val logger = handler.logger
|
||||||
|
private val listener = handler.listener
|
||||||
|
|
||||||
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
|
init {
|
||||||
|
reloadCommand()
|
||||||
|
dispatcher.subscribe(SongEvent::class) {
|
||||||
|
if(it.type == SongType.STREAM_OFF) {
|
||||||
|
val user = UserService.getUser(channel.channelId)
|
||||||
|
if(! user?.let { usr -> SongListService.getSong(usr) }.isNullOrEmpty()) {
|
||||||
|
SongListService.deleteUser(user!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun reloadCommand() {
|
||||||
|
val user = UserService.getUser(channel.channelId)
|
||||||
|
?: throw RuntimeException("User not found. it's bug? ${channel.channelName} - ${channel.channelId}")
|
||||||
|
val commands = CommandService.getCommands(user)
|
||||||
|
val manageCommands = mapOf(
|
||||||
|
"!명령어추가" to this::manageAddCommand,
|
||||||
|
"!명령어삭제" to this::manageRemoveCommand,
|
||||||
|
"!명령어수정" to this::manageUpdateCommand,
|
||||||
|
"!시간" to this::timerCommand,
|
||||||
|
"!노래추가" to this::songAddCommand,
|
||||||
|
"!노래목록" to this::songListCommand,
|
||||||
|
"!노래시작" to this::songStartCommand
|
||||||
|
)
|
||||||
|
|
||||||
|
manageCommands.forEach { (commandName, command) ->
|
||||||
|
this.commands[commandName] = command
|
||||||
|
}
|
||||||
|
|
||||||
|
commands.map {
|
||||||
|
this.commands.put(it.command.lowercase()) { msg, user ->
|
||||||
|
logger.debug("${channel.channelName} - ${it.command} - ${it.content}/${it.failContent}")
|
||||||
|
|
||||||
|
val result = replaceCounters(Pair(it.content, it.failContent), user, msg, listener, msg.profile?.nickname ?: "")
|
||||||
|
listener.sendChat(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun manageAddCommand(msg: ChatMessage, user: User) {
|
||||||
|
if (msg.profile?.userRoleCode == "common_user") {
|
||||||
|
listener.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val parts = msg.content.split(" ", limit = 3)
|
||||||
|
if (parts.size < 3) {
|
||||||
|
listener.sendChat("명령어 추가 형식은 '!명령어추가 명령어 내용'입니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (commands.containsKey(parts[1])) {
|
||||||
|
listener.sendChat("${parts[1]} 명령어는 이미 있는 명령어입니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val command = parts[1]
|
||||||
|
val content = parts[2]
|
||||||
|
CommandService.saveCommand(user, command, content, "")
|
||||||
|
listener.sendChat("명령어 '$command' 추가되었습니다.")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun manageUpdateCommand(msg: ChatMessage, user: User) {
|
||||||
|
if (msg.profile?.userRoleCode == "common_user") {
|
||||||
|
listener.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val parts = msg.content.split(" ", limit = 3)
|
||||||
|
if (parts.size < 3) {
|
||||||
|
listener.sendChat("명령어 수정 형식은 '!명령어수정 명령어 내용'입니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!commands.containsKey(parts[1])) {
|
||||||
|
listener.sendChat("${parts[1]} 명령어는 없는 명령어입니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val command = parts[1]
|
||||||
|
val content = parts[2]
|
||||||
|
CommandService.updateCommand(user, command, content, "")
|
||||||
|
listener.sendChat("명령어 '$command' 수정되었습니다.")
|
||||||
|
ChzzkHandler.reloadCommand(channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun manageRemoveCommand(msg: ChatMessage, user: User) {
|
||||||
|
if (msg.profile?.userRoleCode == "common_user") {
|
||||||
|
listener.sendChat("매니저만 명령어를 삭제할 수 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val parts = msg.content.split(" ", limit = 2)
|
||||||
|
if (parts.size < 2) {
|
||||||
|
listener.sendChat("명령어 삭제 형식은 '!명령어삭제 명령어'입니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val command = parts[1]
|
||||||
|
CommandService.removeCommand(user, command)
|
||||||
|
listener.sendChat("명령어 '$command' 삭제되었습니다.")
|
||||||
|
ChzzkHandler.reloadCommand(channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun timerCommand(msg: ChatMessage, user: User) {
|
||||||
|
if (msg.profile?.userRoleCode == "common_user") {
|
||||||
|
listener.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val parts = msg.content.split(" ", limit = 3)
|
||||||
|
if (parts.size < 2) {
|
||||||
|
listener.sendChat("타이머 명령어 형식을 잘 찾아봐주세요!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val command = parts[1]
|
||||||
|
when (parts[1]) {
|
||||||
|
"업타임" -> {
|
||||||
|
logger.debug("${user.token} / 업타임")
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
dispatcher.post(
|
||||||
|
TimerEvent(
|
||||||
|
user.token,
|
||||||
|
TimerType.UPTIME,
|
||||||
|
getUptime(handler.streamStartTime!!)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"삭제" -> {
|
||||||
|
logger.debug("${user.token} / 삭제")
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
dispatcher.post(TimerEvent(user.token, TimerType.REMOVE, ""))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"설정" -> {
|
||||||
|
when (parts[2]) {
|
||||||
|
"업타임" -> {
|
||||||
|
TimerConfigService.saveOrUpdateConfig(user, TimerType.UPTIME)
|
||||||
|
listener.sendChat("기본 타이머 설정이 업타임으로 바뀌었습니다.")
|
||||||
|
}
|
||||||
|
"삭제" -> {
|
||||||
|
TimerConfigService.saveOrUpdateConfig(user, TimerType.REMOVE)
|
||||||
|
listener.sendChat("기본 타이머 설정이 삭제로 바뀌었습니다.")
|
||||||
|
}
|
||||||
|
else -> listener.sendChat("!타이머 설정 (업타임/삭제) 형식으로 써주세요!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
logger.debug("${user.token} / 그외")
|
||||||
|
try {
|
||||||
|
val time = command.toInt()
|
||||||
|
val currentTime = LocalDateTime.now()
|
||||||
|
val timestamp = currentTime.plus(time.toLong(), ChronoUnit.MINUTES)
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
dispatcher.post(TimerEvent(user.token, TimerType.TIMER, timestamp.toString()))
|
||||||
|
}
|
||||||
|
} catch (e: NumberFormatException) {
|
||||||
|
listener.sendChat("!타이머/숫자 형식으로 적어주세요! 단위: 분")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
listener.sendChat("타이머 설정 중 오류가 발생했습니다.")
|
||||||
|
logger.error("Error processing timer command: ${e.message}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// songs
|
||||||
|
private fun songAddCommand(msg: ChatMessage, user: User) {
|
||||||
|
val parts = msg.content.split(" ", limit = 3)
|
||||||
|
if (parts.size < 2) {
|
||||||
|
listener.sendChat("유튜브 URL을 입력해주세요!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val config = SongConfigService.getConfig(user)
|
||||||
|
|
||||||
|
if(config.streamerOnly && msg.profile?.userRoleCode == "common_user") {
|
||||||
|
listener.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val url = parts[1]
|
||||||
|
val songs = SongListService.getSong(user)
|
||||||
|
|
||||||
|
if(songs.size >= config.queueLimit) {
|
||||||
|
listener.sendChat("더이상 노래를 신청할 수 없습니다. 잠시 뒤 다시 시도해주세요!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if(songs.filter { it.uid == msg.userId }.size >= config.personalLimit) {
|
||||||
|
listener.sendChat("더이상 노래를 신청할 수 없습니다. 잠시 뒤 다시 시도해주세요!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
val video = getYoutubeVideo(url)
|
||||||
|
if (video == null) {
|
||||||
|
listener.sendChat("유튜브에서 찾을 수 없어요!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (songs.any { it.url == video.url }) {
|
||||||
|
listener.sendChat("같은 노래가 이미 신청되어 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
SongListService.saveSong(
|
||||||
|
user,
|
||||||
|
msg.userId,
|
||||||
|
video.url,
|
||||||
|
video.name,
|
||||||
|
video.author,
|
||||||
|
video.length,
|
||||||
|
msg.profile?.nickname ?: ""
|
||||||
|
)
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
dispatcher.post(
|
||||||
|
SongEvent(
|
||||||
|
user.token,
|
||||||
|
SongType.ADD,
|
||||||
|
msg.userId,
|
||||||
|
msg.profile?.nickname ?: "",
|
||||||
|
video.name,
|
||||||
|
video.author,
|
||||||
|
video.length,
|
||||||
|
video.url
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.sendChat("노래가 추가되었습니다.")
|
||||||
|
} catch(e: Exception) {
|
||||||
|
listener.sendChat("유튜브 영상 주소로 다시 신청해주세요!")
|
||||||
|
logger.info(e.stackTraceToString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun songListCommand(msg: ChatMessage, user: User) {
|
||||||
|
listener.sendChat("리스트는 여기입니다. https://nabot.mori.space/songs/${user.token}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun songStartCommand(msg: ChatMessage, user: User) {
|
||||||
|
if (msg.profile?.userRoleCode == "common_user") {
|
||||||
|
listener.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val session = "${UUID.randomUUID()}${UUID.randomUUID()}".replace("-", "")
|
||||||
|
|
||||||
|
SongConfigService.updateSession(user, session)
|
||||||
|
|
||||||
|
|
||||||
|
bot.retrieveUserById(user.discord).queue { discordUser ->
|
||||||
|
discordUser?.openPrivateChannel()?.queue { channel ->
|
||||||
|
channel.sendMessage("여기로 접속해주세요! ||https://nabot.mori.space/songlist/${session}||.\n주소가 노출될 경우 방송을 다시 켜셔야 합니다!")
|
||||||
|
.queue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun handle(msg: ChatMessage, user: User) {
|
||||||
|
if(msg.userId == ChzzkHandler.botUid) return
|
||||||
|
|
||||||
|
val commandKey = msg.content.split(' ')[0]
|
||||||
|
commands[commandKey.lowercase()]?.let { it(msg, user) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun replaceCounters(chat: Pair<String, String>, user: User, msg: ChatMessage, listener: ChzzkChat, userName: String): String {
|
||||||
|
var result = chat.first
|
||||||
|
var isFail = false
|
||||||
|
|
||||||
|
// Replace dailyCounterPattern
|
||||||
|
result = dailyCounterPattern.replace(result) { matchResult ->
|
||||||
|
val name = matchResult.groupValues[1]
|
||||||
|
val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user)
|
||||||
|
|
||||||
|
if (dailyCounter.second) {
|
||||||
|
CounterService.updateDailyCounterValue(name, msg.userId, 1, user).first.toString()
|
||||||
|
} else {
|
||||||
|
isFail = true
|
||||||
|
dailyCounter.first.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle fail case
|
||||||
|
if (isFail && chat.second.isNotEmpty()) {
|
||||||
|
result = chat.second
|
||||||
|
result = dailyCounterPattern.replace(result) { matchResult ->
|
||||||
|
val name = matchResult.groupValues[1]
|
||||||
|
val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user)
|
||||||
|
dailyCounter.first.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace followPattern
|
||||||
|
result = followPattern.replace(result) { _ ->
|
||||||
|
try {
|
||||||
|
val followingDate = getFollowDate(listener.chatId, msg.userId)
|
||||||
|
.content?.streamingProperty?.following?.followDate
|
||||||
|
|
||||||
|
val period = followingDate?.let {
|
||||||
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||||
|
val pastDate = LocalDateTime.parse(it, formatter)
|
||||||
|
val today = LocalDateTime.now()
|
||||||
|
ChronoUnit.DAYS.between(pastDate, today)
|
||||||
|
} ?: 0
|
||||||
|
|
||||||
|
period.toString()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.error(e.message)
|
||||||
|
"0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace daysPattern
|
||||||
|
result = daysPattern.replace(result) { matchResult ->
|
||||||
|
val (year, month, day) = matchResult.destructured
|
||||||
|
val pastDate = LocalDateTime.of(year.toInt(), month.toInt(), day.toInt(), 0, 0, 0)
|
||||||
|
val today = LocalDateTime.now()
|
||||||
|
|
||||||
|
val daysBetween = ChronoUnit.DAYS.between(pastDate, today)
|
||||||
|
daysBetween.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace counterPattern
|
||||||
|
result = counterPattern.replace(result) { matchResult ->
|
||||||
|
val name = matchResult.groupValues[1]
|
||||||
|
CounterService.updateCounterValue(name, 1, user).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace personalCounterPattern
|
||||||
|
result = personalCounterPattern.replace(result) { matchResult ->
|
||||||
|
val name = matchResult.groupValues[1]
|
||||||
|
CounterService.updatePersonalCounterValue(name, msg.userId, 1, user).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace namePattern
|
||||||
|
result = namePattern.replace(result, userName)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,11 @@
|
|||||||
|
package space.mori.chzzk_bot.chatbot.chzzk
|
||||||
|
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
import space.mori.chzzk_bot.common.services.CommandService
|
||||||
|
import xyz.r2turntrue.chzzk4j.chat.ChatMessage
|
||||||
|
|
||||||
|
class SongModule {
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.discord
|
package space.mori.chzzk_bot.chatbot.discord
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
@@ -1,23 +1,62 @@
|
|||||||
package space.mori.chzzk_bot.discord
|
package space.mori.chzzk_bot.chatbot.discord
|
||||||
|
|
||||||
|
import io.github.cdimascio.dotenv.dotenv
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder
|
||||||
import net.dv8tion.jda.api.JDA
|
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.guild.GuildJoinEvent
|
import net.dv8tion.jda.api.events.guild.GuildJoinEvent
|
||||||
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent
|
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent
|
||||||
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 net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.discord.commands.*
|
import space.mori.chzzk_bot.common.utils.IData
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.utils.IStreamInfo
|
||||||
|
import space.mori.chzzk_bot.chatbot.discord.commands.*
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
val dotenv = dotenv {
|
||||||
|
ignoreIfMissing = true
|
||||||
|
}
|
||||||
|
|
||||||
class Discord: ListenerAdapter() {
|
class Discord: ListenerAdapter() {
|
||||||
private lateinit var bot: JDA
|
|
||||||
private var guild: Guild? = null
|
private var guild: Guild? = null
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
lateinit var bot: JDA
|
||||||
|
|
||||||
|
internal fun getChannel(guildId: Long, channelId: Long): TextChannel? = bot.getGuildById(guildId)?.getTextChannelById(channelId)
|
||||||
|
|
||||||
|
fun sendDiscord(user: User, status: IData<IStreamInfo?>) {
|
||||||
|
if(status.content == null) return
|
||||||
|
if(user.liveAlertMessage != "" && user.liveAlertGuild != null && user.liveAlertChannel != null) {
|
||||||
|
val channel = getChannel(user.liveAlertGuild!!, user.liveAlertChannel!!) ?: 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.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.replace("{type}", "1080"))
|
||||||
|
|
||||||
|
channel.sendMessage(
|
||||||
|
MessageCreateBuilder()
|
||||||
|
.setContent(user.liveAlertMessage)
|
||||||
|
.setEmbeds(embed.build())
|
||||||
|
.build()
|
||||||
|
).queue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val commands = listOf(
|
private val commands = listOf(
|
||||||
AddCommand,
|
AddCommand,
|
||||||
AlertCommand,
|
AlertCommand,
|
||||||
@@ -61,7 +100,7 @@ class Discord: ListenerAdapter() {
|
|||||||
.queue()
|
.queue()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun enable() {
|
fun enable() {
|
||||||
val thread = Thread {
|
val thread = Thread {
|
||||||
try {
|
try {
|
||||||
bot = JDABuilder.createDefault(dotenv["DISCORD_TOKEN"])
|
bot = JDABuilder.createDefault(dotenv["DISCORD_TOKEN"])
|
||||||
@@ -83,7 +122,7 @@ class Discord: ListenerAdapter() {
|
|||||||
thread.start()
|
thread.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun disable() {
|
fun disable() {
|
||||||
try {
|
try {
|
||||||
bot.shutdown()
|
bot.shutdown()
|
||||||
} catch(e: Exception) {
|
} catch(e: Exception) {
|
||||||
@@ -91,7 +130,4 @@ class Discord: ListenerAdapter() {
|
|||||||
logger.debug(e.stackTraceToString())
|
logger.debug(e.stackTraceToString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
internal fun getChannel(guildId: Long, channelId: Long) =
|
|
||||||
bot.getGuildById(guildId)?.getTextChannelById(channelId)
|
|
||||||
}
|
|
@@ -1,17 +1,18 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.OptionType
|
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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.ChzzkHandler
|
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
|
||||||
import space.mori.chzzk_bot.chzzk.Connector
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.CommandService
|
import space.mori.chzzk_bot.common.services.CommandService
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object AddCommand : CommandInterface {
|
object AddCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
@@ -39,8 +40,15 @@ object AddCommand : CommandInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (manager != null) {
|
if (manager != null) {
|
||||||
user = manager.user
|
transaction {
|
||||||
ManagerService.updateManager(user, event.user.idLong, event.user.effectiveName)
|
user = manager.user
|
||||||
|
}
|
||||||
|
user?.let { ManagerService.updateManager(it, event.user.idLong, event.user.effectiveName) }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val commands = CommandService.getCommands(user!!)
|
val commands = CommandService.getCommands(user!!)
|
||||||
@@ -49,14 +57,14 @@ object AddCommand : CommandInterface {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val chzzkChannel = Connector.getChannel(user.token)
|
val chzzkChannel = Connector.getChannel(user!!.token)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CommandService.saveCommand(user, label, content, failContent ?: "")
|
CommandService.saveCommand(user!!, label, content, failContent ?: "")
|
||||||
try {
|
try {
|
||||||
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
event.hook.sendMessage("등록이 완료되었습니다. $label = $content/$failContent").queue()
|
event.hook.sendMessage("등록이 완료되었습니다. $label = $content / $failContent").queue()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||||
logger.debug(e.stackTraceToString())
|
logger.debug(e.stackTraceToString())
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
@@ -6,9 +6,9 @@ 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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object AddManagerCommand : CommandInterface {
|
object AddManagerCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
@@ -1,21 +1,22 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.OptionType
|
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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.ChzzkHandler
|
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
|
||||||
import space.mori.chzzk_bot.chzzk.Connector
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object AlertCommand : CommandInterface {
|
object AlertCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
override val name: String = "alert"
|
override val name: String = "alert"
|
||||||
override val command = Commands.slash(name, "명령어를 추가합니다.")
|
override val command = Commands.slash(name, "방송알람 채널을 설정합니다. / 알람 취소도 이 명령어를 이용하세요!")
|
||||||
.addOptions(OptionData(OptionType.CHANNEL, "channel", "알림을 보낼 채널을 입력하세요."))
|
.addOptions(OptionData(OptionType.CHANNEL, "channel", "알림을 보낼 채널을 입력하세요."))
|
||||||
.addOptions(OptionData(OptionType.STRING, "content", "표시될 텍스트를 입력하세요. 비워두면 알람이 취소됩니다."))
|
.addOptions(OptionData(OptionType.STRING, "content", "표시될 텍스트를 입력하세요. 비워두면 알람이 취소됩니다."))
|
||||||
|
|
||||||
@@ -31,14 +32,21 @@ object AlertCommand : CommandInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (manager != null) {
|
if (manager != null) {
|
||||||
user = manager.user
|
transaction {
|
||||||
ManagerService.updateManager(user, event.user.idLong, event.user.effectiveName)
|
user = manager.user
|
||||||
|
}
|
||||||
|
user?.let { ManagerService.updateManager(it, event.user.idLong, event.user.effectiveName) }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val chzzkChannel = Connector.getChannel(user!!.token)
|
val chzzkChannel = Connector.getChannel(user!!.token)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val newUser = UserService.updateLiveAlert(user.id.value, channel?.guild?.idLong ?: 0L, channel?.idLong ?: 0L, content ?: "")
|
val newUser = UserService.updateLiveAlert(user!!.id.value, channel?.guild?.idLong ?: 0L, channel?.idLong ?: 0L, content ?: "")
|
||||||
try {
|
try {
|
||||||
ChzzkHandler.reloadUser(chzzkChannel!!, newUser)
|
ChzzkHandler.reloadUser(chzzkChannel!!, newUser)
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
@@ -1,13 +1,13 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.EmbedBuilder
|
import net.dv8tion.jda.api.EmbedBuilder
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.Commands
|
import net.dv8tion.jda.api.interactions.commands.build.Commands
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object ListManagerCommand : CommandInterface {
|
object ListManagerCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
@@ -1,10 +1,10 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.Commands
|
import net.dv8tion.jda.api.interactions.commands.build.Commands
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
|
|
||||||
object PingCommand: CommandInterface {
|
object PingCommand: CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
@@ -1,25 +1,31 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
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.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.OptionType
|
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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.ChzzkHandler
|
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
|
||||||
import space.mori.chzzk_bot.chzzk.Connector
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object RegisterCommand: CommandInterface {
|
object RegisterCommand: CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
override val name = "register"
|
override val name = "register"
|
||||||
|
|
||||||
|
private val regex = """(?:.+chzzk\.naver\.com/)?([a-f0-9]{32})?(?:/live)?${'$'}""".toRegex()
|
||||||
|
|
||||||
override val command = Commands.slash(name, "치지직 계정을 등록합니다.")
|
override val command = Commands.slash(name, "치지직 계정을 등록합니다.")
|
||||||
.addOptions(
|
.addOptions(
|
||||||
OptionData(
|
OptionData(
|
||||||
OptionType.STRING,
|
OptionType.STRING,
|
||||||
"chzzk_id",
|
"chzzk_id",
|
||||||
"36da10b7c35800f298e9c565a396bafd 형식으로 입력해주세요.",
|
"치지직 채널 URL 혹은 ID를 입력해주세요.",
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -30,17 +36,22 @@ object RegisterCommand: CommandInterface {
|
|||||||
event.hook.sendMessage("치지직 계정은 필수 입력입니다.").queue()
|
event.hook.sendMessage("치지직 계정은 필수 입력입니다.").queue()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
val matchResult = regex.find(chzzkID)
|
||||||
|
val matchedChzzkId = matchResult?.groups?.get(1)?.value
|
||||||
|
|
||||||
val chzzkChannel = Connector.getChannel(chzzkID)
|
val chzzkChannel = matchedChzzkId?.let { Connector.getChannel(it) }
|
||||||
if (chzzkChannel == null) {
|
if (chzzkChannel == null) {
|
||||||
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
|
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
val user = UserService.saveUser(chzzkChannel.channelName, chzzkChannel.channelId, event.user.idLong)
|
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}`")
|
event.hook.sendMessage("등록이 완료되었습니다. `${chzzkChannel.channelId}` - `${chzzkChannel.channelName}`")
|
||||||
ChzzkHandler.addUser(chzzkChannel, user)
|
|
||||||
} catch(e: Exception) {
|
} catch(e: Exception) {
|
||||||
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||||
logger.debug(e.stackTraceToString())
|
logger.debug(e.stackTraceToString())
|
@@ -1,17 +1,18 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.OptionType
|
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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.ChzzkHandler
|
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
|
||||||
import space.mori.chzzk_bot.chzzk.Connector
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.CommandService
|
import space.mori.chzzk_bot.common.services.CommandService
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object RemoveCommand : CommandInterface {
|
object RemoveCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
@@ -35,14 +36,21 @@ object RemoveCommand : CommandInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (manager != null) {
|
if (manager != null) {
|
||||||
user = manager.user
|
transaction {
|
||||||
ManagerService.updateManager(user, event.user.idLong, event.user.effectiveName)
|
user = manager.user
|
||||||
|
}
|
||||||
|
user?.let { ManagerService.updateManager(it, event.user.idLong, event.user.effectiveName) }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val chzzkChannel = Connector.getChannel(user!!.token)
|
val chzzkChannel = Connector.getChannel(user!!.token)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CommandService.removeCommand(user, label)
|
CommandService.removeCommand(user!!, label)
|
||||||
try {
|
try {
|
||||||
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
@@ -6,9 +6,9 @@ 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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object RemoveManagerCommand : CommandInterface {
|
object RemoveManagerCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
@@ -1,17 +1,18 @@
|
|||||||
package space.mori.chzzk_bot.discord.commands
|
package space.mori.chzzk_bot.chatbot.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
import net.dv8tion.jda.api.JDA
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||||
import net.dv8tion.jda.api.interactions.commands.OptionType
|
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.Commands
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.ChzzkHandler
|
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
|
||||||
import space.mori.chzzk_bot.chzzk.Connector
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector
|
||||||
import space.mori.chzzk_bot.discord.CommandInterface
|
import space.mori.chzzk_bot.chatbot.discord.CommandInterface
|
||||||
import space.mori.chzzk_bot.services.CommandService
|
import space.mori.chzzk_bot.common.services.CommandService
|
||||||
import space.mori.chzzk_bot.services.ManagerService
|
import space.mori.chzzk_bot.common.services.ManagerService
|
||||||
import space.mori.chzzk_bot.services.UserService
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
object UpdateCommand : CommandInterface {
|
object UpdateCommand : CommandInterface {
|
||||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
@@ -39,14 +40,21 @@ object UpdateCommand : CommandInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (manager != null) {
|
if (manager != null) {
|
||||||
user = manager.user
|
transaction {
|
||||||
ManagerService.updateManager(user, event.user.idLong, event.user.effectiveName)
|
user = manager.user
|
||||||
|
}
|
||||||
|
user?.let { ManagerService.updateManager(it, event.user.idLong, event.user.effectiveName) }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val chzzkChannel = Connector.getChannel(user!!.token)
|
val chzzkChannel = Connector.getChannel(user!!.token)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CommandService.updateCommand(user, label, content, failContent ?: "")
|
CommandService.updateCommand(user!!, label, content, failContent ?: "")
|
||||||
chzzkChannel?.let { ChzzkHandler.reloadCommand(it) }
|
chzzkChannel?.let { ChzzkHandler.reloadCommand(it) }
|
||||||
event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue()
|
event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
48
common/build.gradle.kts
Normal file
48
common/build.gradle.kts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm")
|
||||||
|
}
|
||||||
|
|
||||||
|
group = project.rootProject.group
|
||||||
|
version = project.rootProject.version
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-core
|
||||||
|
api("org.jetbrains.exposed:exposed-core:0.52.0")
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-dao
|
||||||
|
api("org.jetbrains.exposed:exposed-dao:0.52.0")
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-jdbc
|
||||||
|
api("org.jetbrains.exposed:exposed-jdbc:0.52.0")
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.exposed/exposed-kotlin-datetime
|
||||||
|
api("org.jetbrains.exposed:exposed-java-time:0.52.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/com.zaxxer/HikariCP
|
||||||
|
api("com.zaxxer:HikariCP:5.1.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
|
||||||
|
implementation("ch.qos.logback:logback-classic:1.5.6")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
|
||||||
|
implementation("org.mariadb.jdbc:mariadb-java-client:3.4.1")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/io.github.cdimascio/dotenv-kotlin
|
||||||
|
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||||
|
implementation("com.google.code.gson:gson:2.11.0")
|
||||||
|
|
||||||
|
testImplementation(kotlin("test"))
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
kotlin {
|
||||||
|
jvmToolchain(21)
|
||||||
|
}
|
@@ -1,11 +1,16 @@
|
|||||||
package space.mori.chzzk_bot
|
package space.mori.chzzk_bot.common
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig
|
import com.zaxxer.hikari.HikariConfig
|
||||||
import com.zaxxer.hikari.HikariDataSource
|
import com.zaxxer.hikari.HikariDataSource
|
||||||
|
import io.github.cdimascio.dotenv.dotenv
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.Database
|
||||||
import org.jetbrains.exposed.sql.SchemaUtils
|
import org.jetbrains.exposed.sql.SchemaUtils
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import space.mori.chzzk_bot.models.*
|
import space.mori.chzzk_bot.common.models.*
|
||||||
|
|
||||||
|
val dotenv = dotenv {
|
||||||
|
ignoreIfMissing = true
|
||||||
|
}
|
||||||
|
|
||||||
object Connector {
|
object Connector {
|
||||||
private val hikariConfig = HikariConfig().apply {
|
private val hikariConfig = HikariConfig().apply {
|
||||||
@@ -19,7 +24,18 @@ object Connector {
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
Database.connect(dataSource)
|
Database.connect(dataSource)
|
||||||
val tables = listOf(Users, Commands, Counters, DailyCounters, PersonalCounters, Managers)
|
val tables = listOf(
|
||||||
|
Users,
|
||||||
|
Commands,
|
||||||
|
Counters,
|
||||||
|
DailyCounters,
|
||||||
|
PersonalCounters,
|
||||||
|
Managers,
|
||||||
|
TimerConfigs,
|
||||||
|
LiveStatuses,
|
||||||
|
SongLists,
|
||||||
|
SongConfigs
|
||||||
|
)
|
||||||
|
|
||||||
transaction {
|
transaction {
|
||||||
SchemaUtils.createMissingTablesAndColumns(* tables.toTypedArray())
|
SchemaUtils.createMissingTablesAndColumns(* tables.toTypedArray())
|
@@ -0,0 +1,32 @@
|
|||||||
|
package space.mori.chzzk_bot.common.events
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
|
import kotlinx.coroutines.flow.filterIsInstance
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
interface Event
|
||||||
|
|
||||||
|
interface EventBus {
|
||||||
|
suspend fun <T: Event> post(event: T)
|
||||||
|
fun <T: Event> subscribe(eventClass: KClass<T>, listener: (T) -> Unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
class CoroutinesEventBus: EventBus {
|
||||||
|
private val _events = MutableSharedFlow<Event>()
|
||||||
|
val events: SharedFlow<Event> get() = _events
|
||||||
|
|
||||||
|
override suspend fun<T: Event> post(event: T) = _events.emit(event)
|
||||||
|
|
||||||
|
override fun <T: Event> subscribe(eventClass: KClass<T>, listener: (T) -> Unit) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
events.filterIsInstance(eventClass)
|
||||||
|
.collect {
|
||||||
|
listener(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,22 @@
|
|||||||
|
package space.mori.chzzk_bot.common.events
|
||||||
|
|
||||||
|
enum class SongType(var value: Int) {
|
||||||
|
ADD(0),
|
||||||
|
REMOVE(1),
|
||||||
|
NEXT(2),
|
||||||
|
|
||||||
|
STREAM_OFF(50)
|
||||||
|
}
|
||||||
|
|
||||||
|
class SongEvent(
|
||||||
|
val uid: String,
|
||||||
|
val type: SongType,
|
||||||
|
val reqUid: String?,
|
||||||
|
val reqName: String?,
|
||||||
|
val name: String?,
|
||||||
|
val author: String?,
|
||||||
|
val time: Int?,
|
||||||
|
val url: String?
|
||||||
|
): Event {
|
||||||
|
var TAG = javaClass.simpleName
|
||||||
|
}
|
@@ -0,0 +1,17 @@
|
|||||||
|
package space.mori.chzzk_bot.common.events
|
||||||
|
|
||||||
|
enum class TimerType(var value: Int) {
|
||||||
|
UPTIME(0),
|
||||||
|
TIMER(1),
|
||||||
|
REMOVE(2),
|
||||||
|
|
||||||
|
STREAM_OFF(50)
|
||||||
|
}
|
||||||
|
|
||||||
|
class TimerEvent(
|
||||||
|
val uid: String,
|
||||||
|
val type: TimerType,
|
||||||
|
val time: String?
|
||||||
|
): Event {
|
||||||
|
var TAG = javaClass.simpleName
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.models
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.models
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.models
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
@@ -0,0 +1,19 @@
|
|||||||
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
|
import org.jetbrains.exposed.dao.IntEntityClass
|
||||||
|
import org.jetbrains.exposed.dao.id.EntityID
|
||||||
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||||
|
import org.jetbrains.exposed.sql.ReferenceOption
|
||||||
|
|
||||||
|
object LiveStatuses: IntIdTable("live_statuses") {
|
||||||
|
val user = reference("user", Users, onDelete = ReferenceOption.CASCADE)
|
||||||
|
val status = bool("status")
|
||||||
|
}
|
||||||
|
|
||||||
|
class LiveStatus(id: EntityID<Int>) : IntEntity(id) {
|
||||||
|
companion object : IntEntityClass<LiveStatus>(LiveStatuses)
|
||||||
|
|
||||||
|
var user by User referencedOn LiveStatuses.user
|
||||||
|
var status by LiveStatuses.status
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.models
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.models
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
@@ -0,0 +1,24 @@
|
|||||||
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
|
import org.jetbrains.exposed.dao.IntEntityClass
|
||||||
|
import org.jetbrains.exposed.dao.id.EntityID
|
||||||
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||||
|
import org.jetbrains.exposed.sql.ReferenceOption
|
||||||
|
|
||||||
|
object SongConfigs: IntIdTable("song_config") {
|
||||||
|
val user = reference("user", Users, onDelete = ReferenceOption.CASCADE)
|
||||||
|
val token = varchar("token", 64).nullable()
|
||||||
|
val streamerOnly = bool("streamer_only").default(false)
|
||||||
|
val queueLimit = integer("queue_limit").default(50)
|
||||||
|
val personalLimit = integer("personal_limit").default(5)
|
||||||
|
}
|
||||||
|
class SongConfig(id: EntityID<Int>) : IntEntity(id) {
|
||||||
|
companion object : IntEntityClass<SongConfig>(SongConfigs)
|
||||||
|
|
||||||
|
var user by User referencedOn SongConfigs.user
|
||||||
|
var token by SongConfigs.token
|
||||||
|
var streamerOnly by SongConfigs.streamerOnly
|
||||||
|
var queueLimit by SongConfigs.queueLimit
|
||||||
|
var personalLimit by SongConfigs.personalLimit
|
||||||
|
}
|
@@ -0,0 +1,33 @@
|
|||||||
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
|
import org.jetbrains.exposed.dao.IntEntityClass
|
||||||
|
import org.jetbrains.exposed.dao.id.EntityID
|
||||||
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||||
|
import org.jetbrains.exposed.sql.javatime.datetime
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
object SongLists: IntIdTable("song_list") {
|
||||||
|
val user = reference("user", Users)
|
||||||
|
val uid = varchar("uid", 64)
|
||||||
|
val url = varchar("url", 128)
|
||||||
|
val name = text("name")
|
||||||
|
val reqName = varchar("req_name", 20)
|
||||||
|
val author = text("author")
|
||||||
|
val time = integer("time")
|
||||||
|
val created_at = datetime("created_at").default(LocalDateTime.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
class SongList(id: EntityID<Int>) : IntEntity(id) {
|
||||||
|
companion object : IntEntityClass<SongList>(SongLists)
|
||||||
|
|
||||||
|
var url by SongLists.url
|
||||||
|
var name by SongLists.name
|
||||||
|
var author by SongLists.author
|
||||||
|
var time by SongLists.time
|
||||||
|
var created_at by SongLists.created_at
|
||||||
|
|
||||||
|
var user by User referencedOn SongLists.user
|
||||||
|
var uid by SongLists.uid
|
||||||
|
var reqName by SongLists.reqName
|
||||||
|
}
|
@@ -0,0 +1,18 @@
|
|||||||
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
|
import org.jetbrains.exposed.dao.IntEntityClass
|
||||||
|
import org.jetbrains.exposed.dao.id.EntityID
|
||||||
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||||
|
import org.jetbrains.exposed.sql.ReferenceOption
|
||||||
|
|
||||||
|
object TimerConfigs: IntIdTable("timer_config") {
|
||||||
|
val user = reference("user", Users, onDelete = ReferenceOption.CASCADE)
|
||||||
|
val option = integer("option")
|
||||||
|
}
|
||||||
|
class TimerConfig(id: EntityID<Int>) : IntEntity(id) {
|
||||||
|
companion object : IntEntityClass<TimerConfig>(TimerConfigs)
|
||||||
|
|
||||||
|
var user by User referencedOn TimerConfigs.user
|
||||||
|
var option by TimerConfigs.option
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.models
|
package space.mori.chzzk_bot.common.models
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
@@ -1,17 +1,17 @@
|
|||||||
package space.mori.chzzk_bot.services
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
import org.jetbrains.exposed.sql.and
|
import org.jetbrains.exposed.sql.and
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.jetbrains.exposed.sql.update
|
import org.jetbrains.exposed.sql.update
|
||||||
import space.mori.chzzk_bot.models.Command
|
import space.mori.chzzk_bot.common.models.Command
|
||||||
import space.mori.chzzk_bot.models.Commands
|
import space.mori.chzzk_bot.common.models.Commands
|
||||||
import space.mori.chzzk_bot.models.User
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
|
||||||
object CommandService {
|
object CommandService {
|
||||||
fun saveCommand(user: User, command: String, content: String, failContent: String): Command {
|
fun saveCommand(user: User, command: String, content: String, failContent: String): Command {
|
||||||
return transaction {
|
return transaction {
|
||||||
return@transaction Command.new {
|
Command.new {
|
||||||
this.user = user
|
this.user = user
|
||||||
this.command = command
|
this.command = command
|
||||||
this.content = content
|
this.content = content
|
@@ -1,8 +1,8 @@
|
|||||||
package space.mori.chzzk_bot.services
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
import org.jetbrains.exposed.sql.and
|
import org.jetbrains.exposed.sql.and
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import space.mori.chzzk_bot.models.*
|
import space.mori.chzzk_bot.common.models.*
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
|
|
||||||
object CounterService {
|
object CounterService {
|
@@ -0,0 +1,30 @@
|
|||||||
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
import space.mori.chzzk_bot.common.models.LiveStatus
|
||||||
|
import space.mori.chzzk_bot.common.models.LiveStatuses
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
|
||||||
|
object LiveStatusService {
|
||||||
|
fun updateOrCreate(user: User, status: Boolean): LiveStatus {
|
||||||
|
return transaction {
|
||||||
|
return@transaction when(val liveStatus = LiveStatus.find(LiveStatuses.user eq user.id).firstOrNull()) {
|
||||||
|
null -> LiveStatus.new {
|
||||||
|
this.user = user
|
||||||
|
this.status = status
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
liveStatus.status = status
|
||||||
|
liveStatus
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLiveStatus(user: User): LiveStatus? {
|
||||||
|
return transaction {
|
||||||
|
LiveStatus.find(LiveStatuses.user eq user.id).firstOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,11 +1,13 @@
|
|||||||
package space.mori.chzzk_bot.services
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.dao.load
|
||||||
|
import org.jetbrains.exposed.dao.with
|
||||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
import org.jetbrains.exposed.sql.and
|
import org.jetbrains.exposed.sql.and
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import space.mori.chzzk_bot.models.Manager
|
import space.mori.chzzk_bot.common.models.Manager
|
||||||
import space.mori.chzzk_bot.models.Managers
|
import space.mori.chzzk_bot.common.models.Managers
|
||||||
import space.mori.chzzk_bot.models.User
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
|
||||||
object ManagerService {
|
object ManagerService {
|
||||||
fun saveManager(user: User, discordId: Long, name: String): Manager {
|
fun saveManager(user: User, discordId: Long, name: String): Manager {
|
||||||
@@ -23,31 +25,38 @@ object ManagerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun updateManager(user: User, discordId: Long, name: String): Manager {
|
fun updateManager(user: User, discordId: Long, name: String): Manager {
|
||||||
if (user.liveAlertGuild == null)
|
return transaction {
|
||||||
throw RuntimeException("${user.username} has no liveAlertGuild")
|
if (user.liveAlertGuild == null)
|
||||||
|
throw RuntimeException("${user.username} has no liveAlertGuild")
|
||||||
|
|
||||||
val manager = getUser(user.liveAlertGuild!!, discordId)
|
val manager = getUser(user.liveAlertGuild!!, discordId) ?: throw RuntimeException("$name isn't manager.")
|
||||||
|
|
||||||
if (manager == null)
|
manager.lastUserName = name
|
||||||
throw RuntimeException("$name isn't manager.")
|
|
||||||
manager.lastUserName = name
|
|
||||||
|
|
||||||
return manager
|
manager
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getUser(guildId: Long, discordId: Long): Manager? {
|
fun getUser(guildId: Long, discordId: Long): Manager? {
|
||||||
return transaction {
|
return transaction {
|
||||||
val manager = Manager.find(
|
val manager = Manager.find(
|
||||||
(Managers.discordGuildId eq guildId) and (Managers.managerId eq discordId)
|
(Managers.discordGuildId eq guildId) and (Managers.managerId eq discordId),
|
||||||
)
|
)
|
||||||
|
.with(Manager::user)
|
||||||
manager.firstOrNull()
|
.firstOrNull()
|
||||||
|
manager
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getAllUsers(guildId: Long): List<Manager> {
|
fun getAllUsers(guildId: Long): List<Manager> {
|
||||||
return transaction {
|
return transaction {
|
||||||
Manager.find(Managers.discordGuildId eq guildId).toList()
|
val result = Manager.find(Managers.discordGuildId eq guildId)
|
||||||
|
.with(Manager::user)
|
||||||
|
.toList()
|
||||||
|
|
||||||
|
result.forEach { it.load(Manager::user) }
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@@ -0,0 +1,86 @@
|
|||||||
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
import space.mori.chzzk_bot.common.models.SongConfig
|
||||||
|
import space.mori.chzzk_bot.common.models.SongConfigs
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
|
||||||
|
object SongConfigService {
|
||||||
|
private fun initConfig(user: User): SongConfig {
|
||||||
|
return transaction {
|
||||||
|
SongConfig.new {
|
||||||
|
this.user = user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getConfig(user: User): SongConfig {
|
||||||
|
return transaction {
|
||||||
|
var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull()
|
||||||
|
if (songConfig == null) {
|
||||||
|
songConfig = initConfig(user)
|
||||||
|
}
|
||||||
|
songConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getConfig(token: String): SongConfig? {
|
||||||
|
return transaction {
|
||||||
|
SongConfig.find(SongConfigs.token eq token).firstOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun getUserByToken(token: String): User? {
|
||||||
|
return transaction {
|
||||||
|
val songConfig = SongConfig.find(SongConfigs.token eq token).firstOrNull()
|
||||||
|
if(songConfig == null) null
|
||||||
|
else UserService.getUser(songConfig.user.discord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updatePersonalLimit(user: User, limit: Int): SongConfig {
|
||||||
|
return transaction {
|
||||||
|
var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull()
|
||||||
|
if (songConfig == null) {
|
||||||
|
songConfig = initConfig(user)
|
||||||
|
}
|
||||||
|
songConfig.personalLimit = limit
|
||||||
|
songConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun updateQueueLimit(user: User, limit: Int): SongConfig {
|
||||||
|
return transaction {
|
||||||
|
var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull()
|
||||||
|
if (songConfig == null) {
|
||||||
|
songConfig = initConfig(user)
|
||||||
|
}
|
||||||
|
songConfig.queueLimit = limit
|
||||||
|
songConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateSession(user: User, token: String?): SongConfig {
|
||||||
|
return transaction {
|
||||||
|
var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull()
|
||||||
|
if (songConfig == null) {
|
||||||
|
songConfig = initConfig(user)
|
||||||
|
}
|
||||||
|
songConfig.token = token
|
||||||
|
|
||||||
|
songConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateStreamerOnly(user: User, config: Boolean): SongConfig {
|
||||||
|
return transaction {
|
||||||
|
var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull()
|
||||||
|
if (songConfig == null) {
|
||||||
|
songConfig = initConfig(user)
|
||||||
|
}
|
||||||
|
songConfig.streamerOnly = config
|
||||||
|
|
||||||
|
songConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,63 @@
|
|||||||
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
|
import org.jetbrains.exposed.sql.and
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
import space.mori.chzzk_bot.common.models.SongList
|
||||||
|
import space.mori.chzzk_bot.common.models.SongLists
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
|
||||||
|
object SongListService {
|
||||||
|
fun saveSong(user: User, uid: String, url: String, name: String, author: String, time: Int, reqName: String) {
|
||||||
|
return transaction {
|
||||||
|
SongList.new {
|
||||||
|
this.user = user
|
||||||
|
this.uid = uid
|
||||||
|
this.url = url
|
||||||
|
this.name = name
|
||||||
|
this.author = author
|
||||||
|
this.time = time
|
||||||
|
this.reqName = reqName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getSong(user: User, uid: String): List<SongList> {
|
||||||
|
return transaction {
|
||||||
|
SongList.find(
|
||||||
|
(SongLists.user eq user.id) and
|
||||||
|
(SongLists.uid eq uid)
|
||||||
|
).toList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getSong(user: User): List<SongList> {
|
||||||
|
return transaction {
|
||||||
|
SongList.find(SongLists.user eq user.id).toList().sortedBy { it.created_at }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteSong(user: User, uid: String, name: String): SongList {
|
||||||
|
return transaction {
|
||||||
|
val songRow = SongList.find(
|
||||||
|
(SongLists.user eq user.id) and
|
||||||
|
(SongLists.uid eq uid) and
|
||||||
|
(SongLists.name eq name)
|
||||||
|
).firstOrNull()
|
||||||
|
|
||||||
|
songRow ?: throw RuntimeException("Song not found! ${user.username} / $uid / $name")
|
||||||
|
|
||||||
|
songRow.delete()
|
||||||
|
songRow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteUser(user: User): Boolean {
|
||||||
|
return transaction {
|
||||||
|
val songRow = SongList.find(SongLists.user eq user.id).toList()
|
||||||
|
|
||||||
|
songRow.forEach { it.delete() }
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,48 @@
|
|||||||
|
package space.mori.chzzk_bot.common.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.common.events.TimerType
|
||||||
|
import space.mori.chzzk_bot.common.models.TimerConfig
|
||||||
|
import space.mori.chzzk_bot.common.models.TimerConfigs
|
||||||
|
import space.mori.chzzk_bot.common.models.User
|
||||||
|
|
||||||
|
object TimerConfigService {
|
||||||
|
fun saveConfig(user: User, timerConfig: TimerType) {
|
||||||
|
return transaction {
|
||||||
|
TimerConfig.new {
|
||||||
|
this.user = user
|
||||||
|
this.option = timerConfig.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateConfig(user: User, timerConfig: TimerType) {
|
||||||
|
return transaction {
|
||||||
|
val updated = TimerConfigs.update({
|
||||||
|
TimerConfigs.user eq user.id
|
||||||
|
}) {
|
||||||
|
it[option] = timerConfig.value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updated == 0) throw RuntimeException("TimerConfig not found! ${user.username}")
|
||||||
|
|
||||||
|
TimerConfig.find { TimerConfigs.user eq user.id }.first()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getConfig(user: User): TimerConfig? {
|
||||||
|
return transaction {
|
||||||
|
TimerConfig.find(TimerConfigs.user eq user.id).firstOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveOrUpdateConfig(user: User, timerConfig: TimerType) {
|
||||||
|
return if (getConfig(user) == null) {
|
||||||
|
saveConfig(user, timerConfig)
|
||||||
|
} else {
|
||||||
|
updateConfig(user, timerConfig)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,10 +1,10 @@
|
|||||||
package space.mori.chzzk_bot.services
|
package space.mori.chzzk_bot.common.services
|
||||||
|
|
||||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.jetbrains.exposed.sql.update
|
import org.jetbrains.exposed.sql.update
|
||||||
import space.mori.chzzk_bot.models.User
|
import space.mori.chzzk_bot.common.models.User
|
||||||
import space.mori.chzzk_bot.models.Users
|
import space.mori.chzzk_bot.common.models.Users
|
||||||
|
|
||||||
object UserService {
|
object UserService {
|
||||||
fun saveUser(username: String, token: String, discordID: Long): User {
|
fun saveUser(username: String, token: String, discordID: Long): User {
|
@@ -1,4 +1,4 @@
|
|||||||
package space.mori.chzzk_bot.chzzk
|
package space.mori.chzzk_bot.common.utils
|
||||||
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
@@ -108,7 +108,7 @@ val client = OkHttpClient.Builder()
|
|||||||
.build()
|
.build()
|
||||||
val gson = Gson()
|
val gson = Gson()
|
||||||
|
|
||||||
fun getFollowDate(chatID: String, userId: String) : IData<IFollowContent> {
|
fun getFollowDate(chatID: String, userId: String) : IData<IFollowContent?> {
|
||||||
val url = "https://comm-api.game.naver.com/nng_main/v1/chats/$chatID/users/$userId/profile-card?chatType=STREAMING"
|
val url = "https://comm-api.game.naver.com/nng_main/v1/chats/$chatID/users/$userId/profile-card?chatType=STREAMING"
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
@@ -118,7 +118,7 @@ fun getFollowDate(chatID: String, userId: String) : IData<IFollowContent> {
|
|||||||
try {
|
try {
|
||||||
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
||||||
val body = response.body?.string()
|
val body = response.body?.string()
|
||||||
val follow = gson.fromJson(body, object: TypeToken<IData<IFollowContent>>() {})
|
val follow = gson.fromJson(body, object: TypeToken<IData<IFollowContent?>>() {})
|
||||||
|
|
||||||
return follow
|
return follow
|
||||||
} catch(e: Exception) {
|
} catch(e: Exception) {
|
||||||
@@ -128,8 +128,8 @@ fun getFollowDate(chatID: String, userId: String) : IData<IFollowContent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getStreamInfo(userId: String) : IData<IStreamInfo> {
|
fun getStreamInfo(userId: String) : IData<IStreamInfo?> {
|
||||||
val url = "https://api.chzzk.naver.com/service/v2/channels/${userId}/live-detail"
|
val url = "https://api.chzzk.naver.com/service/v3/channels/${userId}/live-detail"
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.build()
|
.build()
|
||||||
@@ -138,7 +138,7 @@ fun getStreamInfo(userId: String) : IData<IStreamInfo> {
|
|||||||
try {
|
try {
|
||||||
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
||||||
val body = response.body?.string()
|
val body = response.body?.string()
|
||||||
val follow = gson.fromJson(body, object: TypeToken<IData<IStreamInfo>>() {})
|
val follow = gson.fromJson(body, object: TypeToken<IData<IStreamInfo?>>() {})
|
||||||
|
|
||||||
return follow
|
return follow
|
||||||
} catch(e: Exception) {
|
} catch(e: Exception) {
|
@@ -0,0 +1,20 @@
|
|||||||
|
package space.mori.chzzk_bot.common.utils
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
import java.time.format.DateTimeParseException
|
||||||
|
|
||||||
|
val logger: Logger = LoggerFactory.getLogger("convertChzzkDateToLocalDateTime")
|
||||||
|
|
||||||
|
fun convertChzzkDateToLocalDateTime(chzzkDate: String): LocalDateTime? {
|
||||||
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||||
|
|
||||||
|
return try {
|
||||||
|
LocalDateTime.parse(chzzkDate, formatter)
|
||||||
|
} catch(e: DateTimeParseException) {
|
||||||
|
logger.debug("Error to parsing date", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,9 @@
|
|||||||
|
package space.mori.chzzk_bot.common.utils
|
||||||
|
|
||||||
|
fun getRandomString(length: Int): String {
|
||||||
|
val charPool = ('a'..'z') + ('0'..'9')
|
||||||
|
return (1..length)
|
||||||
|
.map { kotlin.random.Random.nextInt(0, charPool.size) }
|
||||||
|
.map(charPool::get)
|
||||||
|
.joinToString("")
|
||||||
|
}
|
@@ -0,0 +1,14 @@
|
|||||||
|
package space.mori.chzzk_bot.common.utils
|
||||||
|
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
fun getUptime(streamOnTime: LocalDateTime): String {
|
||||||
|
val currentTime = LocalDateTime.now()
|
||||||
|
|
||||||
|
val hours = ChronoUnit.HOURS.between(streamOnTime, currentTime)
|
||||||
|
val minutes = ChronoUnit.MINUTES.between(streamOnTime?.plusHours(hours), currentTime)
|
||||||
|
val seconds = ChronoUnit.SECONDS.between(streamOnTime?.plusHours(hours)?.plusMinutes(minutes), currentTime)
|
||||||
|
|
||||||
|
return String.format("%02d:%02d:%02d", hours, minutes, seconds)
|
||||||
|
}
|
@@ -0,0 +1,115 @@
|
|||||||
|
package space.mori.chzzk_bot.common.utils
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject
|
||||||
|
import io.github.cdimascio.dotenv.dotenv
|
||||||
|
import okhttp3.HttpUrl
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import java.io.IOException
|
||||||
|
import java.net.URLEncoder
|
||||||
|
|
||||||
|
data class YoutubeVideo(
|
||||||
|
val url: String,
|
||||||
|
val name: String,
|
||||||
|
val author: String,
|
||||||
|
val length: Int
|
||||||
|
)
|
||||||
|
|
||||||
|
val regex = ".*(?:youtu.be/|v/|u/\\w/|embed/|watch\\?v=|&v=)([^#&?]*).*".toRegex()
|
||||||
|
val durationRegex = """PT(\d+H)?(\d+M)?(\d+S)?""".toRegex()
|
||||||
|
|
||||||
|
val dotenv = dotenv {
|
||||||
|
ignoreIfMissing = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun searchYoutube(query: String): String? {
|
||||||
|
val url = "https://youtube-search-results.p.rapidapi.com/youtube-search/?q=${URLEncoder.encode(query, "UTF-8")}"
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.addHeader("x-rapidapi-host", "youtube-search-results.p.rapidapi.com")
|
||||||
|
.addHeader("x-rapidapi-key", dotenv["RAPID_KEY"] ?: "")
|
||||||
|
.build()
|
||||||
|
|
||||||
|
OkHttpClient().newCall(request).execute().use { response ->
|
||||||
|
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||||
|
|
||||||
|
val responseBody = response.body?.string()
|
||||||
|
val json = gson.fromJson(responseBody, JsonObject::class.java)
|
||||||
|
|
||||||
|
val videos = json.getAsJsonArray("videos")
|
||||||
|
|
||||||
|
val firstVideo = videos.get(0).asJsonObject
|
||||||
|
|
||||||
|
val videoId = firstVideo.get("id").asString
|
||||||
|
|
||||||
|
return videoId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getYoutubeVideoId(query: String): String? {
|
||||||
|
val matchResult = regex.find(query)
|
||||||
|
|
||||||
|
return if(matchResult == null) {
|
||||||
|
searchYoutube(query)
|
||||||
|
} else {
|
||||||
|
matchResult.groups[1]?.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun parseDuration(duration: String): Int {
|
||||||
|
val matchResult = durationRegex.find(duration)
|
||||||
|
val (hours, minutes, seconds) = matchResult?.destructured ?: return 0
|
||||||
|
|
||||||
|
val hourInSec = hours.dropLast(1).toIntOrNull()?.times(3600) ?: 0
|
||||||
|
val minutesInSec = minutes.dropLast(1).toIntOrNull()?.times(60) ?: 0
|
||||||
|
val totalSeconds = seconds.dropLast(1).toIntOrNull() ?: 0
|
||||||
|
|
||||||
|
return hourInSec + minutesInSec + totalSeconds
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getYoutubeVideo(query: String): YoutubeVideo? {
|
||||||
|
val videoId = getYoutubeVideoId(query)
|
||||||
|
|
||||||
|
val api = HttpUrl.Builder()
|
||||||
|
.scheme("https")
|
||||||
|
.host("www.googleapis.com")
|
||||||
|
.addPathSegment("youtube")
|
||||||
|
.addPathSegment("v3")
|
||||||
|
.addPathSegment("videos")
|
||||||
|
.addQueryParameter("id", videoId)
|
||||||
|
.addQueryParameter("key", dotenv["YOUTUBE_API_KEY"])
|
||||||
|
.addQueryParameter("part", "snippet,contentDetails,status")
|
||||||
|
.build()
|
||||||
|
|
||||||
|
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(api)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
client.newCall(request).execute().use { response ->
|
||||||
|
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||||
|
|
||||||
|
val responseBody = response.body?.string()
|
||||||
|
val json = gson.fromJson(responseBody, JsonObject::class.java)
|
||||||
|
val items = json.getAsJsonArray("items")
|
||||||
|
|
||||||
|
if (items == null || items.size() == 0) return null
|
||||||
|
|
||||||
|
val item = items[0].asJsonObject
|
||||||
|
val snippet = item.getAsJsonObject("snippet")
|
||||||
|
val contentDetail = item.getAsJsonObject("contentDetails")
|
||||||
|
val status = item.getAsJsonObject("status")
|
||||||
|
|
||||||
|
if (!status.get("embeddable").asBoolean) return null
|
||||||
|
|
||||||
|
val duration = contentDetail.get("duration").asString
|
||||||
|
val length = parseDuration(duration)
|
||||||
|
|
||||||
|
return YoutubeVideo(
|
||||||
|
"https://www.youtube.com/watch?v=$videoId",
|
||||||
|
snippet.get("title").asString,
|
||||||
|
snippet.get("channelTitle").asString,
|
||||||
|
length
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
group = space.mori
|
group = space.mori
|
||||||
version = 1.0.3
|
version = 1.2.0
|
||||||
|
|
||||||
org.gradle.jvmargs=-Dfile.encoding=UTF-8
|
org.gradle.jvmargs=-Dfile.encoding=UTF-8
|
||||||
org.gradle.console=plain
|
org.gradle.console=plain
|
||||||
|
2
inc.env
2
inc.env
@@ -4,5 +4,7 @@ DB_URL=jdbc:mariadb://localhost:3306/chzzk
|
|||||||
DB_USER=chzzk
|
DB_USER=chzzk
|
||||||
DB_PASS=chzzk
|
DB_PASS=chzzk
|
||||||
RUN_AGENT=false
|
RUN_AGENT=false
|
||||||
|
YOUTUBE_API_KEY=
|
||||||
|
RAPID_KEY=
|
||||||
NID_AUT=
|
NID_AUT=
|
||||||
NID_SES=
|
NID_SES=
|
@@ -14,6 +14,13 @@ pluginManagement {
|
|||||||
mavenCentral()
|
mavenCentral()
|
||||||
gradlePluginPortal()
|
gradlePluginPortal()
|
||||||
maven { url = uri("https://maven.covers1624.net") }
|
maven { url = uri("https://maven.covers1624.net") }
|
||||||
maven { url = uri("https://repo.spring.io/plugins-release/") }
|
maven { url = uri("https://jitpack.io") }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
plugins {
|
||||||
|
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
|
||||||
|
}
|
||||||
|
include("common")
|
||||||
|
include("chatbot")
|
||||||
|
include("webserver")
|
||||||
|
@@ -3,11 +3,17 @@ package space.mori.chzzk_bot
|
|||||||
import io.github.cdimascio.dotenv.dotenv
|
import io.github.cdimascio.dotenv.dotenv
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.koin.core.context.GlobalContext.startKoin
|
||||||
|
import org.koin.dsl.module
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chzzk.ChzzkHandler
|
import space.mori.chzzk_bot.chatbot.chzzk.ChzzkHandler
|
||||||
import space.mori.chzzk_bot.discord.Discord
|
import space.mori.chzzk_bot.chatbot.discord.Discord
|
||||||
import space.mori.chzzk_bot.chzzk.Connector as ChzzkConnector
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector as ChzzkConnector
|
||||||
|
import space.mori.chzzk_bot.common.Connector
|
||||||
|
import space.mori.chzzk_bot.common.events.CoroutinesEventBus
|
||||||
|
import space.mori.chzzk_bot.webserver.start
|
||||||
|
import space.mori.chzzk_bot.webserver.stop
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
@@ -16,16 +22,24 @@ val dotenv = dotenv {
|
|||||||
}
|
}
|
||||||
val logger: Logger = LoggerFactory.getLogger("main")
|
val logger: Logger = LoggerFactory.getLogger("main")
|
||||||
|
|
||||||
val discord = Discord()
|
|
||||||
|
|
||||||
val connector = Connector
|
|
||||||
val chzzkConnector = ChzzkConnector
|
|
||||||
val chzzkHandler = ChzzkHandler
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
|
val dispatcher = module {
|
||||||
|
single { CoroutinesEventBus() }
|
||||||
|
}
|
||||||
|
startKoin {
|
||||||
|
modules(dispatcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
val discord = Discord()
|
||||||
|
|
||||||
|
val connector = Connector
|
||||||
|
val chzzkConnector = ChzzkConnector
|
||||||
|
val chzzkHandler = ChzzkHandler
|
||||||
|
|
||||||
discord.enable()
|
discord.enable()
|
||||||
chzzkHandler.enable()
|
chzzkHandler.enable()
|
||||||
chzzkHandler.runStreamInfo()
|
chzzkHandler.runStreamInfo()
|
||||||
|
start()
|
||||||
|
|
||||||
if(dotenv.get("RUN_AGENT", "false").toBoolean()) {
|
if(dotenv.get("RUN_AGENT", "false").toBoolean()) {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
@@ -36,6 +50,8 @@ fun main(args: Array<String>) {
|
|||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(Thread {
|
Runtime.getRuntime().addShutdownHook(Thread {
|
||||||
logger.info("Shutting down...")
|
logger.info("Shutting down...")
|
||||||
|
stop()
|
||||||
|
|
||||||
chzzkHandler.stopStreamInfo()
|
chzzkHandler.stopStreamInfo()
|
||||||
chzzkHandler.disable()
|
chzzkHandler.disable()
|
||||||
discord.disable()
|
discord.disable()
|
||||||
|
@@ -1,193 +0,0 @@
|
|||||||
package space.mori.chzzk_bot.chzzk
|
|
||||||
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import space.mori.chzzk_bot.models.User
|
|
||||||
import space.mori.chzzk_bot.services.CommandService
|
|
||||||
import space.mori.chzzk_bot.services.CounterService
|
|
||||||
import space.mori.chzzk_bot.services.UserService
|
|
||||||
import xyz.r2turntrue.chzzk4j.chat.ChatMessage
|
|
||||||
import xyz.r2turntrue.chzzk4j.chat.ChzzkChat
|
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
|
||||||
import java.time.LocalDateTime
|
|
||||||
import java.time.format.DateTimeFormatter
|
|
||||||
import java.time.temporal.ChronoUnit
|
|
||||||
|
|
||||||
|
|
||||||
class MessageHandler(
|
|
||||||
private val channel: ChzzkChannel,
|
|
||||||
private val logger: Logger,
|
|
||||||
private val listener: ChzzkChat
|
|
||||||
) {
|
|
||||||
private val commands = mutableMapOf<String, (msg: ChatMessage, user: User) -> Unit>()
|
|
||||||
|
|
||||||
private val counterPattern = Regex("<counter:([^>]+)>")
|
|
||||||
private val personalCounterPattern = Regex("<counter_personal:([^>]+)>")
|
|
||||||
private val dailyCounterPattern = Regex("<daily_counter:([^>]+)>")
|
|
||||||
private val namePattern = Regex("<name>")
|
|
||||||
private val followPattern = Regex("<following>")
|
|
||||||
private val daysPattern = """<days:(\d{4})-(\d{2})-(\d{2})>""".toRegex()
|
|
||||||
|
|
||||||
init {
|
|
||||||
reloadCommand()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun reloadCommand() {
|
|
||||||
val user = UserService.getUser(channel.channelId)
|
|
||||||
?: throw RuntimeException("User not found. it's bug? ${channel.channelName} - ${channel.channelId}")
|
|
||||||
val commands = CommandService.getCommands(user)
|
|
||||||
val manageCommands = mapOf("!명령어추가" to this::manageAddCommand, "!명령어삭제" to this::manageRemoveCommand, "!명령어수정" to this::manageUpdateCommand)
|
|
||||||
|
|
||||||
manageCommands.forEach { (commandName, command) ->
|
|
||||||
this.commands[commandName] = command
|
|
||||||
}
|
|
||||||
|
|
||||||
commands.map {
|
|
||||||
this.commands.put(it.command.lowercase()) { msg, user ->
|
|
||||||
logger.debug("${channel.channelName} - ${it.command} - ${it.content}/${it.failContent}")
|
|
||||||
|
|
||||||
val result = replaceCounters(Pair(it.content, it.failContent), user, msg, listener, msg.profile?.nickname ?: "")
|
|
||||||
listener.sendChat(result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun manageAddCommand(msg: ChatMessage, user: User) {
|
|
||||||
if (msg.profile?.userRoleCode == "common_user") {
|
|
||||||
listener.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val parts = msg.content.split(" ", limit = 3)
|
|
||||||
if (parts.size < 3) {
|
|
||||||
listener.sendChat("명령어 추가 형식은 '!명령어추가 명령어 내용'입니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (commands.containsKey(parts[1])) {
|
|
||||||
listener.sendChat("${parts[1]} 명령어는 이미 있는 명령어입니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val command = parts[1]
|
|
||||||
val content = parts[2]
|
|
||||||
CommandService.saveCommand(user, command, content, "")
|
|
||||||
listener.sendChat("명령어 '$command' 추가되었습니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun manageUpdateCommand(msg: ChatMessage, user: User) {
|
|
||||||
if (msg.profile?.userRoleCode == "common_user") {
|
|
||||||
listener.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val parts = msg.content.split(" ", limit = 3)
|
|
||||||
if (parts.size < 3) {
|
|
||||||
listener.sendChat("명령어 수정 형식은 '!명령어수정 명령어 내용'입니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (!commands.containsKey(parts[1])) {
|
|
||||||
listener.sendChat("${parts[1]} 명령어는 없는 명령어입니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val command = parts[1]
|
|
||||||
val content = parts[2]
|
|
||||||
CommandService.updateCommand(user, command, content, "")
|
|
||||||
listener.sendChat("명령어 '$command' 수정되었습니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun manageRemoveCommand(msg: ChatMessage, user: User) {
|
|
||||||
if (msg.profile?.userRoleCode == "common_user") {
|
|
||||||
listener.sendChat("매니저만 명령어를 삭제할 수 있습니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val parts = msg.content.split(" ", limit = 2)
|
|
||||||
if (parts.size < 2) {
|
|
||||||
listener.sendChat("명령어 삭제 형식은 '!명령어삭제 명령어'입니다.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val command = parts[1]
|
|
||||||
CommandService.removeCommand(user, command)
|
|
||||||
listener.sendChat("명령어 '$command' 삭제되었습니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun handle(msg: ChatMessage, user: User) {
|
|
||||||
val commandKey = msg.content.split(' ')[0]
|
|
||||||
|
|
||||||
commands[commandKey.lowercase()]?.let { it(msg, user) }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun replaceCounters(chat: Pair<String, String>, user: User, msg: ChatMessage, listener: ChzzkChat, userName: String): String {
|
|
||||||
var result = chat.first
|
|
||||||
var isFail = false
|
|
||||||
|
|
||||||
// Replace dailyCounterPattern
|
|
||||||
result = dailyCounterPattern.replace(result) { matchResult ->
|
|
||||||
val name = matchResult.groupValues[1]
|
|
||||||
val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user)
|
|
||||||
|
|
||||||
if (dailyCounter.second) {
|
|
||||||
CounterService.updateDailyCounterValue(name, msg.userId, 1, user).first.toString()
|
|
||||||
} else {
|
|
||||||
isFail = true
|
|
||||||
dailyCounter.first.toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle fail case
|
|
||||||
if (isFail && chat.second.isNotEmpty()) {
|
|
||||||
result = chat.second
|
|
||||||
result = dailyCounterPattern.replace(result) { matchResult ->
|
|
||||||
val name = matchResult.groupValues[1]
|
|
||||||
val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user)
|
|
||||||
dailyCounter.first.toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace followPattern
|
|
||||||
result = followPattern.replace(result) { matchResult ->
|
|
||||||
try {
|
|
||||||
val followingDate = getFollowDate(listener.chatId, msg.userId)
|
|
||||||
.content.streamingProperty.following?.followDate
|
|
||||||
|
|
||||||
val period = followingDate?.let {
|
|
||||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
|
||||||
val pastDate = LocalDateTime.parse(it, formatter)
|
|
||||||
val today = LocalDateTime.now()
|
|
||||||
ChronoUnit.DAYS.between(pastDate, today)
|
|
||||||
} ?: 0
|
|
||||||
|
|
||||||
period.toString()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error(e.message)
|
|
||||||
"0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace daysPattern
|
|
||||||
result = daysPattern.replace(result) { matchResult ->
|
|
||||||
val (year, month, day) = matchResult.destructured
|
|
||||||
val pastDate = LocalDateTime.of(year.toInt(), month.toInt(), day.toInt(), 0, 0, 0)
|
|
||||||
val today = LocalDateTime.now()
|
|
||||||
|
|
||||||
val daysBetween = ChronoUnit.DAYS.between(pastDate, today)
|
|
||||||
daysBetween.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace counterPattern
|
|
||||||
result = counterPattern.replace(result) { matchResult ->
|
|
||||||
val name = matchResult.groupValues[1]
|
|
||||||
CounterService.updateCounterValue(name, 1, user).toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace personalCounterPattern
|
|
||||||
result = personalCounterPattern.replace(result) { matchResult ->
|
|
||||||
val name = matchResult.groupValues[1]
|
|
||||||
CounterService.updatePersonalCounterValue(name, msg.userId, 1, user).toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace namePattern
|
|
||||||
result = namePattern.replace(result, userName)
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
48
webserver/build.gradle.kts
Normal file
48
webserver/build.gradle.kts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm")
|
||||||
|
kotlin("plugin.serialization").version("2.0.0")
|
||||||
|
}
|
||||||
|
|
||||||
|
group = project.rootProject.group
|
||||||
|
version = project.rootProject.version
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
val ktorVersion = "2.3.12"
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("io.ktor:ktor-server-core:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-server-netty:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-server-websockets:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-server-swagger:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-server-content-negotiation:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-server-cors:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-server-swagger:$ktorVersion")
|
||||||
|
|
||||||
|
implementation("io.swagger.codegen.v3:swagger-codegen-generators:1.0.50")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
|
||||||
|
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect
|
||||||
|
implementation("org.jetbrains.kotlin:kotlin-reflect:2.0.0")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/io.insert-koin/koin-core
|
||||||
|
implementation("io.insert-koin:koin-core:4.0.0-RC1")
|
||||||
|
|
||||||
|
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
|
||||||
|
implementation("ch.qos.logback:logback-classic:1.5.6")
|
||||||
|
|
||||||
|
implementation(project(":common"))
|
||||||
|
|
||||||
|
testImplementation(kotlin("test"))
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
kotlin {
|
||||||
|
jvmToolchain(21)
|
||||||
|
}
|
@@ -0,0 +1,57 @@
|
|||||||
|
package space.mori.chzzk_bot.webserver
|
||||||
|
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.serialization.kotlinx.*
|
||||||
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.engine.*
|
||||||
|
import io.ktor.server.netty.*
|
||||||
|
import io.ktor.server.plugins.contentnegotiation.*
|
||||||
|
import io.ktor.server.plugins.cors.routing.*
|
||||||
|
import io.ktor.server.plugins.swagger.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
import io.ktor.server.websocket.*
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import space.mori.chzzk_bot.webserver.routes.*
|
||||||
|
import java.time.Duration
|
||||||
|
|
||||||
|
val server = embeddedServer(Netty, port = 8080) {
|
||||||
|
install(WebSockets) {
|
||||||
|
pingPeriod = Duration.ofSeconds(15)
|
||||||
|
timeout = Duration.ofSeconds(15)
|
||||||
|
maxFrameSize = Long.MAX_VALUE
|
||||||
|
masking = false
|
||||||
|
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||||
|
}
|
||||||
|
|
||||||
|
install(ContentNegotiation) {
|
||||||
|
json(Json {
|
||||||
|
prettyPrint = true
|
||||||
|
isLenient = true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
install(CORS) {
|
||||||
|
anyHost()
|
||||||
|
allowHeader(HttpHeaders.ContentType)
|
||||||
|
}
|
||||||
|
routing {
|
||||||
|
apiRoutes()
|
||||||
|
apiSongRoutes()
|
||||||
|
wsTimerRoutes()
|
||||||
|
wsSongRoutes()
|
||||||
|
wsSongListRoutes()
|
||||||
|
swaggerUI("swagger-ui/index.html", "openapi/documentation.yaml") {
|
||||||
|
options {
|
||||||
|
version = "1.2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun start() {
|
||||||
|
server.start(wait = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stop() {
|
||||||
|
server.stop()
|
||||||
|
}
|
@@ -0,0 +1,100 @@
|
|||||||
|
package space.mori.chzzk_bot.webserver.routes
|
||||||
|
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import space.mori.chzzk_bot.common.services.SongConfigService
|
||||||
|
import space.mori.chzzk_bot.common.utils.getStreamInfo
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class GetUserDTO(
|
||||||
|
val uid: String,
|
||||||
|
val nickname: String,
|
||||||
|
val isStreamOn: Boolean,
|
||||||
|
val avatarUrl: String
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class GetSessionDTO(
|
||||||
|
val uid: String,
|
||||||
|
val nickname: String,
|
||||||
|
val isStreamOn: Boolean,
|
||||||
|
val avatarUrl: String,
|
||||||
|
val maxQueueSize: Int,
|
||||||
|
val maxUserSize: Int,
|
||||||
|
val isStreamerOnly: Boolean,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Routing.apiRoutes() {
|
||||||
|
route("/") {
|
||||||
|
get {
|
||||||
|
call.respondText("Hello World!", status =
|
||||||
|
HttpStatusCode.OK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
route("/health") {
|
||||||
|
get {
|
||||||
|
call.respondText("OK", status= HttpStatusCode.OK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
route("/user/{uid}") {
|
||||||
|
get {
|
||||||
|
val uid = call.parameters["uid"]
|
||||||
|
if(uid == null) {
|
||||||
|
call.respondText("Require UID", status = HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
}
|
||||||
|
val user = getStreamInfo(uid)
|
||||||
|
if(user.content == null) {
|
||||||
|
call.respondText("User not found", status = HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.OK, GetUserDTO(
|
||||||
|
user.content!!.channel.channelId,
|
||||||
|
user.content!!.channel.channelName,
|
||||||
|
user.content!!.status == "OPEN",
|
||||||
|
user.content!!.channel.channelImageUrl
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
route("/user") {
|
||||||
|
get {
|
||||||
|
call.respondText("Require UID", status = HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
route("/session/{sid}") {
|
||||||
|
get {
|
||||||
|
val sid = call.parameters["sid"]
|
||||||
|
if(sid == null) {
|
||||||
|
call.respondText("Require SID", status = HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
}
|
||||||
|
val user = SongConfigService.getUserByToken(sid)
|
||||||
|
val session = SongConfigService.getConfig(sid)
|
||||||
|
if(user == null) {
|
||||||
|
call.respondText("User not found", status = HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
} else {
|
||||||
|
val chzzkUser = getStreamInfo(user.token)
|
||||||
|
call.respond(HttpStatusCode.OK, GetSessionDTO(
|
||||||
|
chzzkUser.content!!.channel.channelId,
|
||||||
|
chzzkUser.content!!.channel.channelName,
|
||||||
|
chzzkUser.content!!.status == "OPEN",
|
||||||
|
chzzkUser.content!!.channel.channelImageUrl,
|
||||||
|
session!!.queueLimit,
|
||||||
|
session.personalLimit,
|
||||||
|
session.streamerOnly
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
route("/session") {
|
||||||
|
get {
|
||||||
|
call.respondText("Require SID", status = HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,48 @@
|
|||||||
|
package space.mori.chzzk_bot.webserver.routes
|
||||||
|
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import space.mori.chzzk_bot.common.models.SongList
|
||||||
|
import space.mori.chzzk_bot.common.services.SongListService
|
||||||
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SongsDTO(
|
||||||
|
val url: String,
|
||||||
|
val name: String,
|
||||||
|
val author: String,
|
||||||
|
val time: Int,
|
||||||
|
val reqName: String
|
||||||
|
)
|
||||||
|
|
||||||
|
fun SongList.toDTO(): SongsDTO = SongsDTO(
|
||||||
|
this.url,
|
||||||
|
this.name,
|
||||||
|
this.author,
|
||||||
|
this.time,
|
||||||
|
this.reqName
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Routing.apiSongRoutes() {
|
||||||
|
route("/songs/{uid}") {
|
||||||
|
get {
|
||||||
|
val uid = call.parameters["uid"]
|
||||||
|
val user = uid?.let { it1 -> UserService.getUser(it1) }
|
||||||
|
if (user == null) {
|
||||||
|
call.respondText("No user found", status = HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
}
|
||||||
|
|
||||||
|
val songs = SongListService.getSong(user)
|
||||||
|
call.respond(HttpStatusCode.OK, songs.map { it.toDTO() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
route("/songs") {
|
||||||
|
get {
|
||||||
|
call.respondText("Require UID", status= HttpStatusCode.BadRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,210 @@
|
|||||||
|
package space.mori.chzzk_bot.webserver.routes
|
||||||
|
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
import io.ktor.server.websocket.*
|
||||||
|
import io.ktor.websocket.*
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import space.mori.chzzk_bot.common.events.*
|
||||||
|
import space.mori.chzzk_bot.common.models.Counters.withDefinition
|
||||||
|
import space.mori.chzzk_bot.common.services.SongConfigService
|
||||||
|
import space.mori.chzzk_bot.common.services.SongListService
|
||||||
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
import space.mori.chzzk_bot.common.utils.getYoutubeVideo
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
|
||||||
|
fun Routing.wsSongListRoutes() {
|
||||||
|
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
||||||
|
val status = ConcurrentHashMap<String, SongType>()
|
||||||
|
val logger = LoggerFactory.getLogger("WSSongListRoutes")
|
||||||
|
|
||||||
|
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
|
fun addSession(sid: String, session: WebSocketServerSession) {
|
||||||
|
sessions.computeIfAbsent(sid) { ConcurrentLinkedQueue() }.add(session)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeSession(sid: String, session: WebSocketServerSession) {
|
||||||
|
sessions[sid]?.remove(session)
|
||||||
|
if(sessions[sid]?.isEmpty() == true) {
|
||||||
|
sessions.remove(sid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
webSocket("/songlist/{sid}") {
|
||||||
|
val sid = call.parameters["sid"]
|
||||||
|
val session = sid?.let { SongConfigService.getConfig(it) }
|
||||||
|
val user = sid?.let {SongConfigService.getUserByToken(sid) }
|
||||||
|
if (sid == null) {
|
||||||
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID"))
|
||||||
|
return@webSocket
|
||||||
|
}
|
||||||
|
if (user == null || session == null) {
|
||||||
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID"))
|
||||||
|
return@webSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
addSession(sid, this)
|
||||||
|
|
||||||
|
if(status[sid] == SongType.STREAM_OFF) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sendSerialized(SongResponse(
|
||||||
|
SongType.STREAM_OFF.value,
|
||||||
|
user.token,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
))
|
||||||
|
}
|
||||||
|
removeSession(sid, this)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (frame in incoming) {
|
||||||
|
when(frame) {
|
||||||
|
is Frame.Text -> {
|
||||||
|
val data = frame.readText().let { Json.decodeFromString<SongRequest>(it) }
|
||||||
|
|
||||||
|
if(data.maxQueue != null && data.maxQueue > 0) SongConfigService.updateQueueLimit(user, data.maxQueue)
|
||||||
|
if(data.maxUserLimit != null && data.maxUserLimit > 0) SongConfigService.updatePersonalLimit(user, data.maxUserLimit)
|
||||||
|
if(data.isStreamerOnly != null) SongConfigService.updateStreamerOnly(user, data.isStreamerOnly)
|
||||||
|
|
||||||
|
if(data.type == SongType.ADD.value && data.url != null) {
|
||||||
|
try {
|
||||||
|
val youtubeVideo = getYoutubeVideo(data.url)
|
||||||
|
if (youtubeVideo != null) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
SongListService.saveSong(
|
||||||
|
user,
|
||||||
|
user.token,
|
||||||
|
data.url,
|
||||||
|
youtubeVideo.name,
|
||||||
|
youtubeVideo.author,
|
||||||
|
youtubeVideo.length,
|
||||||
|
user.username
|
||||||
|
)
|
||||||
|
dispatcher.post(
|
||||||
|
SongEvent(
|
||||||
|
user.token,
|
||||||
|
SongType.ADD,
|
||||||
|
user.token,
|
||||||
|
user.username,
|
||||||
|
youtubeVideo.name,
|
||||||
|
youtubeVideo.author,
|
||||||
|
youtubeVideo.length,
|
||||||
|
youtubeVideo.url
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(e: Exception) {
|
||||||
|
logger.debug("SongType.ADD Error: {} / {}", session.token, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(data.type == SongType.REMOVE.value && data.url != null) {
|
||||||
|
dispatcher.post(SongEvent(
|
||||||
|
user.token,
|
||||||
|
SongType.REMOVE,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
0,
|
||||||
|
data.url
|
||||||
|
))
|
||||||
|
} else if(data.type == SongType.NEXT.value) {
|
||||||
|
val song = SongListService.getSong(user)[0]
|
||||||
|
SongListService.deleteSong(user, song.uid, song.name)
|
||||||
|
dispatcher.post(SongEvent(
|
||||||
|
user.token,
|
||||||
|
SongType.NEXT,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is Frame.Ping -> send(Frame.Pong(frame.data))
|
||||||
|
else -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(e: ClosedReceiveChannelException) {
|
||||||
|
logger.error("Error in WebSocket: ${e.message}")
|
||||||
|
} finally {
|
||||||
|
removeSession(sid, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatcher.subscribe(SongEvent::class) {
|
||||||
|
logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.name)
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
val user = UserService.getUser(it.uid)
|
||||||
|
if(user != null) {
|
||||||
|
val session = SongConfigService.getConfig(user)
|
||||||
|
sessions[session.token ?: ""]?.forEach { ws ->
|
||||||
|
ws.sendSerialized(
|
||||||
|
SongResponse(
|
||||||
|
it.type.value,
|
||||||
|
it.uid,
|
||||||
|
it.reqUid,
|
||||||
|
it.name,
|
||||||
|
it.author,
|
||||||
|
it.time,
|
||||||
|
it.url
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
|
if(it.type == TimerType.STREAM_OFF) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
val user = UserService.getUser(it.uid)
|
||||||
|
if(user != null) {
|
||||||
|
val session = SongConfigService.getConfig(user)
|
||||||
|
|
||||||
|
sessions[session.token ?: ""]?.forEach { ws ->
|
||||||
|
ws.sendSerialized(
|
||||||
|
SongResponse(
|
||||||
|
it.type.value,
|
||||||
|
it.uid,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
)
|
||||||
|
removeSession(session.token ?: "", ws)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SongRequest(
|
||||||
|
val type: Int,
|
||||||
|
val uid: String,
|
||||||
|
val url: String?,
|
||||||
|
val maxQueue: Int?,
|
||||||
|
val maxUserLimit: Int?,
|
||||||
|
val isStreamerOnly: Boolean?,
|
||||||
|
val remove: Int?
|
||||||
|
)
|
@@ -0,0 +1,127 @@
|
|||||||
|
package space.mori.chzzk_bot.webserver.routes
|
||||||
|
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
import io.ktor.server.websocket.*
|
||||||
|
import io.ktor.websocket.*
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import space.mori.chzzk_bot.common.events.*
|
||||||
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
|
||||||
|
fun Routing.wsSongRoutes() {
|
||||||
|
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
||||||
|
val status = ConcurrentHashMap<String, SongType>()
|
||||||
|
val logger = LoggerFactory.getLogger("WSSongRoutes")
|
||||||
|
|
||||||
|
fun addSession(uid: String, session: WebSocketServerSession) {
|
||||||
|
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeSession(uid: String, session: WebSocketServerSession) {
|
||||||
|
sessions[uid]?.remove(session)
|
||||||
|
if(sessions[uid]?.isEmpty() == true) {
|
||||||
|
sessions.remove(uid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
webSocket("/song/{uid}") {
|
||||||
|
val uid = call.parameters["uid"]
|
||||||
|
val user = uid?.let { UserService.getUser(it) }
|
||||||
|
if (uid == null) {
|
||||||
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
||||||
|
return@webSocket
|
||||||
|
}
|
||||||
|
if (user == null) {
|
||||||
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
||||||
|
return@webSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
addSession(uid, this)
|
||||||
|
|
||||||
|
if(status[uid] == SongType.STREAM_OFF) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sendSerialized(SongResponse(
|
||||||
|
SongType.STREAM_OFF.value,
|
||||||
|
uid,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (frame in incoming) {
|
||||||
|
when(frame) {
|
||||||
|
is Frame.Text -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
is Frame.Ping -> send(Frame.Pong(frame.data))
|
||||||
|
else -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(e: ClosedReceiveChannelException) {
|
||||||
|
logger.error("Error in WebSocket: ${e.message}")
|
||||||
|
} finally {
|
||||||
|
removeSession(uid, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
|
dispatcher.subscribe(SongEvent::class) {
|
||||||
|
logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.name)
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sessions[it.uid]?.forEach { ws ->
|
||||||
|
ws.sendSerialized(SongResponse(
|
||||||
|
it.type.value,
|
||||||
|
it.uid,
|
||||||
|
it.reqUid,
|
||||||
|
it.name,
|
||||||
|
it.author,
|
||||||
|
it.time,
|
||||||
|
it.url
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
|
if(it.type == TimerType.STREAM_OFF) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sessions[it.uid]?.forEach { ws ->
|
||||||
|
ws.sendSerialized(SongResponse(
|
||||||
|
it.type.value,
|
||||||
|
it.uid,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SongResponse(
|
||||||
|
val type: Int,
|
||||||
|
val uid: String,
|
||||||
|
val reqUid: String?,
|
||||||
|
val name: String?,
|
||||||
|
val author: String?,
|
||||||
|
val time: Int?,
|
||||||
|
val url: String?
|
||||||
|
)
|
@@ -0,0 +1,98 @@
|
|||||||
|
package space.mori.chzzk_bot.webserver.routes
|
||||||
|
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
import io.ktor.server.websocket.*
|
||||||
|
import io.ktor.websocket.*
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import space.mori.chzzk_bot.common.events.*
|
||||||
|
import space.mori.chzzk_bot.common.services.TimerConfigService
|
||||||
|
import space.mori.chzzk_bot.common.services.UserService
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
|
||||||
|
fun Routing.wsTimerRoutes() {
|
||||||
|
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
||||||
|
val status = ConcurrentHashMap<String, TimerType>()
|
||||||
|
val logger = LoggerFactory.getLogger("WSTimerRoutes")
|
||||||
|
|
||||||
|
fun addSession(uid: String, session: WebSocketServerSession) {
|
||||||
|
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeSession(uid: String, session: WebSocketServerSession) {
|
||||||
|
sessions[uid]?.remove(session)
|
||||||
|
if(sessions[uid]?.isEmpty() == true) {
|
||||||
|
sessions.remove(uid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
webSocket("/timer/{uid}") {
|
||||||
|
val uid = call.parameters["uid"]
|
||||||
|
val user = uid?.let { UserService.getUser(it) }
|
||||||
|
if (uid == null) {
|
||||||
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
||||||
|
return@webSocket
|
||||||
|
}
|
||||||
|
if (user == null) {
|
||||||
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
||||||
|
return@webSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
addSession(uid, this)
|
||||||
|
|
||||||
|
if(status[uid] == TimerType.STREAM_OFF) {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sendSerialized(TimerResponse(
|
||||||
|
TimerConfigService.getConfig(user)?.option ?: TimerType.REMOVE.value,
|
||||||
|
null
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (frame in incoming) {
|
||||||
|
when(frame) {
|
||||||
|
is Frame.Text -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
is Frame.Ping -> send(Frame.Pong(frame.data))
|
||||||
|
else -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(e: ClosedReceiveChannelException) {
|
||||||
|
logger.error("Error in WebSocket: ${e.message}")
|
||||||
|
} finally {
|
||||||
|
removeSession(uid, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
|
logger.debug("TimerEvent: {} / {}", it.uid, it.type)
|
||||||
|
status[it.uid] = it.type
|
||||||
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
|
sessions[it.uid]?.forEach { ws ->
|
||||||
|
ws.sendSerialized(TimerResponse(it.type.value, it.time ?: ""))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class TimerResponse(
|
||||||
|
val type: Int,
|
||||||
|
val time: String?
|
||||||
|
)
|
182
webserver/src/main/resources/openapi/documentation.yaml
Normal file
182
webserver/src/main/resources/openapi/documentation.yaml
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
openapi: "3.1.0"
|
||||||
|
info:
|
||||||
|
title: "chzzk_bot API"
|
||||||
|
description: "chzzk_bot API"
|
||||||
|
version: "1.0.0"
|
||||||
|
servers:
|
||||||
|
- url: "http://localhost:8080"
|
||||||
|
paths:
|
||||||
|
/:
|
||||||
|
get:
|
||||||
|
description: ""
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: "OK"
|
||||||
|
content:
|
||||||
|
text/plain:
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
examples:
|
||||||
|
Example#1:
|
||||||
|
value: "Hello World!"
|
||||||
|
/health:
|
||||||
|
get:
|
||||||
|
description: ""
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: "OK"
|
||||||
|
content:
|
||||||
|
text/plain:
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
examples:
|
||||||
|
Example#1:
|
||||||
|
value: "OK"
|
||||||
|
/song/{uid}:
|
||||||
|
get:
|
||||||
|
description: ""
|
||||||
|
parameters:
|
||||||
|
- name: "uid"
|
||||||
|
in: "path"
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
- name: "Connection"
|
||||||
|
in: "header"
|
||||||
|
required: true
|
||||||
|
description: "Websocket Connection parameter"
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
- name: "Upgrade"
|
||||||
|
in: "header"
|
||||||
|
required: true
|
||||||
|
description: "Websocket Upgrade parameter"
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
- name: "Sec-WebSocket-Key"
|
||||||
|
in: "header"
|
||||||
|
required: true
|
||||||
|
description: "Websocket Sec-WebSocket-Key parameter"
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
responses:
|
||||||
|
"101":
|
||||||
|
description: "Switching Protocols"
|
||||||
|
headers:
|
||||||
|
Connection:
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
Upgrade:
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
Sec-WebSocket-Accept:
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
/songs:
|
||||||
|
get:
|
||||||
|
description: ""
|
||||||
|
responses:
|
||||||
|
"400":
|
||||||
|
description: "Bad Request"
|
||||||
|
content:
|
||||||
|
text/plain:
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
examples:
|
||||||
|
Example#1:
|
||||||
|
value: "Require UID"
|
||||||
|
/songs/{uid}:
|
||||||
|
get:
|
||||||
|
description: ""
|
||||||
|
parameters:
|
||||||
|
- name: "uid"
|
||||||
|
in: "path"
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
responses:
|
||||||
|
"404":
|
||||||
|
description: "Not Found"
|
||||||
|
content:
|
||||||
|
text/plain:
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
examples:
|
||||||
|
Example#1:
|
||||||
|
value: "No user found"
|
||||||
|
"200":
|
||||||
|
description: "OK"
|
||||||
|
content:
|
||||||
|
'*/*':
|
||||||
|
schema:
|
||||||
|
type: "array"
|
||||||
|
items:
|
||||||
|
$ref: "#/components/schemas/SongList"
|
||||||
|
/timer/{uid}:
|
||||||
|
get:
|
||||||
|
description: ""
|
||||||
|
parameters:
|
||||||
|
- name: "uid"
|
||||||
|
in: "path"
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
- name: "Connection"
|
||||||
|
in: "header"
|
||||||
|
required: true
|
||||||
|
description: "Websocket Connection parameter"
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
- name: "Upgrade"
|
||||||
|
in: "header"
|
||||||
|
required: true
|
||||||
|
description: "Websocket Upgrade parameter"
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
- name: "Sec-WebSocket-Key"
|
||||||
|
in: "header"
|
||||||
|
required: true
|
||||||
|
description: "Websocket Sec-WebSocket-Key parameter"
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
responses:
|
||||||
|
"101":
|
||||||
|
description: "Switching Protocols"
|
||||||
|
headers:
|
||||||
|
Connection:
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
Upgrade:
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
Sec-WebSocket-Accept:
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: "string"
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Object:
|
||||||
|
type: "object"
|
||||||
|
properties: {}
|
||||||
|
ResultRow:
|
||||||
|
type: "object"
|
||||||
|
properties:
|
||||||
|
fieldIndex:
|
||||||
|
type: "object"
|
||||||
|
required:
|
||||||
|
- "fieldIndex"
|
||||||
|
SongList:
|
||||||
|
type: "object"
|
||||||
|
properties:
|
||||||
|
writeValues:
|
||||||
|
$ref: "#/components/schemas/Object"
|
||||||
|
_readValues:
|
||||||
|
$ref: "#/components/schemas/ResultRow"
|
||||||
|
required:
|
||||||
|
- "id"
|
||||||
|
- "writeValues"
|
Reference in New Issue
Block a user