Skip to content

Commit

Permalink
support member mute and unmute event
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Dec 13, 2023
1 parent 7048d5a commit a382425
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
36 changes: 20 additions & 16 deletions docs/dev/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,26 @@

由于 onebot 事件有子类型,此表中 onebot 事件格式为 `事件类型 -> 子类型`

| 事件类型 | mirai | onebot | 是否支持 |
|------------------|---------------------------------|-------------------------------|------|
| 群消息 | GroupMessageEvent | message -> group -> normal ||
| 群匿名消息 | GroupMessageEvent | message -> group -> anonymous ||
| 群系统提示 | - | message -> group -> notice ||
| 好友消息 | FriendMessageEvent | message -> private -> friend ||
| 群临时会话消息 | GroupTempMessageEvent | message -> private -> group ||
| 陌生人消息 | StrangerMessageEvent | message -> private -> other ||
| 群戳一戳 | NudgeEvent | notify -> poke | ** |
| 群撤回消息 | MessageRecallEvent.GroupEvent | group_recall | ** |
| 群名片更改 | MemberCardChangeEvent | - ||
| 加群验证消息 | MemberJoinRequestEvent | request -> group -> add ||
| 被邀请加群 | BotInvitedJoinGroupRequestEvent | request -> group -> invite ||
| 加好友验证 | NewFriendRequestEvent | request -> friend ||
| 群头衔变更 | MemberSpecialTitleChangeEvent | *Not Found* | ** |
| *Coming soon...* | - | - ||
| 事件类型 | mirai | onebot | 是否支持 |
|------------------|---------------------------------|---------------------------------|------|
| 群消息 | GroupMessageEvent | message -> group -> normal ||
| 群匿名消息 | GroupMessageEvent | message -> group -> anonymous ||
| 群系统提示 | - | message -> group -> notice ||
| 好友消息 | FriendMessageEvent | message -> private -> friend ||
| 群临时会话消息 | GroupTempMessageEvent | message -> private -> group ||
| 陌生人消息 | StrangerMessageEvent | message -> private -> other ||
| 群戳一戳 | NudgeEvent | notify -> poke | ** |
| 群撤回消息 | MessageRecallEvent.GroupEvent | group_recall | ** |
| 群名片更改 | MemberCardChangeEvent | - ||
| 加群验证消息 | MemberJoinRequestEvent | request -> group -> add ||
| 被邀请加群 | BotInvitedJoinGroupRequestEvent | request -> group -> invite ||
| 加好友验证 | NewFriendRequestEvent | request -> friend ||
| 群头衔变更 | MemberSpecialTitleChangeEvent | *Not Found* | ** |
| 群员被禁言 | MemberMuteEvent | notice -> group_ban -> ban ||
| 机器人被禁言 | BotMuteEvent | notice -> group_ban -> ban ||
| 群员被解除禁言 | MemberUnmuteEvent | notice -> group_ban -> lift_ban ||
| 机器人被解除禁言 | BotUnmuteEvent | notice -> group_ban -> lift_ban ||
| *Coming soon...* | - | - ||

实现事件说明如下
* 群头衔变更事件 onebot-sdk 里有但是没用到,先实现再说。
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class GroupWrapper(
}

internal suspend fun queryMember(userId: Long): MemberWrapper? {
if (userId == bot.id) return botAsMember
return (members[userId] as? MemberWrapper) ?: botWrapper.impl
.getGroupMemberInfo(id, userId, false).data?.wrapAsMember(this)
}
Expand Down Expand Up @@ -120,11 +121,11 @@ class GroupWrapper(
override var name: String
get() = impl.groupName
set(value) { impl.groupName = value }
override val members: ContactList<NormalMember>
override val members: ContactList<MemberWrapper>
get() = membersInternal ?: runBlocking {
updateGroupMemberList()
}
override val botAsMember: NormalMember
override val botAsMember: MemberWrapper
get() = members.firstOrNull { it.id == bot.id } ?: runBlocking {
val data = botWrapper.impl.getGroupMemberInfo(id, bot.id, false).data
MemberWrapper(botWrapper, this@GroupWrapper, data ?: GroupMemberInfoResp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package top.mrxiaom.overflow.internal.listener

import cn.evole.onebot.sdk.event.message.GroupMessageEvent
import cn.evole.onebot.sdk.event.notice.group.GroupBanNoticeEvent
import cn.evole.onebot.sdk.event.notice.group.GroupMsgDeleteNoticeEvent
import cn.evole.onebot.sdk.event.notice.group.GroupNotifyNoticeEvent
import cn.evole.onebot.sdk.event.notice.group.GroupTitleChangeNoticeEvent
Expand All @@ -27,6 +28,7 @@ fun EventBus.addGroupListeners(bot: BotWrapper) {
GroupMessageRecallListener(bot),
GroupAddRequestListener(bot),
GroupTitleChangeNoticeListener(bot),
GroupBanNoticeListener(bot),

).forEach(::addListener)
}
Expand Down Expand Up @@ -149,3 +151,45 @@ internal class GroupTitleChangeNoticeListener(
)
}
}

internal class GroupBanNoticeListener(
val bot: BotWrapper
): EventListener<GroupBanNoticeEvent> {
override suspend fun onMessage(e: GroupBanNoticeEvent) {
val mute = when(e.subType) {
"ban" -> true
"lift_ban" -> false
else -> return
}
val group = bot.group(e.groupId)
if (e.userId == bot.id) {
val operator = group.queryMember(e.operatorId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}")
if (mute) {
BotMuteEvent(
durationSeconds = e.duration.toInt(),
operator = operator
).broadcast()
} else {
BotUnmuteEvent(
operator = operator
).broadcast()
}
} else {
val operator = group.queryMember(e.operatorId)
val member = group.queryMember(e.userId) ?: throw IllegalStateException("无法找到群 ${e.groupId} 的成员 ${e.userId}")
if (mute) {
MemberMuteEvent(
member = member,
durationSeconds = e.duration.toInt(),
operator = operator
).broadcast()
} else {
MemberUnmuteEvent(
member = member,
operator = operator
).broadcast()
}
}
}

}

0 comments on commit a382425

Please sign in to comment.