diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/contact/GroupWrapper.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/contact/GroupWrapper.kt index df90014c..eaeb7808 100644 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/contact/GroupWrapper.kt +++ b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/contact/GroupWrapper.kt @@ -29,12 +29,11 @@ import top.mrxiaom.overflow.internal.contact.data.AnnouncementsWrapper import top.mrxiaom.overflow.internal.contact.data.AnnouncementsWrapper.Companion.fetchAnnouncements import top.mrxiaom.overflow.internal.contact.data.EssencesWrapper import top.mrxiaom.overflow.internal.contact.data.EssencesWrapper.Companion.fetchEssences -import top.mrxiaom.overflow.internal.listener.wrapAsMember import top.mrxiaom.overflow.internal.message.OnebotMessages import top.mrxiaom.overflow.internal.message.OnebotMessages.findForwardMessage import top.mrxiaom.overflow.internal.message.data.WrappedAudio import top.mrxiaom.overflow.internal.message.data.WrappedVideo -import top.mrxiaom.overflow.internal.utils.update +import top.mrxiaom.overflow.internal.utils.* import top.mrxiaom.overflow.spi.FileService import kotlin.coroutines.CoroutineContext 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 0c2f40a2..c18ffbcf 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 @@ -1,8 +1,6 @@ package top.mrxiaom.overflow.internal.listener import cn.evole.onebot.sdk.event.message.PrivateMessageEvent -import cn.evole.onebot.sdk.response.contact.FriendInfoResp -import cn.evole.onebot.sdk.response.contact.StrangerInfoResp import cn.evolvefield.onebot.client.handler.EventBus import cn.evolvefield.onebot.client.listener.EventListener import net.mamoe.mirai.Bot @@ -14,9 +12,8 @@ import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.OnlineMessageSource import net.mamoe.mirai.utils.MiraiInternalApi import top.mrxiaom.overflow.internal.contact.BotWrapper -import top.mrxiaom.overflow.internal.contact.FriendWrapper -import top.mrxiaom.overflow.internal.contact.StrangerWrapper import top.mrxiaom.overflow.internal.message.OnebotMessages +import top.mrxiaom.overflow.internal.utils.* fun EventBus.addFriendListeners(bot: BotWrapper) { listOf( @@ -89,23 +86,3 @@ internal class FriendMessageListener( } } } - -fun PrivateMessageEvent.PrivateSender.wrapAsFriend(bot: BotWrapper): FriendWrapper { - return bot.updateFriend(FriendWrapper(bot, FriendInfoResp().also { - it.userId = userId - it.nickname = nickname - it.remark = "" - })) -} - -fun PrivateMessageEvent.PrivateSender.wrapAsStranger(bot: BotWrapper): StrangerWrapper { - return bot.updateStranger(StrangerWrapper(bot, StrangerInfoResp( - userId, - nickname, - "", - 0, - "", - 0, - 0 - ))) -} 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 ae291bdc..4cecb2ca 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 @@ -1,27 +1,21 @@ package top.mrxiaom.overflow.internal.listener import cn.evole.onebot.sdk.event.message.GroupMessageEvent -import cn.evole.onebot.sdk.event.message.GroupMessageEvent.GroupSender import cn.evole.onebot.sdk.event.notice.group.GroupMsgDeleteNoticeEvent import cn.evole.onebot.sdk.event.notice.group.GroupNotifyNoticeEvent -import cn.evole.onebot.sdk.response.group.GroupMemberInfoResp import cn.evolvefield.onebot.client.handler.EventBus import cn.evolvefield.onebot.client.listener.EventListener import net.mamoe.mirai.Bot -import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.contact.nameCardOrNick import net.mamoe.mirai.event.broadcast import net.mamoe.mirai.event.events.MessageRecallEvent import net.mamoe.mirai.event.events.NudgeEvent import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.MiraiInternalApi -import top.mrxiaom.overflow.internal.asOnebot import top.mrxiaom.overflow.internal.contact.BotWrapper -import top.mrxiaom.overflow.internal.contact.GroupWrapper -import top.mrxiaom.overflow.internal.contact.MemberWrapper import top.mrxiaom.overflow.internal.message.OnebotMessages +import top.mrxiaom.overflow.internal.utils.* fun EventBus.addGroupListeners(bot: BotWrapper) { listOf( @@ -107,29 +101,3 @@ internal class GroupMessageRecallListener( ) } } - -fun GroupMemberInfoResp.wrapAsMember(group: Group): MemberWrapper { - return (group as GroupWrapper).updateMember(this) -} - -fun GroupSender.wrapAsMember(group: Group): MemberWrapper { - return GroupMemberInfoResp().also { - it.groupId = group.id - it.userId = userId.toLong() - it.nickname = nickname - it.card = card ?: "" - it.sex = sex ?: "" - it.age = age ?: 0 - it.area = area ?: "" - it.level = level?.toIntOrNull() ?: 0 - it.role = role ?: "member" - it.title = title ?: "" - }.wrapAsMember(group) -} - -private suspend fun BotWrapper.group(groupId: Long): Group { - return getGroup(groupId) ?: kotlin.run { - val data = impl.getGroupInfo(groupId, false).data ?: throw IllegalStateException("无法取得群信息") - updateGroup(GroupWrapper(this, data)) - } -} \ No newline at end of file diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/ByteBufUtils.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/ByteBufUtils.kt deleted file mode 100644 index 4550343c..00000000 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/ByteBufUtils.kt +++ /dev/null @@ -1,13 +0,0 @@ -package top.mrxiaom.overflow.internal.utils - -import io.netty.buffer.ByteBuf - -fun ByteBuf.readUTF(): String { - val length = this.readInt() - val bytes = this.readBytes(length) - return bytes.array().toString(Charsets.UTF_8).also { bytes.release() } -} -fun ByteBuf.writeUTF(s: String) { - writeInt(s.length) - writeBytes(s.toByteArray(Charsets.UTF_8)) -} \ No newline at end of file diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/ContactUtils.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/ContactUtils.kt deleted file mode 100644 index 0286f550..00000000 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/ContactUtils.kt +++ /dev/null @@ -1,22 +0,0 @@ -package top.mrxiaom.overflow.internal.utils - -import net.mamoe.mirai.contact.Contact -import net.mamoe.mirai.contact.ContactList -import net.mamoe.mirai.utils.MiraiInternalApi - -/** - * @param list 新的列表 - * @param updater this 是旧的,it 是新的,应当把新的内容放进旧的 - */ -@OptIn(MiraiInternalApi::class) -internal inline fun ContactList.update( - list: List, - updater: T.(T) -> Unit -) { - // 删除旧的 - delegate.removeIf { old -> list.none { old.id == it.id } } - // 更新旧的 - delegate.mapNotNull { old -> list.firstOrNull { old.id == it.id }?.to(old) }.forEach { it.second.updater(it.first) } - // 添加新的 - delegate.addAll(list.filterNot { delegate.any { old -> old.id == it.id } }) -} \ No newline at end of file diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/TransformerUtils.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/TransformerUtils.kt new file mode 100644 index 00000000..12f17921 --- /dev/null +++ b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/utils/TransformerUtils.kt @@ -0,0 +1,82 @@ +package top.mrxiaom.overflow.internal.utils + +import cn.evole.onebot.sdk.event.message.GroupMessageEvent +import cn.evole.onebot.sdk.event.message.PrivateMessageEvent +import cn.evole.onebot.sdk.response.contact.FriendInfoResp +import cn.evole.onebot.sdk.response.contact.StrangerInfoResp +import cn.evole.onebot.sdk.response.group.GroupMemberInfoResp +import net.mamoe.mirai.contact.Contact +import net.mamoe.mirai.contact.ContactList +import net.mamoe.mirai.contact.Group +import net.mamoe.mirai.utils.MiraiInternalApi +import top.mrxiaom.overflow.internal.contact.* + + +/** + * @param list 新的列表 + * @param updater this 是旧的,it 是新的,应当把新的内容放进旧的 + */ +@OptIn(MiraiInternalApi::class) +internal inline fun ContactList.update( + list: List, + updater: T.(T) -> Unit +) { + // 删除旧的 + delegate.removeIf { old -> list.none { old.id == it.id } } + // 更新旧的 + delegate.mapNotNull { old -> list.firstOrNull { old.id == it.id }?.to(old) }.forEach { it.second.updater(it.first) } + // 添加新的 + delegate.addAll(list.filterNot { delegate.any { old -> old.id == it.id } }) +} + +fun GroupMemberInfoResp.wrapAsMember(group: Group): MemberWrapper { + return (group as GroupWrapper).updateMember(this) +} + +fun GroupMessageEvent.GroupSender.wrapAsMember(group: Group): MemberWrapper { + return GroupMemberInfoResp().also { + it.groupId = group.id + it.userId = userId.toLong() + it.nickname = nickname + it.card = card ?: "" + it.sex = sex ?: "" + it.age = age ?: 0 + it.area = area ?: "" + it.level = level?.toIntOrNull() ?: 0 + it.role = role ?: "member" + it.title = title ?: "" + }.wrapAsMember(group) +} + +suspend fun BotWrapper.group(groupId: Long): Group { + return getGroup(groupId) ?: kotlin.run { + val data = impl.getGroupInfo(groupId, false).data ?: throw IllegalStateException("无法取得群信息") + updateGroup(GroupWrapper(this, data)) + } +} + + +fun PrivateMessageEvent.PrivateSender.wrapAsFriend(bot: BotWrapper): FriendWrapper { + return bot.updateFriend(FriendWrapper(bot, FriendInfoResp().also { + it.userId = userId + it.nickname = nickname + it.remark = "" + })) +} + +fun StrangerInfoResp.wrapAsStranger(bot: BotWrapper): StrangerWrapper { + return bot.updateStranger(StrangerWrapper(bot, this)) +} + +fun PrivateMessageEvent.PrivateSender.wrapAsStranger(bot: BotWrapper): StrangerWrapper { + return StrangerInfoResp( + userId, + nickname, + "", + 0, + "", + 0, + 0 + ).wrapAsStranger(bot) +} +