Prefer using group code

This commit is contained in:
Him188 2021-09-27 16:18:57 +01:00
parent 8694c16f05
commit cb3d4362ba
3 changed files with 10 additions and 8 deletions

View File

@ -47,9 +47,10 @@ internal interface NewContactSupport { // can be a marker interface when context
return getNewGroup(code)?.apply { groups.delegate.add(this) }
}
// final
suspend fun QQAndroidBot.addNewGroupByUin(groupUin: Long): GroupImpl? {
if (getGroupByUin(groupUin) != null) return null
return getNewGroup(Mirai.calculateGroupCodeByGroupUin(groupUin))?.apply { groups.delegate.add(this) }
return addNewGroupByCode(Mirai.calculateGroupCodeByGroupUin(groupUin))
}
suspend fun QQAndroidBot.addNewGroup(stTroopNum: StTroopNum): GroupImpl? {

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.contact.MemberPermission.*
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.internal.contact.addNewNormalMember
import net.mamoe.mirai.internal.contact.info.MemberInfoImpl
import net.mamoe.mirai.internal.getGroupByCodeOrUin
import net.mamoe.mirai.internal.getGroupByUin
import net.mamoe.mirai.internal.getGroupByUinOrCode
import net.mamoe.mirai.internal.message.contextualBugReportException
@ -136,14 +137,14 @@ internal class GroupOrMemberListNoticeProcessor(
// 33
private suspend fun NoticePipelineContext.processGroupJoin33(data: MsgComm.Msg) = data.context {
msgBody.msgContent.read {
val groupUin = Mirai.calculateGroupUinByGroupCode(readUInt().toLong())
val groupCode = readUInt().toLong()
if (remaining < 5) return
discardExact(1)
val joinedMemberUin = readUInt().toLong()
if (joinedMemberUin == bot.id && bot.getGroupByUin(groupUin) != null) return // duplicate
if (joinedMemberUin == bot.id && bot.getGroupByCodeOrUin(groupCode) != null) return // duplicate
val group = bot.getGroupByUin(groupUin) ?: bot.addNewGroupByUin(groupUin) ?: return
val group = bot.getGroupByCodeOrUin(groupCode) ?: bot.addNewGroupByCode(groupCode) ?: return
val joinType = readByte().toInt()
val invitorUin = readUInt().toLong()
when (joinType) {

View File

@ -106,8 +106,8 @@ internal class BotInvitedJoinTest : AbstractNoticeProcessorTest() {
suspend fun runTest() = use(
createContext = { bot, attributes ->
object : NoticeProcessorPipelineImpl.ContextImpl(bot, attributes) {
override suspend fun QQAndroidBot.addNewGroupByUin(groupUin: Long): GroupImpl {
assertEquals(204230203, groupUin) // uin of 2230203
override suspend fun QQAndroidBot.addNewGroupByCode(code: Long): GroupImpl {
assertEquals(2230203, code)
return bot.addGroup(2230203, 1230001, name = "testtest").apply {
addMember(1230003, permission = MemberPermission.MEMBER)
addMember(1230001, permission = MemberPermission.OWNER)
@ -156,8 +156,8 @@ internal class BotInvitedJoinTest : AbstractNoticeProcessorTest() {
suspend fun runTest() =
use(createContext = { bot, attributes ->
object : NoticeProcessorPipelineImpl.ContextImpl(bot, attributes) {
override suspend fun QQAndroidBot.addNewGroupByUin(groupUin: Long): GroupImpl {
assertEquals(204230203, groupUin) // uin of 2230203
override suspend fun QQAndroidBot.addNewGroupByCode(code: Long): GroupImpl {
assertEquals(2230203, code)
return bot.addGroup(2230203, 1230002, name = "testtest").apply {
addMember(1230001, permission = MemberPermission.MEMBER)
}