Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize unavailable guilds properly #675

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions common/src/main/kotlin/entity/DiscordGuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,60 @@ public data class DiscordGuild(
*/
@Serializable
public data class DiscordPartialGuild(
val id: Snowflake,
val name: String,
val icon: String?,
val owner: OptionalBoolean = OptionalBoolean.Missing,
val permissions: Optional<Permissions> = Optional.Missing(),
val features: List<GuildFeature>,
@SerialName("welcome_screen") val welcomeScreen: Optional<DiscordWelcomeScreen> = Optional.Missing(),
@SerialName("vanity_url_code") val vanityUrlCode: Optional<String?> = Optional.Missing(),
val description: Optional<String?> = Optional.Missing(),
val banner: Optional<String?> = Optional.Missing(),
val splash: Optional<String?> = Optional.Missing(),
@SerialName("nsfw_level") val nsfwLevel: Optional<NsfwLevel> = Optional.Missing(),
@SerialName("verification_level")
val verificationLevel: Optional<VerificationLevel> = Optional.Missing(),
public val id: Snowflake,
public val name: Optional<String> = Optional.Missing(),
public val icon: Optional<String>? = Optional.Missing(),
@SerialName("icon_hash") public val iconHash: Optional<String?> = Optional.Missing(),
public val splash: Optional<String?> = Optional.Missing(),
@SerialName("discovery_splash") public val discoverySplash: Optional<String?> = Optional.Missing(),
public val owner: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("owner_id") public val ownerId: OptionalSnowflake = OptionalSnowflake.Missing,
public val permissions: Optional<Permissions> = Optional.Missing(),
@SerialName("afk_channel_id") public val afkChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("afk_timeout") public val afkTimeout: Optional<DurationInSeconds> = Optional.Missing(),
@SerialName("widget_enabled") public val widgetEnabled: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("widget_channel_id") public val widgetChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("verification_level") public val verificationLevel: Optional<VerificationLevel> = Optional.Missing(),
@SerialName("default_message_notifications") public val defaultMessageNotifications: Optional<DefaultMessageNotificationLevel> = Optional.Missing(),
@SerialName("explicit_content_filter") public val explicitContentFilter: Optional<ExplicitContentFilter> = Optional.Missing(),
public val roles: List<DiscordRole> = emptyList(),
public val emojis: List<DiscordEmoji> = emptyList(),
public val features: List<GuildFeature> = emptyList(),
@SerialName("mfa_level") public val mfaLevel: Optional<MFALevel> = Optional.Missing(),
@SerialName("application_id") public val applicationId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("system_channel_id") public val systemChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("system_channel_flags") public val systemChannelFlags: Optional<SystemChannelFlags> = Optional.Missing(),
@SerialName("rules_channel_id") public val rulesChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("joined_at") public val joinedAt: Optional<Instant> = Optional.Missing(),
public val large: OptionalBoolean = OptionalBoolean.Missing,
public val unavailable: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("member_count") public val memberCount: OptionalInt = OptionalInt.Missing,
@SerialName("voice_states") public val voiceStates: Optional<List<DiscordVoiceState>> = Optional.Missing(),
public val members: Optional<List<DiscordGuildMember>> = Optional.Missing(),
public val channels: Optional<List<DiscordChannel>> = Optional.Missing(),
public val threads: Optional<List<DiscordChannel>> = Optional.Missing(),
public val presences: Optional<List<DiscordPresenceUpdate>> = Optional.Missing(),
@SerialName("max_presences") public val maxPresences: OptionalInt? = OptionalInt.Missing,
@SerialName("max_members") public val maxMembers: OptionalInt = OptionalInt.Missing,
@SerialName("vanity_url_code") public val vanityUrlCode: Optional<String>? = Optional.Missing(),
public val description: Optional<String>? = Optional.Missing(),
public val banner: Optional<String>? = Optional.Missing(),
@SerialName("premium_tier") public val premiumTier: Optional<PremiumTier> = Optional.Missing(),
@SerialName("premium_subscription_count") public val premiumSubscriptionCount: OptionalInt = OptionalInt.Missing,
@SerialName("preferred_locale") public val preferredLocale: Optional<String> = Optional.Missing(),
@SerialName("public_updates_channel_id") public val publicUpdatesChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("max_video_channel_users") public val maxVideoChannelUsers: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_member_count") public val approximateMemberCount: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_presence_count") public val approximatePresenceCount: OptionalInt = OptionalInt.Missing,
@SerialName("welcome_screen") public val welcomeScreen: Optional<DiscordWelcomeScreen> = Optional.Missing(),
@SerialName("nsfw_level") public val nsfwLevel: Optional<NsfwLevel> = Optional.Missing(),
@SerialName("stage_instances")
val stageInstances: Optional<List<DiscordStageInstance>> = Optional.Missing(),
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
public val stageInstances: Optional<List<DiscordStageInstance>> = Optional.Missing(),
public val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("guild_scheduled_events")
val guildScheduledEvents: Optional<List<DiscordGuildScheduledEvent>> = Optional.Missing(),
public val guildScheduledEvents: Optional<List<DiscordGuildScheduledEvent>> = Optional.Missing(),
@SerialName("premium_progress_bar_enabled")
val premiumProgressBarEnabled: OptionalBoolean = OptionalBoolean.Missing

public val premiumProgressBarEnabled: OptionalBoolean = OptionalBoolean.Missing
)

/**
Expand Down
9 changes: 5 additions & 4 deletions common/src/test/kotlin/json/GuildTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package json

import dev.kord.common.entity.*
import dev.kord.common.entity.optional.value
import kotlinx.datetime.Instant
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -94,10 +95,10 @@ class GuildTest {

with(guild) {
id shouldBe "80351110224678912"
name shouldBe "1337 Krew"
icon shouldBe "8342729096ea3675442027381ff50dfe"
owner shouldBe true
permissions shouldBe Permissions("36953089")
name.value shouldBe "1337 Krew"
icon?.value shouldBe "8342729096ea3675442027381ff50dfe"
owner.value shouldBe true
permissions.value shouldBe Permissions("36953089")
features shouldBe listOf(GuildFeature.Community, GuildFeature.News)
}
}
Expand Down
56 changes: 52 additions & 4 deletions core/src/main/kotlin/cache/data/GuildData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public data class GuildData(
//val owner: OptionalBoolean = OptionalBoolean.Missing, useless?
val ownerId: Snowflake,
val permissions: Optional<Permissions> = Optional.Missing(),
@Deprecated("The region field has been moved to Channel#rtcRegion in Discord API v9", ReplaceWith("ChannelData#rtcRegion"))
val region: String,
val afkChannelId: Snowflake? = null,
val afkTimeout: DurationInSeconds,
val widgetEnabled: OptionalBoolean = OptionalBoolean.Missing,
Expand Down Expand Up @@ -90,7 +88,6 @@ public data class GuildData(
//owner = owner,
ownerId = ownerId,
permissions = permissions,
region = @Suppress("DEPRECATION") region,
afkChannelId = afkChannelId,
afkTimeout = afkTimeout,
widgetEnabled = widgetEnabled,
Expand Down Expand Up @@ -133,7 +130,58 @@ public data class GuildData(
premiumProgressBarEnabled = premiumProgressBarEnabled
)
}

public fun from(entity: DiscordPartialGuild): GuildData = with(entity) {
return GuildData(
id = id,
name = name.value!!,
icon = icon?.value,
iconHash = iconHash,
splash = splash,
discoverySplash = discoverySplash,
//owner = owner,
ownerId = ownerId.value!!,
permissions = permissions,
afkChannelId = afkChannelId.value,
afkTimeout = afkTimeout.value!!,
widgetEnabled = widgetEnabled,
widgetChannelId = widgetChannelId,
verificationLevel = verificationLevel.value!!,
defaultMessageNotifications = defaultMessageNotifications.value!!,
explicitContentFilter = explicitContentFilter.value!!,
roles = roles.orEmpty().map { it.id },
emojis = emojis.orEmpty().map { it.id!! },
features = features.orEmpty(),
mfaLevel = mfaLevel.value!!,
applicationId = applicationId.value,
systemChannelId = systemChannelId.value,
systemChannelFlags = systemChannelFlags.value!!,
rulesChannelId = rulesChannelId.value,
joinedAt = joinedAt,
large = large,
memberCount = memberCount,
channels = channels.mapList { it.id },
maxPresences = maxPresences,
maxMembers = maxMembers,
vanityUrlCode = vanityUrlCode?.value,
description = description?.value,
banner = banner?.value,
premiumTier = premiumTier.value!!,
premiumSubscriptionCount = premiumSubscriptionCount,
preferredLocale = preferredLocale.value!!,
publicUpdatesChannelId = publicUpdatesChannelId.value,
maxVideoChannelUsers = maxVideoChannelUsers,
approximateMemberCount = approximateMemberCount,
approximatePresenceCount = approximatePresenceCount,
welcomeScreen = welcomeScreen.map { WelcomeScreenData.from(it) },
nsfwLevel = nsfwLevel.value!!,
threads = threads.mapList { it.toData() },
stageInstances = stageInstances.mapList { StageInstanceData.from(it) },
stickers = stickers.mapList { StickerData.from(it) },
guildScheduledEvents = guildScheduledEvents.mapList { GuildScheduledEventData.from(it) },
premiumProgressBarEnabled = premiumProgressBarEnabled.value!!
)
}
}
}

public fun DiscordGuild.toData(): GuildData = GuildData.from(this)
118 changes: 91 additions & 27 deletions core/src/main/kotlin/cache/data/PartialGuildData.kt
Original file line number Diff line number Diff line change
@@ -1,51 +1,115 @@
package dev.kord.core.cache.data

import dev.kord.common.entity.*
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.mapList
import dev.kord.common.entity.optional.*
import dev.kord.common.serialization.DurationInSeconds
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
public class PartialGuildData(
public val id: Snowflake,
public val name: String,
public val icon: String? = null,
public val name: Optional<String> = Optional.Missing(),
public val icon: Optional<String>? = Optional.Missing(),
@SerialName("icon_hash") public val iconHash: Optional<String?> = Optional.Missing(),
public val splash: Optional<String?> = Optional.Missing(),
@SerialName("discovery_splash") public val discoverySplash: Optional<String?> = Optional.Missing(),
public val owner: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("owner_id") public val ownerId: OptionalSnowflake,
public val permissions: Optional<Permissions> = Optional.Missing(),
public val features: List<GuildFeature>,
public val welcomeScreen: Optional<WelcomeScreenData> = Optional.Missing(),
@SerialName("vanity_url_code") public val vanityUrlCode: Optional<String?> = Optional.Missing(),
public val description: Optional<String?> = Optional.Missing(),
public val banner: Optional<String?> = Optional.Missing(),
public val splash: Optional<String?> = Optional.Missing(),
@SerialName("afk_channel_id") public val afkChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("afk_timeout") public val afkTimeout: Optional<DurationInSeconds> = Optional.Missing(),
@SerialName("widget_enabled") public val widgetEnabled: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("widget_channel_id") public val widgetChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("verification_level") public val verificationLevel: Optional<VerificationLevel> = Optional.Missing(),
@SerialName("default_message_notifications") public val defaultMessageNotifications: Optional<DefaultMessageNotificationLevel> = Optional.Missing(),
@SerialName("explicit_content_filter") public val explicitContentFilter: Optional<ExplicitContentFilter> = Optional.Missing(),
public val roles: List<Snowflake> = emptyList(),
public val emojis: List<Snowflake> = emptyList(),
public val features: List<GuildFeature> = emptyList(),
@SerialName("mfa_level") public val mfaLevel: Optional<MFALevel> = Optional.Missing(),
@SerialName("application_id") public val applicationId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("system_channel_id") public val systemChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("system_channel_flags") public val systemChannelFlags: Optional<SystemChannelFlags> = Optional.Missing(),
@SerialName("rules_channel_id") public val rulesChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("joined_at") public val joinedAt: Optional<Instant> = Optional.Missing(),
public val large: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("member_count") public val memberCount: OptionalInt = OptionalInt.Missing,
@SerialName("voice_states") public val voiceStates: Optional<List<DiscordVoiceState>> = Optional.Missing(),
public val members: Optional<List<Snowflake>> = Optional.Missing(),
public val channels: Optional<List<Snowflake>> = Optional.Missing(),
public val threads: Optional<List<ChannelData>> = Optional.Missing(),
public val presences: Optional<List<DiscordPresenceUpdate>> = Optional.Missing(),
@SerialName("max_presences") public val maxPresences: OptionalInt? = OptionalInt.Missing,
@SerialName("max_members") public val maxMembers: OptionalInt = OptionalInt.Missing,
@SerialName("vanity_url_code") public val vanityUrlCode: Optional<String>? = Optional.Missing(),
public val description: Optional<String>? = Optional.Missing(),
public val banner: Optional<String>? = Optional.Missing(),
@SerialName("premium_tier") public val premiumTier: Optional<PremiumTier> = Optional.Missing(),
@SerialName("premium_subscription_count") public val premiumSubscriptionCount: OptionalInt = OptionalInt.Missing,
@SerialName("preferred_locale") public val preferredLocale: Optional<String> = Optional.Missing(),
@SerialName("public_updates_channel_id") public val publicUpdatesChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("max_video_channel_users") public val maxVideoChannelUsers: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_member_count") public val approximateMemberCount: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_presence_count") public val approximatePresenceCount: OptionalInt = OptionalInt.Missing,
@SerialName("welcome_screen") public val welcomeScreen: Optional<WelcomeScreenData> = Optional.Missing(),
@SerialName("nsfw_level") public val nsfwLevel: Optional<NsfwLevel> = Optional.Missing(),
@SerialName("verification_level")
public val verificationLevel: Optional<VerificationLevel> = Optional.Missing(),
@SerialName("stage_instances")
public val stageInstances: Optional<List<StageInstanceData>> = Optional.Missing(),
public val stickers: Optional<List<StickerData>> = Optional.Missing(),
@SerialName("guild_scheduled_events")
public val guildScheduledEvents: Optional<List<GuildScheduledEventData>> = Optional.Missing(),
@SerialName("premium_progress_bar_enabled")
public val premiumProgressBarEnabled: OptionalBoolean = OptionalBoolean.Missing

) {
public companion object {
public fun from(partialGuild: DiscordPartialGuild): PartialGuildData = with(partialGuild) {
PartialGuildData(
id,
name,
icon,
owner,
permissions,
features,
welcomeScreen.map { WelcomeScreenData.from(it) },
vanityUrlCode,
description,
banner,
splash,
nsfwLevel,
verificationLevel,
id = id,
name = name,
icon = icon,
iconHash = iconHash,
splash = splash,
discoverySplash = discoverySplash,
//owner = owner,
ownerId = ownerId,
permissions = permissions,
afkChannelId = afkChannelId,
afkTimeout = afkTimeout,
widgetEnabled = widgetEnabled,
widgetChannelId = widgetChannelId,
verificationLevel = verificationLevel,
defaultMessageNotifications = defaultMessageNotifications,
explicitContentFilter = explicitContentFilter,
roles = roles.map { it.id },
emojis = emojis.map { it.id!! },
features = features,
mfaLevel = mfaLevel,
applicationId = applicationId,
systemChannelId = systemChannelId,
systemChannelFlags = systemChannelFlags,
rulesChannelId = rulesChannelId,
joinedAt = joinedAt,
large = large,
memberCount = memberCount,
channels = channels.mapList { it.id },
maxPresences = maxPresences,
maxMembers = maxMembers,
vanityUrlCode = vanityUrlCode,
description = description,
banner = banner,
premiumTier = premiumTier,
premiumSubscriptionCount = premiumSubscriptionCount,
preferredLocale = preferredLocale,
publicUpdatesChannelId = publicUpdatesChannelId,
maxVideoChannelUsers = maxVideoChannelUsers,
approximateMemberCount = approximateMemberCount,
approximatePresenceCount = approximatePresenceCount,
welcomeScreen = welcomeScreen.map { WelcomeScreenData.from(it) },
nsfwLevel = nsfwLevel,
threads = threads.mapList { it.toData() },
stageInstances = stageInstances.mapList { StageInstanceData.from(it) },
stickers = stickers.mapList { StickerData.from(it) },
guildScheduledEvents = guildScheduledEvents.mapList { GuildScheduledEventData.from(it) },
Expand Down
12 changes: 0 additions & 12 deletions core/src/main/kotlin/entity/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,6 @@ public class Guild(
*/
public val defaultMessageNotificationLevel: DefaultMessageNotificationLevel get() = data.defaultMessageNotifications


/**
* The voice region id for the guild.
*/
@Suppress("DEPRECATION")
@Deprecated(
"The region field has been moved to Channel#rtcRegion in Discord API v9",
ReplaceWith("Channel#rtcRegion")
)
public val regionId: String
get() = data.region

/**
* The id of the channel in which a discoverable server's rules should be found
**/
Expand Down
Loading