Skip to content

Commit

Permalink
add id check and warning
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Sep 18, 2024
1 parent d6669df commit 92fc569
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
6 changes: 5 additions & 1 deletion onebot/src/main/kotlin/client/connection/IAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,9 @@ internal class NotifyNoticeListener : EventListener<NotifyNoticeEvent> {
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)
Expand Down Expand Up @@ -59,3 +63,11 @@ internal class UnsolvedEventListener : EventListener<UnsolvedEvent> {
}
}
}

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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ internal class FriendMessageListener : EventListener<PrivateMessageEvent> {
&& !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) {
Expand Down Expand Up @@ -131,7 +134,11 @@ internal class FriendMessageRecallListener : EventListener<PrivateMsgDeleteNotic
override suspend fun onMessage(e: PrivateMsgDeleteNoticeEvent) {
val bot = e.bot ?: return
val operatorId = e.operatorId.takeIf { it > 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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ internal fun addGroupListeners() {
internal class GroupMessageListener : EventListener<GroupMessageEvent> {
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)
Expand Down Expand Up @@ -103,6 +106,13 @@ internal class GroupMessageListener : EventListener<GroupMessageEvent> {
internal class GroupMessageRecallListener : EventListener<GroupMsgDeleteNoticeEvent> {
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}")
Expand All @@ -120,6 +130,9 @@ internal class GroupMessageRecallListener : EventListener<GroupMsgDeleteNoticeEv
internal class GroupAddRequestListener : EventListener<GroupAddRequestEvent> {
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(
Expand Down Expand Up @@ -150,13 +163,21 @@ internal class GroupAddRequestListener : EventListener<GroupAddRequestEvent> {
internal class GroupDecreaseNoticeListener : EventListener<GroupDecreaseNoticeEvent> {
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) {
"leave" -> { // 主动退群
bot.eventDispatcher.broadcastAsync(MemberLeaveEvent.Quit(member))
}
"kick" -> { // 成员被踢
if (bot.checkId(e.operatorId) {
"%onebot 返回了异常的数值 operator_id=%value"
}) return
bot.eventDispatcher.broadcastAsync(MemberLeaveEvent.Kick(
member = member,
operator = group.queryMember(e.operatorId)
Expand All @@ -172,6 +193,11 @@ internal class GroupDecreaseNoticeListener : EventListener<GroupDecreaseNoticeEv
internal class GroupIncreaseNoticeListener : EventListener<GroupIncreaseNoticeEvent> {
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) {
Expand All @@ -191,6 +217,11 @@ internal class GroupIncreaseNoticeListener : EventListener<GroupIncreaseNoticeEv
internal class GroupTitleChangeNoticeListener : EventListener<GroupTitleChangeNoticeEvent> {
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(
Expand All @@ -210,14 +241,17 @@ internal class GroupBanNoticeListener : EventListener<GroupBanNoticeEvent> {
"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,
Expand All @@ -227,7 +261,10 @@ internal class GroupBanNoticeListener : EventListener<GroupBanNoticeEvent> {
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(),
Expand All @@ -240,9 +277,14 @@ internal class GroupBanNoticeListener : EventListener<GroupBanNoticeEvent> {
}
} 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,
Expand All @@ -261,6 +303,11 @@ internal class GroupBanNoticeListener : EventListener<GroupBanNoticeEvent> {
internal class GroupAdminNoticeListener : EventListener<GroupAdminNoticeEvent> {
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
Expand All @@ -284,6 +331,13 @@ internal class GroupAdminNoticeListener : EventListener<GroupAdminNoticeEvent> {
internal class GroupEssenceNoticeListener : EventListener<GroupEssenceNoticeEvent> {
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
Expand All @@ -303,6 +357,11 @@ internal class GroupEssenceNoticeListener : EventListener<GroupEssenceNoticeEven
internal class GroupCardChangeNoticeListener : EventListener<GroupCardChangeNoticeEvent> {
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(
Expand Down

0 comments on commit 92fc569

Please sign in to comment.