Skip to content

Commit

Permalink
支持从 GroupRelation 中直接获取 member 信息
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jun 9, 2024
1 parent 96cf616 commit 8450e34
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@ import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
*/
@InternalOneBotAPI
public object OneBot

// TODO 一些Opt注解放在这个模块?
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package love.forte.simbot.component.onebot.v11.core.bot

import io.ktor.client.HttpClient
import io.ktor.http.Url
import io.ktor.client.*
import io.ktor.http.*
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.firstOrNull
import love.forte.simbot.bot.Bot
Expand All @@ -30,6 +30,7 @@ import love.forte.simbot.common.id.ID
import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
import love.forte.simbot.component.onebot.v11.core.actor.OneBotFriend
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.api.GetLoginInfoApi
import love.forte.simbot.component.onebot.v11.core.api.GetLoginInfoResult
import love.forte.simbot.suspendrunner.ST
Expand Down Expand Up @@ -227,4 +228,16 @@ public interface OneBotBotGroupRelation : GroupRelation {
@JvmSynthetic
override suspend fun groupCount(): Int =
groups.asFlow().count()

/**
* 根据ID查询某个群中的某个成员。
*/
@ST(
blockingBaseName = "getMember",
blockingSuffix = "",
asyncBaseName = "getMember",
reserveBaseName = "getMember"
)
public suspend fun member(groupId: ID, memberId: ID): OneBotMember?

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.component.onebot.v11.core.OneBot11
import love.forte.simbot.component.onebot.v11.core.actor.OneBotFriend
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.actor.internal.toFriend
import love.forte.simbot.component.onebot.v11.core.actor.internal.toGroup
import love.forte.simbot.component.onebot.v11.core.api.GetFriendListApi
import love.forte.simbot.component.onebot.v11.core.api.GetGroupInfoApi
import love.forte.simbot.component.onebot.v11.core.api.GetGroupListApi
import love.forte.simbot.component.onebot.v11.core.api.GetLoginInfoApi
import love.forte.simbot.component.onebot.v11.core.api.GetLoginInfoResult
import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember
import love.forte.simbot.component.onebot.v11.core.api.*
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotConfiguration
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotFriendRelation
Expand Down Expand Up @@ -576,6 +574,12 @@ internal class OneBotBotImpl(
// TODO owner?
)
}

override suspend fun member(groupId: ID, memberId: ID): OneBotMember {
// TODO 如何检测不存在?
return GetGroupMemberInfoApi.create(groupId, userId)
.requestDataBy(this@OneBotBotImpl).toMember(this@OneBotBotImpl)
}
}

override fun toString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ internal class OneBotGroupPrivateMessageEventImpl(
override suspend fun source(): OneBotGroup {
// TODO 额,怎么知道群号?
// 无法得知群号
throw UnsupportedOperationException("Don't know how to get the group number from PrivateMessageEvent")
throw UnsupportedOperationException("The way to get the group number from PrivateMessageEvent is unknown.")
}

override fun toString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,34 @@ import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember
import love.forte.simbot.component.onebot.v11.core.api.GetGroupMemberInfoApi
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl
import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot
import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotGroupAdminEvent
import love.forte.simbot.component.onebot.v11.event.notice.GroupAdminEvent

internal class OneBotGroupAdminEventImpl(
override val sourceEventRaw: String?,
override val sourceEvent: GroupAdminEvent,
override val bot: OneBotBotImpl
override val bot: OneBotBot
) : OneBotGroupAdminEvent {
override val id: ID
get() = with(sourceEvent) {
"$postType-$noticeType-$subType-$groupId-$userId-$time"
}.ID

override suspend fun content(): OneBotMember {
// TODO 换成 groupRelation
return GetGroupMemberInfoApi.create(
return bot.groupRelation.member(
groupId = sourceEvent.groupId,
userId = sourceEvent.userId
).requestDataBy(bot).toMember(bot)
memberId = sourceEvent.userId
) ?: error(
"Member with id ${sourceEvent.userId} " +
"in Group ${sourceEvent.groupId} is not found"
)
}

override suspend fun source(): OneBotGroup {
return bot.groupRelation.group(sourceEvent.groupId)
?: error("Group with id ${sourceEvent.groupId} not found")
?: error("Group with id ${sourceEvent.groupId} is not found")
}

override fun toString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember
import love.forte.simbot.component.onebot.v11.core.api.GetGroupMemberInfoApi
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl
import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot
import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotGroupBanEvent
import love.forte.simbot.component.onebot.v11.event.notice.GroupBanEvent
Expand All @@ -37,24 +34,26 @@ import love.forte.simbot.component.onebot.v11.event.notice.GroupBanEvent
internal class OneBotGroupBanEventImpl(
override val sourceEventRaw: String?,
override val sourceEvent: GroupBanEvent,
override val bot: OneBotBotImpl
override val bot: OneBotBot
) : OneBotGroupBanEvent {
override val id: ID
get() = with(sourceEvent) {
"$postType-$noticeType-$subType-$groupId-$userId-$time"
}.ID

override suspend fun content(): OneBotMember {
// TODO 换成 groupRelation
return GetGroupMemberInfoApi.create(
return bot.groupRelation.member(
groupId = sourceEvent.groupId,
userId = sourceEvent.userId
).requestDataBy(bot).toMember(bot)
memberId = sourceEvent.userId
) ?: error(
"Member with id ${sourceEvent.userId} " +
"in Group ${sourceEvent.groupId} is not found"
)
}

override suspend fun source(): OneBotGroup {
return bot.groupRelation.group(sourceEvent.groupId)
?: error("Group with id ${sourceEvent.groupId} not found")
?: error("Group with id ${sourceEvent.groupId} is not found")
}

override fun toString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember
import love.forte.simbot.component.onebot.v11.core.api.GetGroupMemberInfoApi
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl
import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy
import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotGroupChangeEvent
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotGroupMemberDecreaseEvent
Expand All @@ -45,26 +43,29 @@ internal abstract class OneBotGroupChangeEventImpl : OneBotGroupChangeEvent {

override suspend fun content(): OneBotGroup {
return bot.groupRelation.group(groupId)
?: error("Group with id $groupId not found")
?: error("Group with id $groupId is not found")
}
}

internal class OneBotGroupMemberIncreaseEventImpl(
override val sourceEventRaw: String?,
override val sourceEvent: GroupIncreaseEvent,
override val bot: OneBotBotImpl
override val bot: OneBotBot
) : OneBotGroupChangeEventImpl(), OneBotGroupMemberIncreaseEvent {
override suspend fun member(): OneBotMember {
// TODO 换成 groupRelation
return GetGroupMemberInfoApi.create(
return bot.groupRelation.member(
groupId = sourceEvent.groupId,
userId = sourceEvent.userId
).requestDataBy(bot).toMember(bot)
memberId = sourceEvent.userId
) ?: error(
"Member with id ${sourceEvent.userId} " +
"in Group ${sourceEvent.groupId} is not found"
)
}

override fun toString(): String =
eventToString("OneBotGroupMemberIncreaseEvent")
}

internal class OneBotGroupMemberDecreaseEventImpl(
override val sourceEventRaw: String?,
override val sourceEvent: GroupDecreaseEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember
import love.forte.simbot.component.onebot.v11.core.api.GetGroupMemberInfoApi
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl
import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy
import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotBotSelfPokeEvent
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotHonorEvent
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotLuckyKingEvent
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotMemberPokeEvent
import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotNotifyEvent
import love.forte.simbot.component.onebot.v11.core.event.notice.*
import love.forte.simbot.component.onebot.v11.event.notice.NotifyEvent


Expand All @@ -50,15 +43,17 @@ internal abstract class OneBotNotifyEventImpl(

override suspend fun source(): OneBotGroup {
return bot.groupRelation.group(groupId)
?: error("Group with id $groupId not found")
?: error("Group with id $groupId is not found")
}

override suspend fun content(): OneBotMember {
// TODO 换成 groupRelation
return GetGroupMemberInfoApi.create(
return bot.groupRelation.member(
groupId = sourceEvent.groupId,
userId = sourceEvent.userId
).requestDataBy(bot).toMember(bot)
memberId = sourceEvent.userId
) ?: error(
"Member with id ${sourceEvent.userId} " +
"in Group ${sourceEvent.groupId} is not found"
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
exports love.forte.simbot.component.onebot.v11.core.event.meta;
exports love.forte.simbot.component.onebot.v11.core.event.notice;
exports love.forte.simbot.component.onebot.v11.core.event.request;
exports love.forte.simbot.component.onebot.v11.core.event.stage;
exports love.forte.simbot.component.onebot.v11.core.utils;

provides ComponentFactoryProvider with OneBot11ComponentFactoryProvider;
Expand Down

0 comments on commit 8450e34

Please sign in to comment.