Skip to content

Commit

Permalink
Model fixes, credits to Hope (#518)
Browse files Browse the repository at this point in the history
* add missing fields to PartialGuild

* cleanup warnings and update gradle

* make PartialGuild properties optional

* Fix invite endpoints

* paginate users in scheduled events

* implement suppliers

* add missing guild fields

* Fix wrong json mapping

* fix wrong cache queryings

* apply feedback

* Fix live guild tests

* remove `users` deprecations

* match parameter names

* provide member suppliers and fix endpoint return types

* Apply suggestion

Co-authored-by: Lukellmann <[email protected]>

* implement missing supplier functions

* make it compile

* getInvite

* nullable limits in suppliers

* rename functions

* docs typo

Co-authored-by: HopeBaron <[email protected]>
Co-authored-by: Hope <[email protected]>
  • Loading branch information
3 people authored Feb 3, 2022
1 parent 6c2e7a0 commit d584522
Show file tree
Hide file tree
Showing 30 changed files with 616 additions and 218 deletions.
8 changes: 4 additions & 4 deletions common/src/main/kotlin/entity/AuditLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ public data class AuditLogChange<T>(

@Suppress("UNCHECKED_CAST")
override fun serialize(encoder: Encoder, value: AuditLogChange<T>) {
val value = value as AuditLogChange<Unit>
val logChange = value as AuditLogChange<Unit>
encoder.encodeStructure(descriptor) {
encodeSerializableElement(descriptor, 0, value.key.serializer, value.new as Unit)
encodeSerializableElement(descriptor, 0, value.key.serializer, value.old as Unit)
encodeSerializableElement(descriptor, 0, AuditLogChangeKey.serializer(Unit.serializer()), value.key)
encodeSerializableElement(descriptor, 0, logChange.key.serializer, logChange.new as Unit)
encodeSerializableElement(descriptor, 0, logChange.key.serializer, logChange.old as Unit)
encodeSerializableElement(descriptor, 0, AuditLogChangeKey.serializer(Unit.serializer()), logChange.key)
}
}
}
Expand Down
180 changes: 77 additions & 103 deletions common/src/main/kotlin/entity/DiscordGuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,82 +81,61 @@ public data class DiscordGuild(
val id: Snowflake,
val name: String,
val icon: String?,
@SerialName("icon_hash")
val iconHash: Optional<String?> = Optional.Missing(),
@SerialName("icon_hash") val iconHash: Optional<String?> = Optional.Missing(),
val splash: Optional<String?> = Optional.Missing(),
@SerialName("discovery_splash")
val discoverySplash: Optional<String?> = Optional.Missing(),
@SerialName("discovery_splash") val discoverySplash: Optional<String?> = Optional.Missing(),
val owner: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("owner_id")
val ownerId: Snowflake,
@SerialName("owner_id") val ownerId: Snowflake,
val permissions: Optional<Permissions> = Optional.Missing(),
@Deprecated("The region field has been moved to Channel#rtcRegion in Discord API v9", ReplaceWith("DiscordChannel#rtcRegion"))
val region: String,
@SerialName("afk_channel_id")
val afkChannelId: Snowflake?,
@SerialName("afk_timeout")
val afkTimeout: Int,
@SerialName("widget_enabled")
val widgetEnabled: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("widget_channel_id")
val widgetChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("verification_level")
val verificationLevel: VerificationLevel,
@SerialName("default_message_notifications")
val defaultMessageNotifications: DefaultMessageNotificationLevel,
@SerialName("explicit_content_filter")
val explicitContentFilter: ExplicitContentFilter,
@Deprecated(
"The region field has been moved to Channel#rtcRegion in Discord API v9",
ReplaceWith("DiscordChannel#rtcRegion")
) val region: String,
@SerialName("afk_channel_id") val afkChannelId: Snowflake?,
@SerialName("afk_timeout") val afkTimeout: Int,
@SerialName("widget_enabled") val widgetEnabled: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("widget_channel_id") val widgetChannelId: OptionalSnowflake? = OptionalSnowflake.Missing,
@SerialName("verification_level") val verificationLevel: VerificationLevel,
@SerialName("default_message_notifications") val defaultMessageNotifications: DefaultMessageNotificationLevel,
@SerialName("explicit_content_filter") val explicitContentFilter: ExplicitContentFilter,
val roles: List<DiscordRole>,
val emojis: List<DiscordEmoji>,
val features: List<GuildFeature>,
@SerialName("mfa_level")
val mfaLevel: MFALevel,
@SerialName("application_id")
val applicationId: Snowflake?,
@SerialName("system_channel_id")
val systemChannelId: Snowflake?,
@SerialName("system_channel_flags")
val systemChannelFlags: SystemChannelFlags,
@SerialName("rules_channel_id")
val rulesChannelId: Snowflake?,
@SerialName("joined_at")
val joinedAt: Optional<String> = Optional.Missing(),
@SerialName("mfa_level") val mfaLevel: MFALevel,
@SerialName("application_id") val applicationId: Snowflake?,
@SerialName("system_channel_id") val systemChannelId: Snowflake?,
@SerialName("system_channel_flags") val systemChannelFlags: SystemChannelFlags,
@SerialName("rules_channel_id") val rulesChannelId: Snowflake?,
@SerialName("joined_at") val joinedAt: Optional<String> = Optional.Missing(),
val large: OptionalBoolean = OptionalBoolean.Missing,
val unavailable: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("member_count")
val memberCount: OptionalInt = OptionalInt.Missing,
@SerialName("voice_states")
val voiceStates: Optional<List<DiscordVoiceState>> = Optional.Missing(),
@SerialName("member_count") val memberCount: OptionalInt = OptionalInt.Missing,
@SerialName("voice_states") val voiceStates: Optional<List<DiscordVoiceState>> = Optional.Missing(),
val members: Optional<List<DiscordGuildMember>> = Optional.Missing(),
val channels: Optional<List<DiscordChannel>> = Optional.Missing(),
val threads: Optional<List<DiscordChannel>> = Optional.Missing(),
val presences: Optional<List<DiscordPresenceUpdate>> = Optional.Missing(),
@SerialName("max_presences")
val maxPresences: OptionalInt? = OptionalInt.Missing,
@SerialName("max_members")
val maxMembers: OptionalInt = OptionalInt.Missing,
@SerialName("vanity_url_code")
val vanityUrlCode: String?,
@SerialName("max_presences") val maxPresences: OptionalInt? = OptionalInt.Missing,
@SerialName("max_members") val maxMembers: OptionalInt = OptionalInt.Missing,
@SerialName("vanity_url_code") val vanityUrlCode: String?,
val description: String?,
val banner: String?,
@SerialName("premium_tier")
val premiumTier: PremiumTier,
@SerialName("premium_subscription_count")
val premiumSubscriptionCount: OptionalInt = OptionalInt.Missing,
@SerialName("preferred_locale")
val preferredLocale: String,
@SerialName("public_updates_channel_id")
val publicUpdatesChannelId: Snowflake?,
@SerialName("max_video_channel_users")
val maxVideoChannelUsers: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_member_count")
val approximateMemberCount: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_presence_count")
val approximatePresenceCount: OptionalInt = OptionalInt.Missing,
@SerialName("welcome_screen")
val welcomeScreen: Optional<DiscordWelcomeScreen> = Optional.Missing(),
@SerialName("nsfw_level")
val nsfwLevel: NsfwLevel
@SerialName("premium_tier") val premiumTier: PremiumTier,
@SerialName("premium_subscription_count") val premiumSubscriptionCount: OptionalInt = OptionalInt.Missing,
@SerialName("preferred_locale") val preferredLocale: String,
@SerialName("public_updates_channel_id") val publicUpdatesChannelId: Snowflake?,
@SerialName("max_video_channel_users") val maxVideoChannelUsers: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_member_count") val approximateMemberCount: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_presence_count") val approximatePresenceCount: OptionalInt = OptionalInt.Missing,
@SerialName("welcome_screen") val welcomeScreen: Optional<DiscordWelcomeScreen> = Optional.Missing(),
@SerialName("nsfw_level") val nsfwLevel: NsfwLevel,
@SerialName("stage_instances")
val stageInstances: Optional<List<DiscordStageInstance>> = Optional.Missing(),
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("guild_scheduled_events")
val guildScheduledEvents: Optional<List<DiscordGuildScheduledEvent>> = Optional.Missing(),
@SerialName("premium_progress_bar_enabled")
val premiumProgressBarEnabled: Boolean
)

/**
Expand All @@ -178,9 +157,23 @@ public data class DiscordPartialGuild(
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("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(),
@SerialName("stage_instances")
val stageInstances: Optional<List<DiscordStageInstance>> = Optional.Missing(),
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("guild_scheduled_events")
val guildScheduledEvents: Optional<List<DiscordGuildScheduledEvent>> = Optional.Missing(),
@SerialName("premium_progress_bar_enabled")
val premiumProgressBarEnabled: OptionalBoolean = OptionalBoolean.Missing

)

/**
* A representation of a [Discord Guild Feature](https://discord.com/developers/docs/resources/guild#guild-object-guild-features).
Expand Down Expand Up @@ -330,15 +323,13 @@ public enum class SystemChannelFlag(public val code: Int) {

@Serializable
public data class DiscordGuildBan(
@SerialName("guild_id")
val guildId: Snowflake,
@SerialName("guild_id") val guildId: Snowflake,
val user: DiscordUser,
)

@Serializable
public data class DiscordGuildIntegrations(
@SerialName("guild_id")
val guildId: Snowflake,
@SerialName("guild_id") val guildId: Snowflake,
)

@Serializable
Expand All @@ -359,17 +350,14 @@ public data class DiscordIntegrationAccount(
@Serializable
public data class DiscordVoiceServerUpdateData(
val token: String,
@SerialName("guild_id")
val guildId: Snowflake,
@SerialName("guild_id") val guildId: Snowflake,
val endpoint: String?,
)

@Serializable
public data class DiscordWebhooksUpdateData(
@SerialName("guild_id")
val guildId: Snowflake,
@SerialName("channel_id")
val channelId: Snowflake,
@SerialName("guild_id") val guildId: Snowflake,
@SerialName("channel_id") val channelId: Snowflake,
)

/**
Expand All @@ -391,29 +379,19 @@ public data class DiscordWebhooksUpdateData(
*/
@Serializable
public data class DiscordVoiceState(
@SerialName("guild_id")
val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("channel_id")
val channelId: Snowflake?,
@SerialName("user_id")
val userId: Snowflake,
@SerialName("guild_member")
val member: Optional<DiscordGuildMember> = Optional.Missing(),
@SerialName("session_id")
val sessionId: String,
@SerialName("guild_id") val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("channel_id") val channelId: Snowflake?,
@SerialName("user_id") val userId: Snowflake,
@SerialName("guild_member") val member: Optional<DiscordGuildMember> = Optional.Missing(),
@SerialName("session_id") val sessionId: String,
val deaf: Boolean,
val mute: Boolean,
@SerialName("self_deaf")
val selfDeaf: Boolean,
@SerialName("self_mute")
val selfMute: Boolean,
@SerialName("self_video")
val selfVideo: Boolean,
@SerialName("self_stream")
val selfStream: OptionalBoolean = OptionalBoolean.Missing,
@SerialName("self_deaf") val selfDeaf: Boolean,
@SerialName("self_mute") val selfMute: Boolean,
@SerialName("self_video") val selfVideo: Boolean,
@SerialName("self_stream") val selfStream: OptionalBoolean = OptionalBoolean.Missing,
val suppress: Boolean,
@SerialName("request_to_speak_timestamp")
val requestToSpeakTimestamp: String?
@SerialName("request_to_speak_timestamp") val requestToSpeakTimestamp: String?
)

/**
Expand Down Expand Up @@ -618,18 +596,14 @@ public sealed class VerificationLevel(public val value: Int) {

@Serializable
public data class DiscordWelcomeScreenChannel(
@SerialName("channel_id")
val channelId: Snowflake,
@SerialName("channel_id") val channelId: Snowflake,
val description: String,
@SerialName("emoji_id")
val emojiId: Snowflake?,
@SerialName("emoji_name")
val emojiName: String?
@SerialName("emoji_id") val emojiId: Snowflake?,
@SerialName("emoji_name") val emojiName: String?
)

@Serializable
public data class DiscordWelcomeScreen(
val description: String?,
@SerialName("welcome_channels")
val welcomeChannels: List<DiscordWelcomeScreenChannel>
@SerialName("welcome_channels") val welcomeChannels: List<DiscordWelcomeScreenChannel>,
)
2 changes: 1 addition & 1 deletion common/src/main/kotlin/entity/DiscordInvite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
public data class DiscordInvite(
val code: String,
val guild: Optional<DiscordPartialGuild> = Optional.Missing(),
val channel: DiscordChannel,
val channel: DiscordChannel?,
val inviter: Optional<DiscordUser> = Optional.Missing(),
@SerialName("target_user")
val targetUser: Optional<DiscordUser> = Optional.Missing(),
Expand Down
4 changes: 3 additions & 1 deletion common/src/test/resources/json/guild/guild.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@
"preferred_locale": "en-US",
"rules_channel_id": "441688182833020939",
"public_updates_channel_id": "281283303326089216",
"nsfw_level": 0
"nsfw_level": 0,
"premium_progress_bar_enabled": false

}
32 changes: 27 additions & 5 deletions core/src/main/kotlin/Kord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,39 @@ public class Kord(
strategy.supply(this).getUserOrNull(id)

/**
* Requests to get the [Invite] with [code] through the [EntitySupplyStrategy.rest][rest].
* Requests to get the [Invite] represented by the [code].
*
* The returned [Invite], if found, uses the default strategy used by Kord.
*
* @throws [RequestException] if anything went wrong during the request.
* This is not resolvable through cache and will always use the [rest strategy][EntitySupplyStrategy.rest] instead.
*
* @throws RestRequestException if anything went wrong during the request.
* @throws EntityNotFoundException if the [Invite] wasn't present.
*/
public suspend fun getInvite(
code: String,
withCounts: Boolean,
): Invite? =
EntitySupplyStrategy.rest.supply(this).getInviteOrNull(code, withCounts)
withCounts: Boolean = true,
withExpiration: Boolean = true,
scheduledEventId: Snowflake? = null,
): Invite = with(EntitySupplyStrategy.rest).getInvite(code, withCounts, withExpiration, scheduledEventId)

/**
* Requests to get the [Invite] represented by the [code],
* returns null if the [Invite] isn't present.
*
* The returned [Invite], if found, uses the default strategy used by Kord.
*
* This is not resolvable through cache and will always use the [rest strategy][EntitySupplyStrategy.rest] instead.
*
* @throws RestRequestException if anything went wrong during the request.
*/
public suspend fun getInviteOrNull(
code: String,
withCounts: Boolean = true,
withExpiration: Boolean = true,
scheduledEventId: Snowflake? = null,
): Invite? = with(EntitySupplyStrategy.rest).getInviteOrNull(code, withCounts, withExpiration, scheduledEventId)


public suspend fun getSticker(id: Snowflake): Sticker = defaultSupplier.getSticker(id)

Expand Down
27 changes: 17 additions & 10 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,32 @@ public interface GuildBehavior : KordEntity, Strategizable {
/**
* Requests to get the [Invite] represented by the [code].
*
* This is not resolvable through cache and will always use the [rest strategy][EntitySupplyStrategy.rest] instead.
*
* This property is not resolvable through cache and will always use the [RestClient] instead.
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the [Invite] wasn't present.
* @throws RestRequestException if anything went wrong during the request.
* @throws EntityNotFoundException if the [Invite] wasn't present.
*/
public suspend fun getInvite(code: String, withCounts: Boolean = true): Invite =
kord.with(rest).getInvite(code, withCounts)
public suspend fun getInvite(
code: String,
withCounts: Boolean = true,
withExpiration: Boolean = true,
scheduledEventId: Snowflake? = null,
): Invite = kord.with(rest).getInvite(code, withCounts, withExpiration, scheduledEventId)

/**
* Requests to get the [Invite] represented by the [code],
* returns null if the [Invite] isn't present.
*
* This property is not resolvable through cache and will always use the [RestClient] instead.
* This is not resolvable through cache and will always use the [rest strategy][EntitySupplyStrategy.rest] instead.
*
* @throws [RequestException] if anything went wrong during the request.
* @throws RestRequestException if anything went wrong during the request.
*/
public suspend fun getInviteOrNull(code: String, withCounts: Boolean = true): Invite? =
kord.with(rest).getInviteOrNull(code, withCounts)
public suspend fun getInviteOrNull(
code: String,
withCounts: Boolean = true,
withExpiration: Boolean = true,
scheduledEventId: Snowflake? = null,
): Invite? = kord.with(rest).getInviteOrNull(code, withCounts, withExpiration, scheduledEventId)


/**
Expand Down
Loading

0 comments on commit d584522

Please sign in to comment.