Skip to content

Commit

Permalink
redundant clean
Browse files Browse the repository at this point in the history
  • Loading branch information
qingshu-ui committed Oct 8, 2024
1 parent 2384530 commit 55bb56a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 175 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
102 changes: 44 additions & 58 deletions src/main/kotlin/io/github/qingshu/ayaka/example/plugin/AdminCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,93 +25,78 @@ 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()
log.info(msg)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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}")
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class Roulette : BotPlugin {
}

enum class RouletteType(val operateName: String, val introduction: String) {
NUTE("禁言", "禁言"),
KIKC("踢人", "踢出")
MUTE("禁言", "禁言"),
KICK("踢人", "踢出")
}

private val expiringMap: ExpiringMap<Long, GroupRouletteData> = ExpiringMap.builder()
Expand All @@ -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(
"看来幸运女神眷顾你,这次安全了。",
Expand All @@ -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
}
Expand Down Expand Up @@ -163,15 +163,15 @@ class Roulette : BotPlugin {

if (botRoleLevel > userRoleLevel) {
when (rouletteType) {
RouletteType.NUTE -> {
RouletteType.MUTE -> {
bot.setGroupBan(
data.groupId,
userId,
(1..EAConfig.plugins.roulette.maxMuteTime).random() * 60
)
}

RouletteType.KIKC -> bot.setGroupKick(data.groupId, userId)
RouletteType.KICK -> bot.setGroupKick(data.groupId, userId)
}
data.progress++
data.hitCount++
Expand Down
Loading

0 comments on commit 55bb56a

Please sign in to comment.