diff --git a/common/src/main/kotlin/entity/DiscordGuild.kt b/common/src/main/kotlin/entity/DiscordGuild.kt index d063bdd162d5..2b77282e92d9 100644 --- a/common/src/main/kotlin/entity/DiscordGuild.kt +++ b/common/src/main/kotlin/entity/DiscordGuild.kt @@ -74,7 +74,7 @@ data class DiscordUnavailableGuild( * @param approximateMemberCount The approximate number of members in this guild, returned from the `GET /guild/` endpoint when `with_counts` is `true`. * @param approximatePresenceCount The approximate number of non-offline members in this guild, returned from the `GET /guild/` endpoint when `with_counts` is `true`. * @param welcomeScreen The welcome screen of a Community guild, shown to new members. - * @param nsfw true if this guild is [designated as NSFW](https://support.discord.com/hc/en-us/articles/1500005389362-NSFW-Server-Designation) + * @param nsfwLevel Guild NSFW level. */ @Serializable data class DiscordGuild( @@ -153,7 +153,8 @@ data class DiscordGuild( val approximatePresenceCount: OptionalInt = OptionalInt.Missing, @SerialName("welcome_screen") val welcomeScreen: Optional = Optional.Missing(), - val nsfw: Boolean + @SerialName("nsfw_level") + val nsfwLevel: NsfwLevel ) /** @@ -501,6 +502,40 @@ sealed class MFALevel(val value: Int) { } } } +/** + * A representation of a [Discord Guild NSFW Level](https://discord.com/developers/docs/resources/guild#guild-object-guild-nsfw-level). + */ +@Serializable(with = NsfwLevel.Serializer::class) +sealed class NsfwLevel(val value: Int) { + class Unknown(value: Int) : NsfwLevel(value) + + object Default : NsfwLevel(0) + + object Explicit : NsfwLevel(1) + + object Safe : NsfwLevel(2) + + object AgeRestricted : NsfwLevel(3) + + internal object Serializer : KSerializer { + + override val descriptor: SerialDescriptor + get() = PrimitiveSerialDescriptor("Kord.GuildNsfwLevel", PrimitiveKind.INT) + + override fun deserialize(decoder: Decoder): NsfwLevel = when (val value = decoder.decodeInt()) { + 0 -> Default + 1 -> Explicit + 2 -> Safe + 3 -> AgeRestricted + else -> Unknown(value) + } + + override fun serialize(encoder: Encoder, value: NsfwLevel) { + encoder.encodeInt(value.value) + } + + } +} /** @@ -562,4 +597,4 @@ data class DiscordWelcomeScreen( val description: String?, @SerialName("welcome_channels") val welcomeChannels: List -) \ No newline at end of file +) diff --git a/common/src/test/kotlin/json/GuildTest.kt b/common/src/test/kotlin/json/GuildTest.kt index 8880dcd394b9..ffd6b5e9e464 100644 --- a/common/src/test/kotlin/json/GuildTest.kt +++ b/common/src/test/kotlin/json/GuildTest.kt @@ -59,7 +59,7 @@ class GuildTest { preferredLocale shouldBe "en-US" rulesChannelId shouldBe "441688182833020939" publicUpdatesChannelId shouldBe "281283303326089216" - nsfw shouldBe true + nsfwLevel shouldBe NsfwLevel.Default } } diff --git a/common/src/test/resources/json/guild/guild.json b/common/src/test/resources/json/guild/guild.json index a9f61a3f2588..ed1cca39c4a1 100644 --- a/common/src/test/resources/json/guild/guild.json +++ b/common/src/test/resources/json/guild/guild.json @@ -39,5 +39,5 @@ "preferred_locale": "en-US", "rules_channel_id": "441688182833020939", "public_updates_channel_id": "281283303326089216", - "nsfw": true + "nsfw_level": 0 } \ No newline at end of file diff --git a/core/src/main/kotlin/cache/data/GuildData.kt b/core/src/main/kotlin/cache/data/GuildData.kt index 32e92ede9b95..f6c175e1eacf 100644 --- a/core/src/main/kotlin/cache/data/GuildData.kt +++ b/core/src/main/kotlin/cache/data/GuildData.kt @@ -55,7 +55,7 @@ data class GuildData( val approximateMemberCount: OptionalInt = OptionalInt.Missing, val approximatePresenceCount: OptionalInt = OptionalInt.Missing, val welcomeScreen: Optional = Optional.Missing(), - val nsfw: Boolean + val nsfwLevel: NsfwLevel ) { companion object { @@ -116,7 +116,7 @@ data class GuildData( approximateMemberCount = approximateMemberCount, approximatePresenceCount = approximatePresenceCount, welcomeScreen = welcomeScreen.map { WelcomeScreenData.from(it) }, - nsfw = nsfw + nsfwLevel = nsfwLevel ) } } diff --git a/core/src/main/kotlin/entity/Guild.kt b/core/src/main/kotlin/entity/Guild.kt index a996240e8e15..2722411b6a66 100644 --- a/core/src/main/kotlin/entity/Guild.kt +++ b/core/src/main/kotlin/entity/Guild.kt @@ -333,9 +333,9 @@ class Guild( val welcomeScreen: WelcomeScreen? get() = data.welcomeScreen.unwrap { WelcomeScreen(it, kord) } /** - * True if this guild is [designated as NSFW](https://support.discord.com/hc/en-us/articles/1500005389362-NSFW-Server-Designation) + * The [NSFW Level](https://discord.com/developers/docs/resources/guild#guild-object-guild-nsfw-level) of this Guild */ - val nsfw: Boolean get() = data.nsfw + val nsfw: NsfwLevel get() = data.nsfwLevel /** * Requests to get the [VoiceChannel] represented by the [afkChannelId], diff --git a/core/src/test/kotlin/performance/KordEventDropTest.kt b/core/src/test/kotlin/performance/KordEventDropTest.kt index 19c1aa4dc77c..71ade851ea06 100644 --- a/core/src/test/kotlin/performance/KordEventDropTest.kt +++ b/core/src/test/kotlin/performance/KordEventDropTest.kt @@ -85,7 +85,7 @@ class KordEventDropTest { vanityUrlCode = null, banner = null, publicUpdatesChannelId = null, - nsfw = false + nsfwLevel = NsfwLevel.Default ), 0) val counter = AtomicInteger(0)