From 55bb56afdba3bab7e3e13a042e888c808cc2b3d4 Mon Sep 17 00:00:00 2001 From: qingshu-ui Date: Tue, 8 Oct 2024 10:52:47 +0800 Subject: [PATCH] redundant clean --- build.gradle.kts | 3 +- .../ayaka/example/plugin/AdminCommand.kt | 102 ++++++++---------- .../ayaka/example/plugin/DriftBottle.kt | 70 ++++++------ .../ayaka/example/plugin/RandomVideo.kt | 18 ++-- .../qingshu/ayaka/example/plugin/Roulette.kt | 12 +-- .../example/service/PermissionManager.kt | 66 ------------ 6 files changed, 96 insertions(+), 175 deletions(-) delete mode 100644 src/main/kotlin/io/github/qingshu/ayaka/example/service/PermissionManager.kt diff --git a/build.gradle.kts b/build.gradle.kts index 8c15390..a3e5e47 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -31,13 +31,14 @@ repositories { dependencies { implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation ("com.github.qingshu-ui:ayaka-spring-boot-starter:1.0.1") + implementation ("com.github.qingshu-ui:ayaka-spring-boot-starter:1.1.0") implementation("com.squareup.okhttp3:okhttp:4.12.0") // spring implementation("org.springframework.boot:spring-boot-starter-web") implementation ("org.springframework.boot:spring-boot-starter-aop") implementation("org.springframework.boot:spring-boot-starter-websocket") + implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-jdbc") implementation("org.xerial:sqlite-jdbc:3.45.2.0") diff --git a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/AdminCommand.kt b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/AdminCommand.kt index f12eed1..8b6af8d 100644 --- a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/AdminCommand.kt +++ b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/AdminCommand.kt @@ -7,11 +7,12 @@ import io.github.qingshu.ayaka.dto.event.message.MessageEvent import io.github.qingshu.ayaka.example.annotation.Slf4j import io.github.qingshu.ayaka.example.annotation.Slf4j.Companion.log import io.github.qingshu.ayaka.example.config.EAConfig -import io.github.qingshu.ayaka.example.service.PermissionManager import io.github.qingshu.ayaka.example.utils.Regex import io.github.qingshu.ayaka.plugin.BotPlugin import io.github.qingshu.ayaka.utils.MsgUtils +import io.github.qingshu.ayaka.utils.buildMsg import io.github.qingshu.ayaka.utils.getAtList +import io.github.qingshu.ayaka.utils.msgBuilder import meteordevelopment.orbit.EventHandler import org.springframework.stereotype.Component @@ -24,13 +25,11 @@ import org.springframework.stereotype.Component */ @Slf4j @Component -class AdminCommand( - private val permissionManager: PermissionManager, -) : BotPlugin { +class AdminCommand : BotPlugin { private val cfg get() = EAConfig.base - fun noPermissionHandler(event: MessageEvent) { + private fun noPermissionHandler(event: MessageEvent) { val bot = event.bot val userId = event.userId val msg = MsgUtils.builder().at(userId).text("你没有权限使用这个指令嘎").build() @@ -38,79 +37,66 @@ class AdminCommand( bot.sendMsg(event, msg) } - private fun registerCmd(cmd: String) { - if (permissionManager.isRegistered(cmd)) return - permissionManager.register(cmd) { - denyHandler = ::noPermissionHandler - checker = { it in cfg.adminList } - } - } - - private fun executeCommand(cmd: String, event: MessageEvent, handler: (MessageEvent) -> Unit) { - permissionManager.grantHandler(cmd, handler) - permissionManager.execute(cmd, event) - } - - init { - val cmdList = listOf( - "禁言", "退群" - ) - cmdList.forEach(::registerCmd) - } - @EventHandler @MessageHandlerFilter(cmd = "退群", at = AtEnum.NEED) fun groupQuitCmd(event: GroupMessageEvent) { - executeCommand(event.rawMessage, event) { - val msg = MsgUtils.builder().reply(event.messageId).text("收到").build() - event.bot.sendGroupMsg(event.groupId, msg) - val echo = event.bot.setGroupLeave(event.groupId, false) - log.info("Executed cmd: {}", echo) + if (event.userId !in cfg.adminList) { + noPermissionHandler(event) + return } + val msg = MsgUtils.builder().reply(event.messageId).text("收到").build() + event.bot.sendGroupMsg(event.groupId, msg) + val echo = event.bot.setGroupLeave(event.groupId, false) + log.info("Executed cmd: {}", echo) } @EventHandler @MessageHandlerFilter(cmd = Regex.BAN_CMD) fun groupBanCmd(event: GroupMessageEvent) { - val cmd = "禁言" + if (event.userId !in cfg.adminList) { + noPermissionHandler(event) + return + } val bot = event.bot val userList = getAtList(event.arrayMsg).distinct() if (userList.isEmpty()) { event.bot.sendGroupMsg( - event.groupId, MsgUtils.builder().reply(event.messageId).text("你搁那禁言空气呢").build() + event.groupId, MsgUtils.build { + reply(event.messageId) + text("你搁那禁言空气呢") + } ) return } - executeCommand(cmd, event) { - val matcher = event.matcher!! - val duration = (matcher.group(3)?.trim()?.toIntOrNull()?.coerceIn(1, 24) ?: 1).m - val banSelfMsg = MsgUtils.builder() - .reply(event.messageId) - .text("什么?你还要禁言我?你良心去哪了!!") - .build() - val finish = userList.mapNotNull { userId -> - if (userId == bot.selfId) { - bot.sendGroupMsg(event.groupId, banSelfMsg) - return@mapNotNull null - } - val echo = bot.setGroupBan(event.groupId, userId, duration) - log.info("{}", echo) - if (echo.status != "ok") return@mapNotNull null - userId - } - if (finish.isEmpty()) return@executeCommand - val msg = MsgUtils.builder() - .reply(event.messageId) - .text("完成辽\n") - .text("有以下倒霉蛋被禁言了\n\n") - finish.forEachIndexed { idx, userId -> - msg.at(userId = userId) - if (idx != finish.size - 1) msg.text("\n") + val matcher = event.matcher!! + val duration = (matcher.group(3)?.trim()?.toIntOrNull()?.coerceIn(1, 24) ?: 1).m + val banSelfMsg by buildMsg { + reply(event.messageId) + text("什么?你还要禁言我?你良心去哪了!!") + } + val finish = userList.mapNotNull { userId -> + if (userId == bot.selfId) { + bot.sendGroupMsg(event.groupId, banSelfMsg) + return@mapNotNull null } - bot.sendGroupMsg(event.groupId, msg.build()) + val echo = bot.setGroupBan(event.groupId, userId, duration) + log.info("{}", echo) + if (echo.status != "ok") return@mapNotNull null + userId + } + if (finish.isEmpty()) return + val msg by msgBuilder { + reply(event.messageId) + text("完成辽\n") + text("有以下倒霉蛋被禁言了\n\n") + } + finish.forEachIndexed { idx, userId -> + msg.at(userId = userId) + if (idx != finish.size - 1) msg.text("\n") } + bot.sendGroupMsg(event.groupId, msg.build()) } private val Int.s: Int get() = this diff --git a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/DriftBottle.kt b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/DriftBottle.kt index 19ee861..98d8365 100644 --- a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/DriftBottle.kt +++ b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/DriftBottle.kt @@ -64,11 +64,11 @@ class DriftBottle( val res = repository.save(DriftBottleEntity(0, groupId, groupName, userId, userName, content)) bot.sendGroupMsg( groupId, - MsgUtils.builder() - .at(userId) - .text("你将一个编号为 ${res.id} 写着如下内容的纸条塞入瓶中扔进大海,希望有人能够捞到~") - .text("\n\n${content}") - .build(), + MsgUtils.build { + at(userId) + text("你将一个编号为 ${res.id} 写着如下内容的纸条塞入瓶中扔进大海,希望有人能够捞到~") + text("\n\n${content}") + } ) return@with } @@ -93,22 +93,22 @@ class DriftBottle( repository.save(bottle) bot.sendGroupMsg( bottle.groupId, - MsgUtils.builder() - .at(bottle.userId) - .text("\n你编号为 ${bottle.id} 的漂流瓶被人捞起来啦~") - .text("\n\n群:${groupName}") - .text("\n用户:${userName}") - .build(), + MsgUtils.build { + at(bottle.userId) + text("\n你编号为 ${bottle.id} 的漂流瓶被人捞起来啦~") + text("\n\n群:${groupName}") + text("\n用户:${userName}") + } ) bot.sendGroupMsg( groupId, - MsgUtils.builder() - .at(userId) - .text("\n你在海边捡到了一个透明的玻璃瓶,你打开了瓶子,里面写着:\n\n") - .text(bottle.content) - .text("\n\n群:${bottle.groupName}") - .text("\n用户:${bottle.userName}") - .build(), + MsgUtils.build { + at(userId) + text("\n你在海边捡到了一个透明的玻璃瓶,你打开了瓶子,里面写着:\n\n") + text(bottle.content) + text("\n\n群:${bottle.groupName}") + text("\n用户:${bottle.userName}") + } ) return@with } @@ -117,14 +117,14 @@ class DriftBottle( val count = repository.countAllByOpenIsFalse() bot.sendGroupMsg( groupId, - MsgUtils.builder() - .at(userId) - .text("\n你缓缓走入大海,感受着海浪轻柔地拍打着你的小腿,膝盖……\n") - .text("波浪卷着你的腰腹,你感觉有些把握不住平衡了……\n") - .text("……\n") - .text("你沉入海中,【${count}】个物体与你一同沉浮。\n") - .text("不知何处涌来一股暗流,你失去了意识。") - .build(), + MsgUtils.build { + at(userId) + text("\n你缓缓走入大海,感受着海浪轻柔地拍打着你的小腿,膝盖……\n") + text("波浪卷着你的腰腹,你感觉有些把握不住平衡了……\n") + text("……\n") + text("你沉入海中,【${count}】个物体与你一同沉浮。\n") + text("不知何处涌来一股暗流,你失去了意识。") + } ) return@with } @@ -135,15 +135,15 @@ class DriftBottle( if (!queryBottle.isPresent) throw EAException("未查询到编号为 $id 的漂流瓶") bot.sendGroupMsg( groupId, - MsgUtils.builder() - .at(userId) - .text("\n${queryBottle.get().content}") - .text("\n\n状态:${if (queryBottle.get().open) "被捞起" else "未被捞起"}") - .text("\n所属用户:${queryBottle.get().userName}") - .text("\n所属群组:${queryBottle.get().groupName}") - .text("\n捞起用户:${queryBottle.get().openUserName}") - .text("\n捞起群组:${queryBottle.get().openGroupName}") - .build(), + MsgUtils.build { + at(userId) + text("\n${queryBottle.get().content}") + text("\n\n状态:${if (queryBottle.get().open) "被捞起" else "未被捞起"}") + text("\n所属用户:${queryBottle.get().userName}") + text("\n所属群组:${queryBottle.get().groupName}") + text("\n捞起用户:${queryBottle.get().openUserName}") + text("\n捞起群组:${queryBottle.get().openGroupName}") + } ) } } diff --git a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/RandomVideo.kt b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/RandomVideo.kt index f5eff88..41915e6 100644 --- a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/RandomVideo.kt +++ b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/RandomVideo.kt @@ -157,10 +157,10 @@ class RandomVideo( val randomMessage = rateLimitMessages[Random.nextInt(rateLimitMessages.size)] bot.sendMsg( - e, MsgUtils.builder() - .reply(e.messageId) - .text(randomMessage.replace("{}", "$expectedExpiration")) - .build() + e, MsgUtils.build { + reply(e.messageId) + text(randomMessage.replace("{}", "$expectedExpiration")) + } ) return } @@ -207,12 +207,12 @@ class RandomVideo( expiringGetTagMap.put(expiringId, e.userId, 2 * 60, TimeUnit.SECONDS) val allTags = service.findAllTags() - val messageBuilder = MsgUtils.builder() - .reply(e.messageId) - .text("官爷,这是咱这最好的了,请过目:\n") - .build() + val messageBuilder = MsgUtils.build { + reply(e.messageId) + text("官爷,这是咱这最好的了,请过目:\n") + } val allTagsMsg = listOf(messageBuilder) + allTags.map { - MsgUtils.builder().text("$it\n").build() + MsgUtils.build { text("$it\n") } } val forwardMsg = generateForwardMsg(123, "bot", allTagsMsg) bot.sendForwardMsg(e, forwardMsg) diff --git a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/Roulette.kt b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/Roulette.kt index edbf03b..86e35d5 100644 --- a/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/Roulette.kt +++ b/src/main/kotlin/io/github/qingshu/ayaka/example/plugin/Roulette.kt @@ -69,8 +69,8 @@ class Roulette : BotPlugin { } enum class RouletteType(val operateName: String, val introduction: String) { - NUTE("禁言", "禁言"), - KIKC("踢人", "踢出") + MUTE("禁言", "禁言"), + KICK("踢人", "踢出") } private val expiringMap: ExpiringMap = ExpiringMap.builder() @@ -83,7 +83,7 @@ class Roulette : BotPlugin { SendUtils.group(v.groupId, v.bot, v.getSummary(true)) } - private var rouletteType: RouletteType = RouletteType.NUTE + private var rouletteType: RouletteType = RouletteType.MUTE private val defaultQuotations = listOf( "看来幸运女神眷顾你,这次安全了。", @@ -99,7 +99,7 @@ class Roulette : BotPlugin { private fun changeMode(groupId: Long, userId: Long, userRole: String, bot: Bot) { if ((level[userRole] ?: 0) > 1) { - rouletteType = if (rouletteType == RouletteType.NUTE) RouletteType.KIKC else RouletteType.NUTE + rouletteType = if (rouletteType == RouletteType.MUTE) RouletteType.KICK else RouletteType.MUTE bot.sendGroupMsg(groupId, "轮盘已切换至${rouletteType.operateName}模式") return } @@ -163,7 +163,7 @@ class Roulette : BotPlugin { if (botRoleLevel > userRoleLevel) { when (rouletteType) { - RouletteType.NUTE -> { + RouletteType.MUTE -> { bot.setGroupBan( data.groupId, userId, @@ -171,7 +171,7 @@ class Roulette : BotPlugin { ) } - RouletteType.KIKC -> bot.setGroupKick(data.groupId, userId) + RouletteType.KICK -> bot.setGroupKick(data.groupId, userId) } data.progress++ data.hitCount++ diff --git a/src/main/kotlin/io/github/qingshu/ayaka/example/service/PermissionManager.kt b/src/main/kotlin/io/github/qingshu/ayaka/example/service/PermissionManager.kt deleted file mode 100644 index dbcf819..0000000 --- a/src/main/kotlin/io/github/qingshu/ayaka/example/service/PermissionManager.kt +++ /dev/null @@ -1,66 +0,0 @@ -package io.github.qingshu.ayaka.example.service - -import io.github.qingshu.ayaka.dto.event.message.MessageEvent -import org.springframework.stereotype.Service - -/** - * Copyright (c) 2024 qingshu. - * This file is part of the example-ayaka project. - * - * This project is licensed under the AGPL-3.0 License. - * See the LICENSE file for details. - */ -@Service -class PermissionManager { - - private val cmdPermission: MutableMap Boolean> = mutableMapOf() - private val denyHandler: MutableMap Unit> = mutableMapOf() - private val grantHandler: MutableMap Unit> = mutableMapOf() - - fun isRegistered(cmd: String) = cmd in cmdPermission - - fun register(cmd: String, setup: PermissionRule.() -> Unit): PermissionManager { - val rule = PermissionRule().apply(setup) - denyHandler[cmd] = rule.denyHandler - cmdPermission[cmd] = rule.checker - grantHandler[cmd] = rule.grantHandler - return this - } - - fun register(cmd: String, rule: PermissionRule) = register(cmd) { - denyHandler = rule.denyHandler - grantHandler = rule.grantHandler - checker = rule.checker - } - - fun grantHandler(cmd: String, handler: (MessageEvent) -> Unit) { - grantHandler[cmd] = handler - } - - fun denyHandler(cmd: String, handler: (MessageEvent) -> Unit) { - denyHandler[cmd] = handler - } - - fun checker(cmd: String, checker: (Long) -> Boolean) { - cmdPermission[cmd] = checker - } - - fun hasPermission(cmd: String, event: MessageEvent): Boolean { - val userId = event.userId - val hasPermission = cmdPermission[cmd]?.invoke(userId) ?: true - if (!hasPermission) { - denyHandler[cmd]?.invoke(event) - } else { - grantHandler[cmd]?.invoke(event) - } - return hasPermission - } - - fun execute(cmd: String, event: MessageEvent) = hasPermission(cmd, event) - - class PermissionRule { - var denyHandler: (MessageEvent) -> Unit = { } - var grantHandler: (MessageEvent) -> Unit = { } - var checker: (Long) -> Boolean = { true } - } -} \ No newline at end of file