Skip to content

Commit

Permalink
Add support for Application Role Connection Metadata
Browse files Browse the repository at this point in the history
The DSL looks like this:
val records = kord.updateApplicationRoleConnectionMetadataRecords {
    record(
        type = BooleanEqual,
        key = "member",
        name = "Member of the secret circle",
        description = "Must be a member of the secret circle",
    ) {
        name(Locale.GERMAN, "Mitglied im geheimen Kreis")
        description(Locale.GERMAN, "Muss ein Mitglied im geheimen Kreis sein")
    }
    record(
        type = DateTimeLessThanOrEqual,
        key = "account_age",
        name = "Account age",
        description = "Account must be at least this many days old",
    ) {
        name(Locale.GERMAN, "Account Alter")
        description(Locale.GERMAN, "Der Account muss zumindest so viele Tage alt sein")
    }
}
println(records)

Note that this does not contain the ability to update the application
role connection for a user as it requires an OAuth2 access token which
Kord doesn't support.

see discord/discord-api-docs#5668
  • Loading branch information
lukellmann committed Jun 14, 2023
1 parent 4bca754 commit 2e61173
Show file tree
Hide file tree
Showing 15 changed files with 612 additions and 20 deletions.
116 changes: 106 additions & 10 deletions common/api/common.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// THIS FILE IS AUTO-GENERATED BY KordEnumProcessor.kt, DO NOT EDIT!
@file:Suppress(names = arrayOf("RedundantVisibilityModifier", "IncorrectFormatting",
"ReplaceArrayOfWithLiteral", "SpellCheckingInspection", "GrazieInspection"))

package dev.kord.common.entity

import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlin.String
import kotlin.Suppress
import kotlin.collections.List
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* Each [ApplicationRoleConnectionMetadataType] offers a comparison operation that allows guilds to
* configure role requirements based on metadata values stored by the bot. Bots specify a 'metadata
* value' for each user and guilds specify the required 'guild's configured value' within the guild
* role settings.
*
* See [ApplicationRoleConnectionMetadataType]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/resources/application-role-connection-metadata#application-role-connection-metadata-object-application-role-connection-metadata-type).
*/
@Serializable(with = ApplicationRoleConnectionMetadataType.Serializer::class)
public sealed class ApplicationRoleConnectionMetadataType(
/**
* The raw value used by Discord.
*/
public val `value`: Int,
) {
public final override fun equals(other: Any?): Boolean = this === other ||
(other is ApplicationRoleConnectionMetadataType && this.value == other.value)

public final override fun hashCode(): Int = value.hashCode()

public final override fun toString(): String =
"ApplicationRoleConnectionMetadataType.${this::class.simpleName}(value=$value)"

/**
* An unknown [ApplicationRoleConnectionMetadataType].
*
* This is used as a fallback for [ApplicationRoleConnectionMetadataType]s that haven't been
* added to Kord yet.
*/
public class Unknown(
`value`: Int,
) : ApplicationRoleConnectionMetadataType(value)

/**
* The metadata value (`integer`) is less than or equal to the guild's configured value
* (`integer`).
*/
public object IntegerLessThanOrEqual : ApplicationRoleConnectionMetadataType(1)

/**
* The metadata value (`integer`) is greater than or equal to the guild's configured value
* (`integer`).
*/
public object IntegerGreaterThanOrEqual : ApplicationRoleConnectionMetadataType(2)

/**
* The metadata value (`integer`) is equal to the guild's configured value (`integer`).
*/
public object IntegerEqual : ApplicationRoleConnectionMetadataType(3)

/**
* The metadata value (`integer`) is not equal to the guild's configured value (`integer`).
*/
public object IntegerNotEqual : ApplicationRoleConnectionMetadataType(4)

/**
* The metadata value (`ISO8601 string`) is less than or equal to the guild's configured value
* (`integer; days before current date`).
*/
public object DateTimeLessThanOrEqual : ApplicationRoleConnectionMetadataType(5)

/**
* The metadata value (`ISO8601 string`) is greater than or equal to the guild's configured
* value (`integer; days before current date`).
*/
public object DateTimeGreaterThanOrEqual : ApplicationRoleConnectionMetadataType(6)

/**
* The metadata value (`integer`) is equal to the guild's configured value (`integer; 1`).
*/
public object BooleanEqual : ApplicationRoleConnectionMetadataType(7)

/**
* The metadata value (`integer`) is not equal to the guild's configured value (`integer; 1`).
*/
public object BooleanNotEqual : ApplicationRoleConnectionMetadataType(8)

internal object Serializer : KSerializer<ApplicationRoleConnectionMetadataType> {
public override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.kord.common.entity.ApplicationRoleConnectionMetadataType",
PrimitiveKind.INT)

public override fun serialize(encoder: Encoder,
`value`: ApplicationRoleConnectionMetadataType) = encoder.encodeInt(value.value)

public override fun deserialize(decoder: Decoder) = when (val value = decoder.decodeInt()) {
1 -> IntegerLessThanOrEqual
2 -> IntegerGreaterThanOrEqual
3 -> IntegerEqual
4 -> IntegerNotEqual
5 -> DateTimeLessThanOrEqual
6 -> DateTimeGreaterThanOrEqual
7 -> BooleanEqual
8 -> BooleanNotEqual
else -> Unknown(value)
}
}

public companion object {
/**
* A [List] of all known [ApplicationRoleConnectionMetadataType]s.
*/
public val entries: List<ApplicationRoleConnectionMetadataType> by
lazy(mode = PUBLICATION) {
listOf(
IntegerLessThanOrEqual,
IntegerGreaterThanOrEqual,
IntegerEqual,
IntegerNotEqual,
DateTimeLessThanOrEqual,
DateTimeGreaterThanOrEqual,
BooleanEqual,
BooleanNotEqual,
)
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@file: GenerateKordEnum(
name = "ApplicationRoleConnectionMetadataType", valueType = INT,
kDoc = "Each [ApplicationRoleConnectionMetadataType] offers a comparison operation that allows guilds to " +
"configure role requirements based on metadata values stored by the bot. Bots specify a 'metadata value' for " +
"each user and guilds specify the required 'guild's configured value' within the guild role settings.",
docUrl = "https://discord.com/developers/docs/resources/application-role-connection-metadata#application-role-connection-metadata-object-application-role-connection-metadata-type",
entries = [
Entry(
"IntegerLessThanOrEqual", intValue = 1,
kDoc = "The metadata value (`integer`) is less than or equal to the guild's configured value (`integer`).",
),
Entry(
"IntegerGreaterThanOrEqual", intValue = 2,
kDoc = "The metadata value (`integer`) is greater than or equal to the guild's configured value " +
"(`integer`).",
),
Entry(
"IntegerEqual", intValue = 3,
kDoc = "The metadata value (`integer`) is equal to the guild's configured value (`integer`).",
),
Entry(
"IntegerNotEqual", intValue = 4,
kDoc = "The metadata value (`integer`) is not equal to the guild's configured value (`integer`).",
),
Entry(
"DateTimeLessThanOrEqual", intValue = 5,
kDoc = "The metadata value (`ISO8601 string`) is less than or equal to the guild's configured value " +
"(`integer; days before current date`).",
),
Entry(
"DateTimeGreaterThanOrEqual", intValue = 6,
kDoc = "The metadata value (`ISO8601 string`) is greater than or equal to the guild's configured value " +
"(`integer; days before current date`).",
),
Entry(
"BooleanEqual", intValue = 7,
kDoc = "The metadata value (`integer`) is equal to the guild's configured value (`integer; 1`).",
),
Entry(
"BooleanNotEqual", intValue = 8,
kDoc = "The metadata value (`integer`) is not equal to the guild's configured value (`integer; 1`).",
),
],
)

package dev.kord.common.entity

import dev.kord.common.Locale
import dev.kord.common.entity.optional.Optional
import dev.kord.ksp.GenerateKordEnum
import dev.kord.ksp.GenerateKordEnum.Entry
import dev.kord.ksp.GenerateKordEnum.ValueType.INT
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
public data class DiscordApplicationRoleConnectionMetadata(
val type: ApplicationRoleConnectionMetadataType,
val key: String,
val name: String,
@SerialName("name_localizations")
val nameLocalizations: Optional<Map<Locale, String>> = Optional.Missing(),
val description: String,
@SerialName("description_localizations")
val descriptionLocalizations: Optional<Map<Locale, String>> = Optional.Missing(),
)
5 changes: 5 additions & 0 deletions common/src/commonMain/kotlin/entity/DiscordApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed interface BaseDiscordApplication {
public val tags: Optional<List<String>>
public val installParams: Optional<InstallParams>
public val customInstallUrl: Optional<String>
public val roleConnectionsVerificationUrl: Optional<String>
}

/**
Expand Down Expand Up @@ -71,6 +72,8 @@ public data class DiscordApplication(
override val installParams: Optional<InstallParams> = Optional.Missing(),
@SerialName("custom_install_url")
override val customInstallUrl: Optional<String> = Optional.Missing(),
@SerialName("role_connections_verification_url")
override val roleConnectionsVerificationUrl: Optional<String> = Optional.Missing(),
) : BaseDiscordApplication

/**
Expand Down Expand Up @@ -106,6 +109,8 @@ public data class DiscordPartialApplication(
override val installParams: Optional<InstallParams> = Optional.Missing(),
@SerialName("custom_install_url")
override val customInstallUrl: Optional<String> = Optional.Missing(),
@SerialName("role_connections_verification_url")
override val roleConnectionsVerificationUrl: Optional<String> = Optional.Missing(),
) : BaseDiscordApplication

public enum class ApplicationFlag(public val code: Int) {
Expand Down
3 changes: 3 additions & 0 deletions common/src/commonMain/kotlin/entity/optional/Optional.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ public inline fun <E, T> Optional<List<E>>.mapList(mapper: (E) -> T): Optional<L

public fun <T> Optional<MutableList<T>>.mapCopy(): Optional<List<T>> = map { mutable -> mutable.toList() }

@JvmName("mapCopyOfMap")
public fun <K, V> Optional<MutableMap<K, V>>.mapCopy(): Optional<Map<K, V>> = map { mutable -> mutable.toMap() }


@Suppress("UNCHECKED_CAST")
public inline fun <K, V, R> Optional<Map<K, V>>.mapValues(mapper: (Map.Entry<K, V>) -> R): Optional<Map<K, R>> = when (this) {
Expand Down
Loading

0 comments on commit 2e61173

Please sign in to comment.