From eccf1a29bcdef784b005a6a9e329da865cb309e7 Mon Sep 17 00:00:00 2001
From: dalbodeule <11470513+dalbodeule@users.noreply.github.com>
Date: Mon, 9 Dec 2024 14:47:41 +0900
Subject: [PATCH] add botIsDisabled, welcomeMessageDisabled.
---
.idea/sqldialects.xml | 4 ++
.../chzzk_bot/chatbot/chzzk/ChzzkHandler.kt | 15 +++++-
.../common/events/BotEnabledEvent.kt | 8 +++
.../mori/chzzk_bot/common/models/User.kt | 2 +
.../chzzk_bot/common/services/UserService.kt | 16 ++++++
.../chzzk_bot/webserver/routes/ApiRoutes.kt | 51 ++++++++++++++++++-
6 files changed, 93 insertions(+), 3 deletions(-)
create mode 100644 common/src/main/kotlin/space/mori/chzzk_bot/common/events/BotEnabledEvent.kt
diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml
index 63772a3..b423331 100644
--- a/.idea/sqldialects.xml
+++ b/.idea/sqldialects.xml
@@ -3,4 +3,8 @@
+
+
+
+
\ No newline at end of file
diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt
index 614b303..69de373 100644
--- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt
+++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt
@@ -39,7 +39,7 @@ object ChzzkHandler {
fun enable() {
botUid = chzzk.loggedUser.userId
UserService.getAllUsers().map {
- if(it.token != null)
+ if(it.token != null && !it.isDisabled)
chzzk.getChannel(it.token)?.let { token -> addUser(token, it) }
}
@@ -55,9 +55,22 @@ object ChzzkHandler {
addUser(channel, user)
}
}
+
dispatcher.subscribe(CommandReloadEvent::class) {
handlers.firstOrNull { handlers -> handlers.channel.channelId == it.uid }?.reloadCommand()
}
+
+ dispatcher.subscribe(BotEnabledEvent::class) {
+ if(it.isDisabled) {
+ handlers.removeIf { handlers -> handlers.channel.channelId == it.chzzkId }
+ } else {
+ val channel = getChannel(it.chzzkId)
+ val user = UserService.getUser(it.chzzkId)
+ if(channel != null && user != null) {
+ addUser(channel, user)
+ }
+ }
+ }
}
fun disable() {
diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/events/BotEnabledEvent.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/events/BotEnabledEvent.kt
new file mode 100644
index 0000000..c59e277
--- /dev/null
+++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/events/BotEnabledEvent.kt
@@ -0,0 +1,8 @@
+package space.mori.chzzk_bot.common.events
+
+data class BotEnabledEvent(
+ val chzzkId: String,
+ val isDisabled: Boolean,
+): Event {
+ val TAG = javaClass.simpleName
+}
\ No newline at end of file
diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/models/User.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/models/User.kt
index 207ea1f..6becf58 100644
--- a/common/src/main/kotlin/space/mori/chzzk_bot/common/models/User.kt
+++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/models/User.kt
@@ -15,6 +15,7 @@ object Users: IntIdTable("users") {
val liveAlertChannel = long("live_alert_channel").nullable()
val liveAlertMessage = text("live_alert_message").nullable()
val isDisableStartupMsg = bool("is_disable_startup_msg").default(false)
+ val isDisabled = bool("is_disabled").default(false)
}
class User(id: EntityID) : IntEntity(id) {
@@ -28,6 +29,7 @@ class User(id: EntityID) : IntEntity(id) {
var liveAlertChannel by Users.liveAlertChannel
var liveAlertMessage by Users.liveAlertMessage
var isDisableStartupMsg by Users.isDisableStartupMsg
+ var isDisabled by Users.isDisabled
// 유저가 가진 매니저들
var managers by User.via(UserManagers.user, UserManagers.manager)
diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/services/UserService.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/services/UserService.kt
index 67df6af..64a2258 100644
--- a/common/src/main/kotlin/space/mori/chzzk_bot/common/services/UserService.kt
+++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/services/UserService.kt
@@ -89,4 +89,20 @@ object UserService {
user
}
}
+
+ fun setIsDisabled(user: User, disabled: Boolean): User {
+ return transaction {
+ user.isDisabled = disabled
+
+ user
+ }
+ }
+
+ fun setIsStartupDisabled(user: User, disabled: Boolean): User {
+ return transaction {
+ user.isDisableStartupMsg = disabled
+
+ user
+ }
+ }
}
\ No newline at end of file
diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt
index c7b9046..1b56761 100644
--- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt
+++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiRoutes.kt
@@ -1,7 +1,6 @@
package space.mori.chzzk_bot.webserver.routes
import io.ktor.http.*
-import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
@@ -41,13 +40,19 @@ data class GetSessionDTO(
val isDisabled: Boolean
)
+@Serializable
+data class GetSettingDTO(
+ val isBotDisabled: Boolean,
+ val isBotMsgDisabled: Boolean,
+)
+
@Serializable
data class RegisterChzzkUserDTO(
val chzzkUrl: String
)
fun Routing.apiRoutes() {
- val chzzkIDRegex = """(?:.+chzzk\.naver\.com\/)?([a-f0-9]{32})(?:.+)?""".toRegex()
+ val chzzkIDRegex = """(?:.+chzzk\.naver\.com/)?([a-f0-9]{32})(?:.+)?""".toRegex()
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
route("/") {
@@ -183,4 +188,46 @@ fun Routing.apiRoutes() {
return@post
}
}
+
+ route("/settings") {
+ get {
+ val session = call.sessions.get()
+ if(session == null) {
+ call.respondText("No session found", status = HttpStatusCode.Unauthorized)
+ return@get
+ }
+
+ val user = UserService.getUserWithNaverId(session.id)
+ if(user == null) {
+ call.respondText("No session found", status = HttpStatusCode.Unauthorized)
+ return@get
+ }
+
+ call.respond(GetSettingDTO(
+ user.isDisabled, user.isDisableStartupMsg
+ ))
+ }
+ post {
+ val session = call.sessions.get()
+ if(session == null) {
+ call.respondText("No session found", status = HttpStatusCode.Unauthorized)
+ return@post
+ }
+
+ val body: GetSettingDTO = call.receive()
+
+ val user = UserService.getUserWithNaverId(session.id)
+ if(user == null) {
+ call.respondText("No session found", status = HttpStatusCode.Unauthorized)
+ return@post
+ }
+
+ UserService.setIsDisabled(user, body.isBotDisabled)
+ UserService.setIsStartupDisabled(user, body.isBotMsgDisabled)
+
+ call.respond(GetSettingDTO(
+ user.isDisabled, user.isDisableStartupMsg
+ ))
+ }
+ }
}
\ No newline at end of file