Skip to content

Commit

Permalink
feat(core): 增加一个 bitBased 类型的 IntentsConfig,用来快捷地通过比特位标记来进行配置
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jul 15, 2024
1 parent fb518ec commit e615fa1
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$AudioAction : lov
public static final field AUDIO_ON_MIC_TYPE Ljava/lang/String;
public static final field AUDIO_START_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$AudioAction;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2548,6 +2549,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$AudioOrLiveChanne
public static final field AUDIO_OR_LIVE_CHANNEL_MEMBER_ENTER_TYPE Ljava/lang/String;
public static final field AUDIO_OR_LIVE_CHANNEL_MEMBER_EXIT_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$AudioOrLiveChannelMember;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2562,6 +2564,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$DirectMessage : l
public static final field DIRECT_MESSAGE_CREATE_TYPE Ljava/lang/String;
public static final field DIRECT_MESSAGE_DELETE_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$DirectMessage;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2579,6 +2582,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$ForumsEvent : lov
public static final field FORUM_THREAD_DELETE_TYPE Ljava/lang/String;
public static final field FORUM_THREAD_UPDATE_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$ForumsEvent;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2598,6 +2602,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$GroupAndC2CEvent
public static final field GROUP_MSG_RECEIVE_TYPE Ljava/lang/String;
public static final field GROUP_MSG_REJECT_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$GroupAndC2CEvent;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2610,6 +2615,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$GuildMembers : lo
public static final field GUILD_MEMBER_REMOVE_TYPE Ljava/lang/String;
public static final field GUILD_MEMBER_UPDATE_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$GuildMembers;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2619,6 +2625,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$GuildMembers : lo

public final class love/forte/simbot/qguild/event/EventIntents$GuildMessageReactions : love/forte/simbot/qguild/event/EventIntents {
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$GuildMessageReactions;
public static final field INTENTS_INDEX I
public static final field MESSAGE_REACTION_ADD_TYPE Ljava/lang/String;
public static final field MESSAGE_REACTION_REMOVE_TYPE Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
Expand All @@ -2630,6 +2637,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$GuildMessageReact

public final class love/forte/simbot/qguild/event/EventIntents$GuildMessages : love/forte/simbot/qguild/event/EventIntents {
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$GuildMessages;
public static final field INTENTS_INDEX I
public static final field MESSAGE_CREATE_TYPE Ljava/lang/String;
public static final field MESSAGE_DELETE_TYPE Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
Expand All @@ -2647,6 +2655,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$Guilds : love/for
public static final field GUILD_DELETE_TYPE Ljava/lang/String;
public static final field GUILD_UPDATE_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$Guilds;
public static final field INTENTS_INDEX I
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
public fun getIntentsValue ()I
Expand All @@ -2656,6 +2665,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$Guilds : love/for

public final class love/forte/simbot/qguild/event/EventIntents$Interaction : love/forte/simbot/qguild/event/EventIntents {
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$Interaction;
public static final field INTENTS_INDEX I
public static final field INTERACTION_CREATE_TYPE Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
Expand All @@ -2666,6 +2676,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$Interaction : lov

public final class love/forte/simbot/qguild/event/EventIntents$MessageAudit : love/forte/simbot/qguild/event/EventIntents {
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$MessageAudit;
public static final field INTENTS_INDEX I
public static final field MESSAGE_AUDIT_PASS_TYPE Ljava/lang/String;
public static final field MESSAGE_AUDIT_REJECT_TYPE Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
Expand All @@ -2677,6 +2688,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$MessageAudit : lo

public final class love/forte/simbot/qguild/event/EventIntents$OpenForumsEvent : love/forte/simbot/qguild/event/EventIntents {
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$OpenForumsEvent;
public static final field INTENTS_INDEX I
public static final field OPEN_FORUM_POST_CREATE_TYPE Ljava/lang/String;
public static final field OPEN_FORUM_POST_DELETE_TYPE Ljava/lang/String;
public static final field OPEN_FORUM_REPLY_CREATE_TYPE Ljava/lang/String;
Expand All @@ -2694,6 +2706,7 @@ public final class love/forte/simbot/qguild/event/EventIntents$OpenForumsEvent :
public final class love/forte/simbot/qguild/event/EventIntents$PublicGuildMessages : love/forte/simbot/qguild/event/EventIntents {
public static final field AT_MESSAGE_CREATE_TYPE Ljava/lang/String;
public static final field INSTANCE Llove/forte/simbot/qguild/event/EventIntents$PublicGuildMessages;
public static final field INTENTS_INDEX I
public static final field PUBLIC_MESSAGE_DELETE_TYPE Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public static final fun getIntents ()I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public sealed class EventIntents {
*
*/
public data object Guilds : EventIntents() {
internal const val INTENTS: Int = 1 shl 0
public const val INTENTS_INDEX: Int = 0
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 频道事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -157,7 +158,8 @@ public sealed class EventIntents {
* ```
*/
public data object GuildMembers : EventIntents() {
internal const val INTENTS = 1 shl 1
public const val INTENTS_INDEX: Int = 1
internal const val INTENTS = 1 shl INTENTS_INDEX

/** 频道成员事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -187,7 +189,8 @@ public sealed class EventIntents {
*/
@PrivateDomainOnly
public data object GuildMessages : EventIntents() {
internal const val INTENTS = 1 shl 9
public const val INTENTS_INDEX: Int = 9
internal const val INTENTS = 1 shl INTENTS_INDEX

/** 频道消息事件 `intents` */
@get:JvmStatic
Expand All @@ -212,7 +215,8 @@ public sealed class EventIntents {
* ```
*/
public data object GuildMessageReactions : EventIntents() {
internal const val INTENTS: Int = 1 shl 10
public const val INTENTS_INDEX: Int = 10
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 表情表态事件 `intents` */
@get:JvmStatic
Expand All @@ -238,7 +242,8 @@ public sealed class EventIntents {
* ```
*/
public data object DirectMessage : EventIntents() {
internal const val INTENTS: Int = 1 shl 12
public const val INTENTS_INDEX: Int = 12
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 表情表态事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -269,7 +274,8 @@ public sealed class EventIntents {
* ```
*/
public data object OpenForumsEvent : EventIntents() {
internal const val INTENTS: Int = 1 shl 18
public const val INTENTS_INDEX: Int = 18
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 论坛事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -310,7 +316,8 @@ public sealed class EventIntents {
* ```
*/
public data object AudioOrLiveChannelMember : EventIntents() {
internal const val INTENTS: Int = 1 shl 19
public const val INTENTS_INDEX: Int = 19
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 音视频/直播子频道成员进出事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -344,7 +351,8 @@ public sealed class EventIntents {
* ```
*/
public data object GroupAndC2CEvent : EventIntents() {
internal const val INTENTS: Int = 1 shl 25
public const val INTENTS_INDEX: Int = 25
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** C2C群聊相关事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -412,7 +420,8 @@ public sealed class EventIntents {
* ```
*/
public data object Interaction : EventIntents() {
internal const val INTENTS: Int = 1 shl 26
public const val INTENTS_INDEX: Int = 26
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 互动事件 `intents` */
@get:JvmStatic
Expand All @@ -435,7 +444,8 @@ public sealed class EventIntents {
* ```
*/
public data object MessageAudit : EventIntents() {
internal const val INTENTS: Int = 1 shl 27
public const val INTENTS_INDEX: Int = 27
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 互动事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -468,7 +478,8 @@ public sealed class EventIntents {
*/
@PrivateDomainOnly
public data object ForumsEvent : EventIntents() {
internal const val INTENTS: Int = 1 shl 28
public const val INTENTS_INDEX: Int = 28
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 论坛事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -515,7 +526,8 @@ public sealed class EventIntents {
*/
@PrivateDomainOnly
public data object AudioAction : EventIntents() {
internal const val INTENTS: Int = 1 shl 29
public const val INTENTS_INDEX: Int = 29
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 论坛事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -548,7 +560,8 @@ public sealed class EventIntents {
*/
@PrivateDomainOnly
public data object PublicGuildMessages : EventIntents() {
internal const val INTENTS = 1 shl 30
public const val INTENTS_INDEX: Int = 30
internal const val INTENTS: Int = 1 shl INTENTS_INDEX

/** 论坛事件 `intents` */
@get:JvmStatic
Expand Down Expand Up @@ -592,7 +605,8 @@ public sealed class EventIntents {
*
* @see EventIntentsAggregation.allEventIntents
*/
@Deprecated("Use EventIntentsAggregation.allEventIntents",
@Deprecated(
"Use EventIntentsAggregation.allEventIntents",
ReplaceWith("EventIntentsAggregation.allEventIntents()")
)
public val EventIntentsInstances: Array<EventIntents>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public sealed class IntentsConfig {
* "names": ["Guilds", "PublicGuildMessages"]
* }
* ```
* @property names 继承了 [EventIntents] 的 `object` 的简单类名,例如 `Guilds`.
* @property names 继承了 [EventIntents] 的 `object` 的简单名称,例如 `Guilds`,
* 可参考 [EventIntentsAggregation.getByName] 参考所有可用名称列表。

Check warning on line 66 in simbot-component-qq-guild-core/src/commonMain/kotlin/love/forte/simbot/component/qguild/bot/config/IntentsConfig.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'EventIntentsAggregation'

Check warning on line 66 in simbot-component-qq-guild-core/src/commonMain/kotlin/love/forte/simbot/component/qguild/bot/config/IntentsConfig.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'getByName'
* @throws NoSuchElementException 如果名称没有找到
*/
@Serializable
@UsedOnlyForConfigSerialization
@SerialName("nameBased")
public data class Names(val names: Set<String>) : IntentsConfig() {

override val intents: Intents
get() {
if (names.isEmpty()) {
Expand All @@ -81,4 +81,26 @@ public sealed class IntentsConfig {
}
}

/**
* 通过 [Intents] 的位索引值来配置 [Intents] 的结果。
* ```json
* {
* "type": "bitBased",
* "bits": [0, 1, 30]
* }
* ```
* 上面的 `0, 1, 30` 即代表订阅 `1<<0 | 1<<1 | 1<<30`
*
* @property bits 需要订阅的bit的位索引,值应当在 0 ~ 31 之内,
* 但是代码内不做校验。
*/
@SerialName("bitBased")
public data class Bits(val bits: Set<Int>) : IntentsConfig() {
override val intents: Intents
get() {
val intentsValue = bits.fold(0) { acc, index -> acc or (1 shl index) }
return Intents(intentsValue)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ class FileConfigTest {
)
}

@Test
fun intentsConfigBitTest() {
assertEquals(
IntentsConfig.Bits(
setOf(
EventIntents.PublicGuildMessages.INTENTS_INDEX
)
).intents,
EventIntents.PublicGuildMessages.intents
)
assertEquals(
IntentsConfig.Bits(
setOf(
EventIntents.Guilds.INTENTS_INDEX,
EventIntents.PublicGuildMessages.INTENTS_INDEX
)
).intents,
EventIntents.Guilds.intents + EventIntents.PublicGuildMessages.intents
)
}

@Test
fun botFileConfigurationTest() {
val json = Json {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public interface BotConfiguration {
@get:JvmSynthetic
public val intents: Intents


/**
* @see intents
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import love.forte.simbot.qguild.event.Shard
import love.forte.simbot.qguild.event.Signal
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic


Expand Down Expand Up @@ -78,9 +79,17 @@ public class ConfigurableBotConfiguration : BotConfiguration {
}

/**
* 异常处理器。
* TODO
* 向 [intents] 中添加一个 [Intents] 值。
*/
@JvmName("addIntents")
public fun addIntents(intents: Intents): ConfigurableBotConfiguration = apply {
this.intents += intents
}

/**
* @suppress 未实现
*/
@Deprecated("Not implemented")
override var exceptionHandler: ExceptionProcessor? = null

/**
Expand Down Expand Up @@ -170,6 +179,7 @@ public class ConfigurableBotConfiguration : BotConfiguration {
*/
override var apiDecoder: Json = QQGuild.DefaultJson

@Suppress("DEPRECATION")
internal fun release(): BotConfiguration = BotConfigurationImpl(
coroutineContext = coroutineContext,
shard = shard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,18 @@ internal class Connect(
val intents = bot.configuration.intents
val properties = bot.configuration.clientProperties

logger.debug("Connect intents : {}", intents)
logger.debug("Connect properties: {}", properties)
if (logger.isDebugEnabled) {
var bitString = intents.value.toString(2)
if (bitString.startsWith("-")) {
bitString = bitString.substring(1)
}
if (bitString.length < 32) {
bitString = "0".repeat(32 - bitString.length) + bitString
}

logger.debug("Connect intents : {} (0x{})", intents, bitString)
logger.debug("Connect properties: {}", properties)
}

val identify = Signal.Identify(
Signal.Identify.Data(
Expand Down

0 comments on commit e615fa1

Please sign in to comment.