From 92fc569127d8ec6476c24203cef46a1b69e02074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=BA=E9=96=93=E5=B7=A5=E4=BD=9C?= Date: Wed, 18 Sep 2024 13:17:08 +0800 Subject: [PATCH] add id check and warning #98 #100 --- .../main/kotlin/client/connection/IAdapter.kt | 6 +- .../mrxiaom/overflow/internal/listener/bot.kt | 12 +++ .../overflow/internal/listener/friend.kt | 9 ++- .../overflow/internal/listener/group.kt | 73 +++++++++++++++++-- 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/onebot/src/main/kotlin/client/connection/IAdapter.kt b/onebot/src/main/kotlin/client/connection/IAdapter.kt index 5cfb745..9c09252 100644 --- a/onebot/src/main/kotlin/client/connection/IAdapter.kt +++ b/onebot/src/main/kotlin/client/connection/IAdapter.kt @@ -28,7 +28,11 @@ interface IAdapter { } else scope.launch { // 处理事件 mutex.withLock { withTimeoutOrNull(processTimeout) { - EventBus.onReceive(message) + runCatching { + EventBus.onReceive(message) + }.onFailure { + logger.error("处理 Onebot 事件时出现异常: ", it) + } } ?: throw IllegalStateException("事件处理超时: $message") } } diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/bot.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/bot.kt index 18aa18b..9218da6 100644 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/bot.kt +++ b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/bot.kt @@ -11,6 +11,7 @@ import net.mamoe.mirai.event.events.NudgeEvent import net.mamoe.mirai.utils.MiraiInternalApi import top.mrxiaom.overflow.Overflow import top.mrxiaom.overflow.event.UnsolvedOnebotEvent +import top.mrxiaom.overflow.internal.contact.BotWrapper import top.mrxiaom.overflow.internal.scope import top.mrxiaom.overflow.internal.utils.bot import top.mrxiaom.overflow.internal.utils.group @@ -30,6 +31,9 @@ internal class NotifyNoticeListener : EventListener { when (e.subType) { "poke" -> { val operatorId = e.realOperatorId + if (bot.checkId(operatorId) { + "%onebot 返回了异常的数值 operator_id=%value" + }) return // Lagrange: notice -> notify -> poke 不仅仅适用于群聊 if (e.groupId == 0L) { val operator = bot.getFriend(operatorId) @@ -59,3 +63,11 @@ internal class UnsolvedEventListener : EventListener { } } } + +internal fun BotWrapper.checkId(id: Long, msg: () -> String): Boolean { + return (id <= 0).also { + logger.warning(msg() + .replace("%onebot", "${impl.appName} v${impl.appVersion}") + .replace("%value", id.toString())) + } +} diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/friend.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/friend.kt index f33041c..b1a7cba 100644 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/friend.kt +++ b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/friend.kt @@ -58,6 +58,9 @@ internal class FriendMessageListener : EventListener { && !listOf(/*Group:*/0, /*Discussion:*/7).contains(e.tempSource)) return if (e.groupId <= 0) return val group = bot.group(e.groupId) + if (bot.checkId(e.userId) { + "%onebot 返回了异常的 user_id=%value" + }) return val member = group.queryMember(e.userId) if (member == null || member.id == bot.id) { @@ -131,7 +134,11 @@ internal class FriendMessageRecallListener : EventListener 0 } ?: e.userId - val friend = bot.getFriend(e.userId) ?: throw IllegalStateException("无法找到好友 ${e.userId}") + val friend = e.userId.takeIf { it > 0 }?.run { bot.getFriend(this) } + if (friend == null) { + bot.logger.warning("无法找到好友 ${e.userId}") + return + } bot.eventDispatcher.broadcastAsync( MessageRecallEvent.FriendRecall(bot, intArrayOf(e.msgId.toInt()), diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/group.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/group.kt index a0a91b0..2484a8e 100644 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/group.kt +++ b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/listener/group.kt @@ -39,6 +39,9 @@ internal fun addGroupListeners() { internal class GroupMessageListener : EventListener { override suspend fun onMessage(e: GroupMessageEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + }) return when(e.subType) { "normal" -> { val group = bot.group(e.groupId) @@ -103,6 +106,13 @@ internal class GroupMessageListener : EventListener { internal class GroupMessageRecallListener : EventListener { override suspend fun onMessage(e: GroupMsgDeleteNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.operatorId) { + "%onebot 返回了异常的数值 operator_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val operator = group.queryMember(e.operatorId) val target = group.queryMember(e.userId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}") @@ -120,6 +130,9 @@ internal class GroupMessageRecallListener : EventListener { override suspend fun onMessage(e: GroupAddRequestEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + }) return when (e.subType) { "add" -> { // 某人申请入群 bot.eventDispatcher.broadcastAsync(MemberJoinRequestEvent( @@ -150,6 +163,11 @@ internal class GroupAddRequestListener : EventListener { internal class GroupDecreaseNoticeListener : EventListener { override suspend fun onMessage(e: GroupDecreaseNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val member = group.queryMember(e.userId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}") when (e.subType) { @@ -157,6 +175,9 @@ internal class GroupDecreaseNoticeListener : EventListener { // 成员被踢 + if (bot.checkId(e.operatorId) { + "%onebot 返回了异常的数值 operator_id=%value" + }) return bot.eventDispatcher.broadcastAsync(MemberLeaveEvent.Kick( member = member, operator = group.queryMember(e.operatorId) @@ -172,6 +193,11 @@ internal class GroupDecreaseNoticeListener : EventListener { override suspend fun onMessage(e: GroupIncreaseNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val member = group.queryMember(e.userId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}") when (e.subType) { @@ -191,6 +217,11 @@ internal class GroupIncreaseNoticeListener : EventListener { override suspend fun onMessage(e: GroupTitleChangeNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val member = group.queryMember(e.userId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}") MemberSpecialTitleChangeEvent( @@ -210,14 +241,17 @@ internal class GroupBanNoticeListener : EventListener { "lift_ban" -> false else -> return } + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + }) return val group = bot.group(e.groupId) - val operator = group.queryMember(e.operatorId) + val operator = e.operatorId.takeIf { it > 0 }?.run { group.queryMember(this) } if (e.userId == 0L && e.duration == -1L) { - if (operator == null && e.operatorId != bot.id) { - throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.operatorId}") - } val origin = group.settings.isMuteAll group.settings.muteAll = mute + if (operator == null && e.operatorId != bot.id) { + return + } bot.eventDispatcher.broadcastAsync(GroupMuteAllEvent( origin = origin, new = mute, @@ -227,7 +261,10 @@ internal class GroupBanNoticeListener : EventListener { return } if (e.userId == bot.id) { - if (operator == null) throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.operatorId}") + if (operator == null) { + bot.logger.warning("无法找到群 ${e.groupId} 的成员 ${e.operatorId}") + return + } if (mute) { bot.eventDispatcher.broadcastAsync(BotMuteEvent( durationSeconds = e.duration.toInt(), @@ -240,9 +277,14 @@ internal class GroupBanNoticeListener : EventListener { } } else { if (operator == null && e.operatorId != bot.id) { - throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.operatorId}") + bot.logger.warning("无法找到群 ${e.groupId} 的成员(操作者) ${e.operatorId}") + return + } + val member = group.queryMember(e.userId) + if (member == null) { + bot.logger.warning("无法找到群 ${e.groupId} 的成员 ${e.userId}") + return } - val member = group.queryMember(e.userId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}") if (mute) { bot.eventDispatcher.broadcastAsync(MemberMuteEvent( member = member, @@ -261,6 +303,11 @@ internal class GroupBanNoticeListener : EventListener { internal class GroupAdminNoticeListener : EventListener { override suspend fun onMessage(e: GroupAdminNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val member = group.queryMember(e.userId) ?: return val origin = member.permission @@ -284,6 +331,13 @@ internal class GroupAdminNoticeListener : EventListener { internal class GroupEssenceNoticeListener : EventListener { override suspend fun onMessage(e: GroupEssenceNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.operatorId) { + "%onebot 返回了异常的数值 operator_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val sender = group.queryMember(e.senderId) ?: return val operator = group.queryMember(e.operatorId) ?: return @@ -303,6 +357,11 @@ internal class GroupEssenceNoticeListener : EventListener { override suspend fun onMessage(e: GroupCardChangeNoticeEvent) { val bot = e.bot ?: return + if (bot.checkId(e.groupId) { + "%onebot 返回了异常的数值 group_id=%value" + } || bot.checkId(e.userId) { + "%onebot 返回了异常的数值 user_id=%value" + }) return val group = bot.group(e.groupId) val member = group.queryMember(e.userId) ?: return bot.eventDispatcher.broadcastAsync(MemberCardChangeEvent(