Skip to content

Commit

Permalink
move onebot & mirai converter to TransformerUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Dec 4, 2023
1 parent 1c01fde commit ccaadf8
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
)))
}
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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))
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 <reified T : Contact> ContactList<T>.update(
list: List<T>,
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)
}

0 comments on commit ccaadf8

Please sign in to comment.