add botIsDisabled, welcomeMessageDisabled.

This commit is contained in:
dalbodeule 2024-12-09 14:47:41 +09:00
parent 4fca9df9c2
commit eccf1a29bc
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
6 changed files with 93 additions and 3 deletions

4
.idea/sqldialects.xml generated
View File

@ -3,4 +3,8 @@
<component name="SqlDialectMappings">
<file url="PROJECT" dialect="MariaDB" />
</component>
<component name="SqlResolveMappings">
<file url="file://$PROJECT_DIR$/common/src/main/kotlin/space/mori/chzzk_bot/common/models/User.kt" scope="{&quot;node&quot;:{ &quot;@negative&quot;:&quot;1&quot;, &quot;group&quot;:{ &quot;@kind&quot;:&quot;root&quot;, &quot;node&quot;:{ &quot;name&quot;:{ &quot;@qname&quot;:&quot;90f8ee11-600e-4155-a316-e8062c7c828b&quot; }, &quot;group&quot;:{ &quot;@kind&quot;:&quot;schema&quot;, &quot;node&quot;:{ &quot;name&quot;:{ &quot;@qname&quot;:&quot;chzzk&quot; } } } } } }}" />
<file url="PROJECT" scope="{&quot;node&quot;:{ &quot;@negative&quot;:&quot;1&quot;, &quot;group&quot;:{ &quot;@kind&quot;:&quot;root&quot;, &quot;node&quot;:{ &quot;name&quot;:{ &quot;@qname&quot;:&quot;90f8ee11-600e-4155-a316-e8062c7c828b&quot; }, &quot;group&quot;:{ &quot;@kind&quot;:&quot;schema&quot;, &quot;node&quot;:{ &quot;name&quot;:{ &quot;@qname&quot;:&quot;chzzk&quot; } } } } } }}" />
</component>
</project>

View File

@ -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() {

View File

@ -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
}

View File

@ -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<Int>) : IntEntity(id) {
@ -28,6 +29,7 @@ class User(id: EntityID<Int>) : 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)

View File

@ -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
}
}
}

View File

@ -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<UserSession>()
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<UserSession>()
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
))
}
}
}