Skip to content

Commit

Permalink
Compatibility for Intents
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Aug 21, 2023
1 parent f93ac62 commit e1e75c4
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 29 deletions.
3 changes: 3 additions & 0 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ public final class dev/kord/core/Unsafe {

public final class dev/kord/core/UtilKt {
public static final fun enableEvent (Ldev/kord/gateway/Intents$Builder;Lkotlin/reflect/KClass;)V
public static final fun enableEvent (Ldev/kord/gateway/Intents$IntentsBuilder;Lkotlin/reflect/KClass;)V
public static final fun enableEvents (Ldev/kord/gateway/Intents$Builder;Ljava/lang/Iterable;)V
public static final fun enableEvents (Ldev/kord/gateway/Intents$Builder;[Lkotlin/reflect/KClass;)V
public static final fun enableEvents (Ldev/kord/gateway/Intents$IntentsBuilder;Ljava/lang/Iterable;)V
public static final fun enableEvents (Ldev/kord/gateway/Intents$IntentsBuilder;[Lkotlin/reflect/KClass;)V
}

public abstract interface class dev/kord/core/behavior/ApplicationCommandBehavior : dev/kord/core/entity/Entity {
Expand Down
28 changes: 28 additions & 0 deletions core/src/commonMain/kotlin/Util.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.kord.core

import dev.kord.common.annotation.KordInternal
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.Message
import dev.kord.core.entity.channel.thread.ThreadChannel
Expand Down Expand Up @@ -243,6 +244,10 @@ internal fun paginateThreads(
*/
public inline fun <reified T : Event> Intents.Builder.enableEvent(): Unit = enableEvent(T::class)

@Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith")
@Deprecated("'Intents.IntentsBuilder' is deprecated, use 'Intents.Builder' instead.", level = DeprecationLevel.WARNING)
public inline fun <reified T : Event> Intents.IntentsBuilder.enableEvent(): Unit = enableEvent0(T::class)

/**
* Adds the necessary [Intent]s to receive the specified types of [events] in all variations and with all data
* available.
Expand All @@ -256,6 +261,11 @@ public inline fun <reified T : Event> Intents.Builder.enableEvent(): Unit = enab
public fun Intents.Builder.enableEvents(events: Iterable<KClass<out Event>>): Unit =
events.forEach { enableEvent(it) }

@Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith")
@Deprecated("'Intents.IntentsBuilder' is deprecated, use 'Intents.Builder' instead.", level = DeprecationLevel.WARNING)
public fun Intents.IntentsBuilder.enableEvents(events: Iterable<KClass<out Event>>): Unit =
events.forEach { enableEvent0(it) }

/**
* Adds the necessary [Intent]s to receive the specified types of [events] in all variations and with all data
* available.
Expand All @@ -269,6 +279,11 @@ public fun Intents.Builder.enableEvents(events: Iterable<KClass<out Event>>): Un
public fun Intents.Builder.enableEvents(vararg events: KClass<out Event>): Unit =
events.forEach { enableEvent(it) }

@Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith")
@Deprecated("'Intents.IntentsBuilder' is deprecated, use 'Intents.Builder' instead.", level = DeprecationLevel.WARNING)
public fun Intents.IntentsBuilder.enableEvents(vararg events: KClass<out Event>): Unit =
events.forEach { enableEvent0(it) }

/**
* Adds the necessary [Intent]s to receive the specified type of [event] in all variations and with all data available.
*
Expand Down Expand Up @@ -443,5 +458,18 @@ public fun Intents.Builder.enableEvent(event: KClass<out Event>): Unit = when (e
else -> Unit
}

@Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith")
@Deprecated("'Intents.IntentsBuilder' is deprecated, use 'Intents.Builder' instead.", level = DeprecationLevel.WARNING)
public fun Intents.IntentsBuilder.enableEvent(event: KClass<out Event>) {
enableEvent0(event)
}

@Suppress("DEPRECATION")
@PublishedApi
@KordInternal
internal fun Intents.IntentsBuilder.enableEvent0(event: KClass<out Event>) {
+(Intents.Builder(flags().code).apply { enableEvent(event) }.build())
}

// Replacement of Objects.hash
internal fun hash(vararg values: Any?) = values.contentHashCode()
33 changes: 31 additions & 2 deletions gateway/api/gateway.api
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,7 @@ public final class dev/kord/gateway/Intent$AutoModerationExecution : dev/kord/ga
public final class dev/kord/gateway/Intent$Companion {
public final fun fromShift (I)Ldev/kord/gateway/Intent;
public final fun getEntries ()Ljava/util/List;
public final fun getValues ()Ljava/util/Set;
}

public final class dev/kord/gateway/Intent$DirectMessageTyping : dev/kord/gateway/Intent {
Expand Down Expand Up @@ -1297,11 +1298,15 @@ public final class dev/kord/gateway/Intent$Unknown : dev/kord/gateway/Intent {

public final class dev/kord/gateway/IntentKt {
public static final fun Intents (Ljava/lang/Iterable;)Ldev/kord/gateway/Intents;
public static final fun Intents (Ljava/lang/String;)Ldev/kord/gateway/Intents;
public static final fun Intents (Lkotlin/jvm/functions/Function1;)Ldev/kord/gateway/Intents;
public static final fun Intents ([Ldev/kord/gateway/Intent;)Ldev/kord/gateway/Intents;
public static final fun Intents ([Ldev/kord/gateway/Intents;)Ldev/kord/gateway/Intents;
public static synthetic fun Intents$default (Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ldev/kord/gateway/Intents;
public static final fun Intents0 (Ljava/lang/Iterable;)Ldev/kord/gateway/Intents;
public static final fun Intents0 (Lkotlin/jvm/functions/Function1;)Ldev/kord/gateway/Intents;
public static synthetic fun Intents0$default (Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ldev/kord/gateway/Intents;
public static final synthetic fun IntentsWithIterable (Ljava/lang/Iterable;)Ldev/kord/gateway/Intents;
}

public final class dev/kord/gateway/Intents {
Expand All @@ -1312,6 +1317,7 @@ public final class dev/kord/gateway/Intents {
public final fun copy (Ldev/kord/common/DiscordBitSet;)Ldev/kord/gateway/Intents;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/gateway/Intents;
public static synthetic fun copy$default (Ldev/kord/gateway/Intents;Ldev/kord/common/DiscordBitSet;ILjava/lang/Object;)Ldev/kord/gateway/Intents;
public final fun copy0 (Lkotlin/jvm/functions/Function1;)Ldev/kord/gateway/Intents;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()Ldev/kord/common/DiscordBitSet;
public final fun getValues ()Ljava/util/Set;
Expand All @@ -1328,25 +1334,47 @@ public final class dev/kord/gateway/Intents$Builder {
public fun <init> (Ldev/kord/common/DiscordBitSet;)V
public synthetic fun <init> (Ldev/kord/common/DiscordBitSet;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun build ()Ldev/kord/gateway/Intents;
public final fun flags ()Ldev/kord/gateway/Intents;
public final fun unaryMinus (Ldev/kord/gateway/Intent;)V
public final fun unaryMinus (Ldev/kord/gateway/Intents;)V
public final fun unaryPlus (Ldev/kord/gateway/Intent;)V
public final fun unaryPlus (Ldev/kord/gateway/Intents;)V
}

public final class dev/kord/gateway/Intents$Companion {
public final fun getAll ()Ldev/kord/gateway/Intents;
public final fun getNonPrivileged ()Ldev/kord/gateway/Intents;
public final fun getNone ()Ldev/kord/gateway/Intents;
public final fun getPrivileged ()Ldev/kord/gateway/Intents;
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class dev/kord/gateway/Intents$IntentsBuilder {
public fun <init> ()V
public fun <init> (Ldev/kord/common/DiscordBitSet;)V
public synthetic fun <init> (Ldev/kord/common/DiscordBitSet;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun flags ()Ldev/kord/gateway/Intents;
public final fun unaryMinus (Ldev/kord/gateway/Intent;)V
public final fun unaryMinus (Ldev/kord/gateway/Intents;)V
public final fun unaryPlus (Ldev/kord/gateway/Intent;)V
public final fun unaryPlus (Ldev/kord/gateway/Intents;)V
}

public final class dev/kord/gateway/IntentsKt {
public static final synthetic fun IntentsWithIterable (Ljava/lang/Iterable;)Ldev/kord/gateway/Intents;
public static final fun getALL (Ldev/kord/gateway/Intents$Companion;)Ldev/kord/gateway/Intents;
public static final fun getNONE (Ldev/kord/gateway/Intents$Companion;)Ldev/kord/gateway/Intents;
public static final fun getNON_PRIVILEGED (Ldev/kord/gateway/Intents$Companion;)Ldev/kord/gateway/Intents;
public static final fun getPRIVILEGED (Ldev/kord/gateway/Intents$Companion;)Ldev/kord/gateway/Intents;
}

public final class dev/kord/gateway/IntentsSerializer : kotlinx/serialization/KSerializer {
public static final field INSTANCE Ldev/kord/gateway/IntentsSerializer;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ldev/kord/gateway/Intents;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Ldev/kord/gateway/Intents;)V
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
}

public final class dev/kord/gateway/InteractionCreate : dev/kord/gateway/DispatchEvent {
public fun <init> (Ldev/kord/common/entity/DiscordInteraction;Ljava/lang/Integer;)V
public final fun component1 ()Ldev/kord/common/entity/DiscordInteraction;
Expand Down Expand Up @@ -1972,6 +2000,7 @@ public final class dev/kord/gateway/builder/LoginBuilder {
public final fun getName ()Ljava/lang/String;
public final fun getPresence ()Ldev/kord/gateway/DiscordPresence;
public final fun intents (Lkotlin/jvm/functions/Function1;)V
public final fun intents0 (Lkotlin/jvm/functions/Function1;)V
public final fun presence (Lkotlin/jvm/functions/Function1;)V
public final fun setIntents (Ldev/kord/gateway/Intents;)V
public final fun setName (Ljava/lang/String;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
@file:Suppress(names = arrayOf("IncorrectFormatting", "ReplaceArrayOfWithLiteral",
"SpellCheckingInspection", "GrazieInspection"))

Expand Down Expand Up @@ -279,6 +278,14 @@ public sealed class Intent(
)
}

// TODO uncomment annotation in Intents.kt and delete this file when this property is removed after deprecation
// cycle
@Deprecated(
"Renamed to 'entries'.",
ReplaceWith("Intent.entries", imports = ["dev.kord.gateway.Intent"]),
DeprecationLevel.WARNING,
)
public val values: Set<Intent> get() = entries.toSet()

/**
* Returns an instance of [Intent] with [Intent.shift] equal to the specified [shift].
Expand Down Expand Up @@ -425,11 +432,23 @@ public class Intents internal constructor(
/**
* Returns a copy of this instance of [Intents] modified with [builder].
*/
@JvmName("copy0") // todo other name when deprecated overload is removed
public inline fun copy(builder: Builder.() -> Unit): Intents {
contract { callsInPlace(builder, EXACTLY_ONCE) }
return Builder(code.copy()).apply(builder).build()
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE","DEPRECATION")
@Deprecated(
"'Intents.IntentsBuilder' is deprecated, use 'Intents.Builder' instead.",
level = DeprecationLevel.WARNING,
)
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun copy(block: IntentsBuilder.() -> Unit): Intents {
contract { callsInPlace(block, EXACTLY_ONCE) }
return IntentsBuilder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is Intents && this.code == other.code)

Expand All @@ -453,6 +472,20 @@ public class Intents internal constructor(
@Deprecated(message = "Intents is no longer a data class. Deprecated without a replacement.")
public fun copy(code: DiscordBitSet = this.code): Intents = Intents(code)

@Deprecated(
"Renamed to 'Builder'.",
ReplaceWith("Intents.Builder", imports = ["dev.kord.gateway.Intents"]),
DeprecationLevel.WARNING,
)
public class IntentsBuilder(code: DiscordBitSet = EmptyBitSet()) {
private val delegate = Builder(code)
public operator fun Intents.unaryPlus(): Unit = with(delegate) { unaryPlus() }
public operator fun Intent.unaryPlus(): Unit = with(delegate) { unaryPlus() }
public operator fun Intent.unaryMinus(): Unit = with(delegate) { unaryMinus() }
public operator fun Intents.unaryMinus(): Unit = with(delegate) { unaryMinus() }
public fun flags(): Intents = delegate.build()
}

public class Builder(
private val code: DiscordBitSet = EmptyBitSet(),
) {
Expand Down Expand Up @@ -489,15 +522,6 @@ public class Intents internal constructor(
* [Builder].
*/
public fun build(): Intents = Intents(code.copy())

/**
* @suppress
*/
@Deprecated(
message = "Renamed to 'build'",
replaceWith = ReplaceWith(expression = "this.build()", imports = arrayOf()),
)
public fun flags(): Intents = build()
}

internal object Serializer : KSerializer<Intents> {
Expand All @@ -513,16 +537,67 @@ public class Intents internal constructor(
override fun deserialize(decoder: Decoder): Intents =
Intents(decoder.decodeSerializableValue(delegate))
}

public companion object {
@Deprecated(
"Renamed to 'ALL'",
ReplaceWith("Intents.ALL", imports = [" dev.kord.gateway.Intents", " dev.kord.gateway.ALL"]),
DeprecationLevel.WARNING,
)
@PrivilegedIntent
public val all: Intents get() = ALL

@Deprecated(
"Renamed to 'PRIVILEGED'",
ReplaceWith("Intents.PRIVILEGED", imports = [" dev.kord.gateway.Intents", " dev.kord.gateway.PRIVILEGED"]),
DeprecationLevel.WARNING,
)
@PrivilegedIntent
public val privileged: Intents get() = PRIVILEGED

@Deprecated(
"Renamed to 'NON_PRIVILEGED'",
ReplaceWith(
"Intents.NON_PRIVILEGED",
imports = [" dev.kord.gateway.Intents", " dev.kord.gateway.NON_PRIVILEGED"],
),
DeprecationLevel.WARNING,
)
public val nonPrivileged: Intents get() = NON_PRIVILEGED

@Deprecated(
"Renamed to 'NONE'",
ReplaceWith("Intents.NONE", imports = [" dev.kord.gateway.Intents", " dev.kord.gateway.NONE"]),
DeprecationLevel.WARNING,
)
public val none: Intents get() = NONE
}
}

@Deprecated(
"Replaced by 'Intents.serializer()'.",
ReplaceWith("Intents.serializer()", imports = ["dev.kord.gateway.Intents"]),
DeprecationLevel.WARNING,
)
public object IntentsSerializer : KSerializer<Intents> by Intents.Serializer

/**
* Returns an instance of [Intents] built with [Intents.Builder].
*/
@JvmName("Intents0") // TODO other name when deprecated overload is removed
public inline fun Intents(builder: Intents.Builder.() -> Unit = {}): Intents {
contract { callsInPlace(builder, EXACTLY_ONCE) }
return Intents.Builder().apply(builder).build()
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "DEPRECATION")
@Deprecated("'Intents.IntentsBuilder' is deprecated, use 'Intents.Builder' instead.", level = DeprecationLevel.WARNING)
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun Intents(builder: Intents.IntentsBuilder.() -> Unit = {}): Intents {
contract { callsInPlace(builder, EXACTLY_ONCE) }
return Intents.IntentsBuilder().apply(builder).flags()
}

/**
* Returns an instance of [Intents] that has all bits set that are set in any element of [flags].
*/
Expand Down Expand Up @@ -551,3 +626,19 @@ public fun Intents(flags: Iterable<Intent>): Intents = Intents {
public fun Intents(flags: Iterable<Intents>): Intents = Intents {
flags.forEach { +it }
}

// TODO uncomment annotation in Intents.kt and delete this file when these functions are removed after deprecation cycle
@Suppress("FunctionName")
@Deprecated("Binary compatibility, keep for some releases.", level = DeprecationLevel.HIDDEN)
public fun IntentsWithIterable(flags: Iterable<Intents>): Intents = Intents(flags)

@Deprecated(
"Don't construct an instance of 'Intents' from a raw value. Use the factory functions described in the " +
"documentation instead.",
ReplaceWith(
"Intents.Builder(DiscordBitSet(value)).build()",
imports = ["dev.kord.gateway.Intents", "dev.kord.common.DiscordBitSet"],
),
DeprecationLevel.WARNING,
)
public fun Intents(value: String): Intents = Intents(DiscordBitSet(value))
28 changes: 11 additions & 17 deletions gateway/src/commonMain/kotlin/Intents.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
@file:Generate(
BIT_SET_FLAGS, name = "Intent", valueName = "code", collectionWasDataClass = true,
BIT_SET_FLAGS, name = "Intent", valueName = "code",
kDoc = "Values that enable a group of events as defined by Discord.",
docUrl = "https://discord.com/developers/docs/topics/gateway#gateway-intents",
entries = [
Expand Down Expand Up @@ -37,14 +38,6 @@
[here](https://discord.com/developers/docs/topics/gateway#thread-members-update))
""",
),
Entry(
"GuildBans", shift = 2,
deprecated = Deprecated(
"Renamed to 'GuildModeration'",
ReplaceWith("Intent.GuildModeration", "dev.kord.gateway.Intent"),
DeprecationLevel.WARNING,
),
),
Entry(
"GuildModeration", shift = 2,
kDoc = """
Expand Down Expand Up @@ -207,10 +200,18 @@ import dev.kord.ksp.Generate
import dev.kord.ksp.Generate.EntityType.BIT_SET_FLAGS
import dev.kord.ksp.Generate.Entry
import kotlin.RequiresOptIn.Level.ERROR
import kotlin.annotation.AnnotationRetention.*
import kotlin.annotation.AnnotationRetention.BINARY
import kotlin.annotation.AnnotationTarget.*
private const val privilegedIntentAnnotation = "dev.kord.gateway.PrivilegedIntent"
*/

package dev.kord.gateway

import dev.kord.gateway.Intent.*
import kotlin.RequiresOptIn.Level.ERROR
import kotlin.annotation.AnnotationRetention.BINARY
import kotlin.annotation.AnnotationTarget.*

/**
* Some [Intent]s are defined as "privileged" due to the sensitive nature of the data and cannot be used by Kord without
Expand Down Expand Up @@ -273,10 +274,3 @@ public val Intents.Companion.NON_PRIVILEGED: Intents get() = NON_PRIVILEGED_INTE

/** An [Intents] instance that contains no [Intent]s. */
public val Intents.Companion.NONE: Intents get() = NO_INTENTS


@Suppress("FunctionName")
@Deprecated("Binary compatibility. Keep for some releases.", level = DeprecationLevel.HIDDEN)
public fun IntentsWithIterable(intents: Iterable<Intent>): Intents = Intents {
intents.forEach { +it }
}
Loading

0 comments on commit e1e75c4

Please sign in to comment.