Skip to content

Commit

Permalink
Merge pull request #17 from simple-robot/dev/support-delete
Browse files Browse the repository at this point in the history
`OneBotGroup` 和 `OneBotMember` 实现 `DeleteSupport`
  • Loading branch information
ForteScarlet authored Jun 11, 2024
2 parents cf7dd18 + 5def48b commit bd57203
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

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

import love.forte.simbot.ability.DeleteOption
import love.forte.simbot.ability.DeleteSupport
import love.forte.simbot.ability.StandardDeleteOption
import love.forte.simbot.common.collectable.Collectable
import love.forte.simbot.common.collectable.asCollectable
import love.forte.simbot.common.id.ID
import love.forte.simbot.component.onebot.v11.core.api.SetGroupLeaveApi
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot
import love.forte.simbot.component.onebot.v11.message.OneBotMessageReceipt
import love.forte.simbot.definition.ChatGroup
Expand All @@ -28,6 +32,9 @@ import love.forte.simbot.message.MessageContent
import love.forte.simbot.suspendrunner.ST
import love.forte.simbot.suspendrunner.STP
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic


/**
Expand All @@ -38,9 +45,15 @@ import kotlin.coroutines.CoroutineContext
* - 来自 Bot relation API
* ([OneBotBot.groupRelation][love.forte.simbot.component.onebot.v11.core.bot.OneBotBot.groupRelation])
*
* ## DeleteSupport
*
* [OneBotGroup] 实现 [DeleteSupport],

Check warning on line 50 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'DeleteSupport'
* [delete] 代表使Bot退出此群或解散群聊。
*
*
* @author ForteScarlet
*/
public interface OneBotGroup : ChatGroup {
public interface OneBotGroup : ChatGroup, DeleteSupport {
/**
* 协程上下文。源自 [OneBotBot], 但是不含 [Job][kotlinx.coroutines.Job]。

Check warning on line 58 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Job'

Check warning on line 58 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'kotlinx'

Check warning on line 58 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'coroutines'
*/
Expand Down Expand Up @@ -82,14 +95,70 @@ public interface OneBotGroup : ChatGroup {
@STP
override suspend fun botAsMember(): OneBotMember


/**
* 向此群内发送消息。
*
* @throws Throwable 任何可能在请求API时产生的异常
*/
@ST
override suspend fun send(message: Message): OneBotMessageReceipt

/**
* 向此群内发送消息。
*
* @throws Throwable 任何可能在请求API时产生的异常
*/
@ST
override suspend fun send(messageContent: MessageContent): OneBotMessageReceipt

/**
* 向此群内发送消息。
*
* @throws Throwable 任何可能在请求API时产生的异常
*/
@ST
override suspend fun send(text: String): OneBotMessageReceipt

/**
* 让当前bot退出/离开此群。
*
* @param options 进行删除行为时的额外属性。
* 支持:
* - [StandardDeleteOption.IGNORE_ON_FAILURE]

Check warning on line 127 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'StandardDeleteOption'

Check warning on line 127 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'IGNORE_ON_FAILURE'
* - [OneBotGroupDeleteOption] 的所有子实现
*
* @throws Throwable 任何可能在请求API时产生的异常
*/
@JvmSynthetic
override suspend fun delete(vararg options: DeleteOption)
}

/**
* 在 [OneBotGroup.delete] 中可选择使用的额外属性。
*/
public sealed class OneBotGroupDeleteOption : DeleteOption {
/**
* 是否为解散群。如果bot为群主,
* 则需要提供此参数来使 [OneBotGroup.delete] 解散群,
* 否则无法解散或退出。
*
* 更多参考:[SetGroupLeaveApi]
*
* @see SetGroupLeaveApi
*/
public data object Dismiss : OneBotGroupDeleteOption()

public companion object {
/**
* 是否为解散群。
* 更多参考见 [Dismiss]。
*
* @see Dismiss
*/
@get:JvmStatic
@get:JvmName("dismiss")
public val dismiss: Dismiss
get() = Dismiss

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@

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

import love.forte.simbot.ability.DeleteOption
import love.forte.simbot.ability.DeleteSupport
import love.forte.simbot.ability.StandardDeleteOption
import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.literal
import love.forte.simbot.component.onebot.v11.common.utils.qqAvatar640
import love.forte.simbot.component.onebot.v11.core.api.SetGroupKickApi
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot
import love.forte.simbot.component.onebot.v11.message.OneBotMessageReceipt
import love.forte.simbot.definition.Member
import love.forte.simbot.message.Message
import love.forte.simbot.message.MessageContent
import love.forte.simbot.suspendrunner.ST
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic


/**
Expand All @@ -37,9 +44,14 @@ import kotlin.coroutines.CoroutineContext
* - 来自 Group API
* ([OneBotGroup.members])
*
* ## DeleteSupport
*
* [OneBotMember] 实现 [DeleteSupport],

Check warning on line 49 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotMember.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'DeleteSupport'
* [delete] 代表试着踢出这个群成员。
*
* @author ForteScarlet
*/
public interface OneBotMember : Member {
public interface OneBotMember : Member, DeleteSupport {
/**
* 协程上下文。源自 [OneBotBot], 但是不含 [Job][kotlinx.coroutines.Job]。

Check warning on line 56 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotMember.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'coroutines'

Check warning on line 56 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotMember.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'kotlinx'

Check warning on line 56 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotMember.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Job'
*/
Expand All @@ -62,13 +74,73 @@ public interface OneBotMember : Member {
override val avatar: String
get() = qqAvatar640(id.literal)

/**
* 向此成员发送消息。
*
* @throws Throwable 任何可能在请求API时得到的异常
*/
@ST
override suspend fun send(text: String): OneBotMessageReceipt

/**
* 向此成员发送消息。
*
* @throws Throwable 任何可能在请求API时得到的异常
*/
@ST
override suspend fun send(message: Message): OneBotMessageReceipt

/**
* 向此成员发送消息。
*
* @throws Throwable 任何可能在请求API时得到的异常
*/
@ST
override suspend fun send(messageContent: MessageContent): OneBotMessageReceipt

/**
* 尝试踢出此群成员,类似于 `kick` 行为。
*
* @param options 删除时可提供的额外选项。
* 可用:
* - [StandardDeleteOption.IGNORE_ON_FAILURE]

Check warning on line 106 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotMember.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'IGNORE_ON_FAILURE'

Check warning on line 106 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotMember.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'StandardDeleteOption'
* - [OneBotMemberDeleteOption] 的所有实现
*
* @throws Throwable 任何可能在请求API时得到的异常
*/
@JvmSynthetic
override suspend fun delete(vararg options: DeleteOption) {
StandardDeleteOption
SetGroupKickApi
}
}

/**
* 在 [OneBotMember.delete] 中可以使用的更多额外属性,
* 大多与 [SetGroupKickApi] 中的属性相关。
*
* @see OneBotMember.delete
*/
public sealed class OneBotMemberDeleteOption : DeleteOption {

/**
* 拒绝此人的加群请求,也就是踢出后将其屏蔽。
*
* @see SetGroupKickApi
*/
public data object RejectRequest : OneBotMemberDeleteOption()

public companion object {
/**
* 拒绝此人的加群请求,也就是踢出后将其屏蔽。
*
* @see SetGroupKickApi
* @see RejectRequest
*/
@get:JvmStatic
@get:JvmName("rejectRequest")
public val rejectRequest: RejectRequest
get() = RejectRequest

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@

package love.forte.simbot.component.onebot.v11.core.actor.internal

import love.forte.simbot.ability.DeleteOption
import love.forte.simbot.ability.StandardDeleteOption
import love.forte.simbot.common.collectable.Collectable
import love.forte.simbot.common.collectable.flowCollectable
import love.forte.simbot.common.id.ID
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroupDeleteOption
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.core.api.GetGroupInfoResult
import love.forte.simbot.component.onebot.v11.core.api.GetGroupMemberInfoApi
import love.forte.simbot.component.onebot.v11.core.api.GetGroupMemberListApi
import love.forte.simbot.component.onebot.v11.core.api.SetGroupLeaveApi
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.requestResultBy
Expand All @@ -38,6 +42,7 @@ import love.forte.simbot.component.onebot.v11.message.resolveToOneBotSegmentList
import love.forte.simbot.message.Message
import love.forte.simbot.message.MessageContent
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmInline


internal abstract class OneBotGroupImpl : OneBotGroup {
Expand Down Expand Up @@ -92,6 +97,49 @@ internal abstract class OneBotGroupImpl : OneBotGroup {
).requestDataBy(bot).toReceipt(bot)
}

override suspend fun delete(vararg options: DeleteOption) {
var mark = DeleteMark()
for (option in options) {
when {
mark.isFull -> break
option == StandardDeleteOption.IGNORE_ON_FAILURE -> mark = mark.ignoreFailure()
option == OneBotGroupDeleteOption.Dismiss -> mark = mark.dismiss()
}
}

doDelete(mark)
}

private suspend fun doDelete(mark: DeleteMark) {
kotlin.runCatching {
SetGroupLeaveApi.create(
groupId = id,
isDismiss = mark.isDismiss
).requestDataBy(bot)
}.onFailure { err ->
if (!mark.isIgnoreFailure) {
throw err
}
}
}

@JvmInline
internal value class DeleteMark(private val mark: Int = 0) {
fun ignoreFailure(): DeleteMark = DeleteMark(mark or IGNORE_FAILURE_MARK)
fun dismiss(): DeleteMark = DeleteMark(mark or DISMISS_MARK)

val isIgnoreFailure: Boolean get() = mark and IGNORE_FAILURE_MARK != 0
val isDismiss: Boolean get() = mark and DISMISS_MARK != 0
val isFull: Boolean get() = mark == FULL

private companion object {
const val IGNORE_FAILURE_MARK = 0b01
const val DISMISS_MARK = 0b10

const val FULL = 0b11
}
}

override fun toString(): String = "OneBotGroup(id=$id, bot=${bot.id})"
}

Expand Down
Loading

0 comments on commit bd57203

Please sign in to comment.