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

Fixes for #513 (Guild Scheduled Events) #514

Merged
merged 7 commits into from
Feb 1, 2022
Merged
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
46 changes: 35 additions & 11 deletions common/src/main/kotlin/entity/DiscordGuildScheduledEvent.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.kord.common.entity

import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.OptionalInt
import kotlinx.datetime.Instant
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
Expand All @@ -23,7 +23,7 @@ import kotlinx.serialization.encoding.Encoder
* @property description the description of the event
* @property scheduledStartTime the [Instant] in which the event will start
* @property scheduledEndTime the [Instant] in which the event wil stop, if any
* @property privacyLevel the [event privacy level][StageInstancePrivacyLevel]
* @property privacyLevel the [event privacy level][GuildScheduledEventPrivacyLevel]
* @property status the [event status][GuildScheduledEventStatus]
* @property entityType the [ScheduledEntityType] of the event
* @property entityId entity id
Expand All @@ -36,32 +36,59 @@ public data class DiscordGuildScheduledEvent(
val id: Snowflake,
@SerialName("guild_id")
val guildId: Snowflake,
@SerialName("channel_id")
val channelId: Snowflake?,
@SerialName("creator_id")
val creatorId: OptionalSnowflake,
val creatorId: Snowflake?,
val name: String,
val description: Optional<String> = Optional.Missing(),
@SerialName("scheduled_start_time")
val scheduledStartTime: Instant,
@SerialName("scheduled_end_time")
val scheduledEndTime: Instant?,
@SerialName("privacy_level")
val privacyLevel: StageInstancePrivacyLevel,
val privacyLevel: GuildScheduledEventPrivacyLevel,
val status: GuildScheduledEventStatus,
@SerialName("entity_type")
val entityType: ScheduledEntityType,
@SerialName("entity_id")
val entityId: Snowflake?,
@SerialName("entity_metadata")
val entityMetadata: GuildScheduledEventEntityMetadata,
val creator: Optional<DiscordUser>,
val entityMetadata: GuildScheduledEventEntityMetadata?,
val creator: Optional<DiscordUser> = Optional.Missing(),
@SerialName("user_count")
val userCount: Int
val userCount: OptionalInt = OptionalInt.Missing,
)

/** Privacy level of a [DiscordGuildScheduledEvent]. */
@Serializable(with = GuildScheduledEventPrivacyLevel.Serializer::class)
public sealed class GuildScheduledEventPrivacyLevel(public val value: Int) {

/** The scheduled event is only accessible to guild members. */
public object GuildOnly : GuildScheduledEventPrivacyLevel(2)

/** An unknown privacy level. */
public class Unknown(value: Int) : GuildScheduledEventPrivacyLevel(value)

internal object Serializer : KSerializer<GuildScheduledEventPrivacyLevel> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("GuildScheduledEventPrivacyLevel", PrimitiveKind.INT)

override fun deserialize(decoder: Decoder): GuildScheduledEventPrivacyLevel {
return when (val value = decoder.decodeInt()) {
2 -> GuildOnly
else -> Unknown(value)
}
}

override fun serialize(encoder: Encoder, value: GuildScheduledEventPrivacyLevel) {
encoder.encodeInt(value.value)
}
}
}

@Serializable(with = ScheduledEntityType.Serializer::class)
public sealed class ScheduledEntityType(public val value: Int) {
public object None : ScheduledEntityType(0)
public object StageInstance : ScheduledEntityType(1)
public object Voice : ScheduledEntityType(2)
public object External : ScheduledEntityType(3)
Expand All @@ -72,7 +99,6 @@ public sealed class ScheduledEntityType(public val value: Int) {

override fun deserialize(decoder: Decoder): ScheduledEntityType {
return when (val value = decoder.decodeInt()) {
0 -> None
1 -> StageInstance
2 -> Voice
3 -> External
Expand Down Expand Up @@ -114,11 +140,9 @@ public sealed class GuildScheduledEventStatus(public val value: Int) {
/**
* Entity metadata for [DiscordGuildScheduledEvent].
*
* @property speakerIds the speakers of the stage channel
* @property location location of the event
*/
@Serializable
public data class GuildScheduledEventEntityMetadata(
val speakerIds: Optional<List<Snowflake>> = Optional.Missing(),
val location: Optional<String> = Optional.Missing()
)
50 changes: 8 additions & 42 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import dev.kord.cache.api.query
import dev.kord.common.annotation.DeprecatedSinceKord
import dev.kord.common.annotation.KordExperimental
import dev.kord.common.entity.DiscordUser
import dev.kord.common.entity.GuildScheduledEventPrivacyLevel
import dev.kord.common.entity.ScheduledEntityType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.StageInstancePrivacyLevel
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.unwrap
import dev.kord.common.exception.RequestException
Expand All @@ -19,13 +19,7 @@ import dev.kord.core.entity.application.GuildApplicationCommand
import dev.kord.core.entity.application.GuildChatInputCommand
import dev.kord.core.entity.application.GuildMessageCommand
import dev.kord.core.entity.application.GuildUserCommand
import dev.kord.core.entity.channel.Category
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.NewsChannel
import dev.kord.core.entity.channel.TextChannel
import dev.kord.core.entity.channel.TopGuildChannel
import dev.kord.core.entity.channel.VoiceChannel
import dev.kord.core.entity.channel.*
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.event.guild.MembersChunkEvent
import dev.kord.core.exception.EntityNotFoundException
Expand All @@ -41,46 +35,18 @@ import dev.kord.rest.Image
import dev.kord.rest.NamedFile
import dev.kord.rest.builder.auditlog.AuditLogGetRequestBuilder
import dev.kord.rest.builder.ban.BanCreateBuilder
import dev.kord.rest.builder.channel.CategoryCreateBuilder
import dev.kord.rest.builder.channel.GuildChannelPositionModifyBuilder
import dev.kord.rest.builder.channel.NewsChannelCreateBuilder
import dev.kord.rest.builder.channel.TextChannelCreateBuilder
import dev.kord.rest.builder.channel.VoiceChannelCreateBuilder
import dev.kord.rest.builder.guild.EmojiCreateBuilder
import dev.kord.rest.builder.guild.GuildModifyBuilder
import dev.kord.rest.builder.guild.GuildWidgetModifyBuilder
import dev.kord.rest.builder.guild.ScheduledEventCreateBuilder
import dev.kord.rest.builder.guild.WelcomeScreenModifyBuilder
import dev.kord.rest.builder.interaction.ApplicationCommandPermissionsBulkModifyBuilder
import dev.kord.rest.builder.interaction.ChatInputCreateBuilder
import dev.kord.rest.builder.interaction.MessageCommandCreateBuilder
import dev.kord.rest.builder.interaction.MultiApplicationCommandBuilder
import dev.kord.rest.builder.interaction.UserCommandCreateBuilder
import dev.kord.rest.builder.channel.*
import dev.kord.rest.builder.guild.*
import dev.kord.rest.builder.interaction.*
import dev.kord.rest.builder.role.RoleCreateBuilder
import dev.kord.rest.builder.role.RolePositionsModifyBuilder
import dev.kord.rest.json.JsonErrorCode
import dev.kord.rest.json.request.CurrentUserNicknameModifyRequest
import dev.kord.rest.json.request.GuildStickerCreateRequest
import dev.kord.rest.json.request.MultipartGuildStickerCreateRequest
import dev.kord.rest.request.RestRequestException
import dev.kord.rest.service.RestClient
import dev.kord.rest.service.createCategory
import dev.kord.rest.service.createNewsChannel
import dev.kord.rest.service.createScheduledEvent
import dev.kord.rest.service.createTextChannel
import dev.kord.rest.service.createVoiceChannel
import dev.kord.rest.service.modifyGuildWelcomeScreen
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.flow.transformWhile
import dev.kord.rest.service.*
import kotlinx.coroutines.flow.*
import kotlinx.datetime.Instant
import java.util.Objects
import kotlin.contracts.InvocationKind
Expand Down Expand Up @@ -1049,7 +1015,7 @@ public suspend inline fun GuildBehavior.bulkEditSlashCommandPermissions(noinline
*/
public suspend fun GuildBehavior.createScheduledEvent(
name: String,
privacyLevel: StageInstancePrivacyLevel,
privacyLevel: GuildScheduledEventPrivacyLevel,
scheduledStartTime: Instant,
entityType: ScheduledEntityType,
builder: ScheduledEventCreateBuilder.() -> Unit
Expand Down
18 changes: 6 additions & 12 deletions core/src/main/kotlin/cache/data/GuildScheduledEventData.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package dev.kord.core.cache.data

import dev.kord.common.entity.DiscordGuildScheduledEvent
import dev.kord.common.entity.GuildScheduledEventEntityMetadata
import dev.kord.common.entity.GuildScheduledEventStatus
import dev.kord.common.entity.ScheduledEntityType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.StageInstancePrivacyLevel
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.map
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
Expand All @@ -17,18 +12,18 @@ public data class GuildScheduledEventData(
val id: Snowflake,
val guildId: Snowflake,
val channelId: Snowflake?,
val creatorId: OptionalSnowflake = OptionalSnowflake.Missing,
val creatorId: Snowflake?,
val name: String,
val description: Optional<String> = Optional.Missing(),
val scheduledStartTime: Instant,
val scheduledEndTime: Instant?,
val privacyLevel: StageInstancePrivacyLevel,
val privacyLevel: GuildScheduledEventPrivacyLevel,
val status: GuildScheduledEventStatus,
val entityId: Snowflake?,
val entityType: ScheduledEntityType,
val entityMetadata: GuildScheduledEventEntityMetadata,
val entityMetadata: GuildScheduledEventEntityMetadata?,
val creator: Optional<UserData> = Optional.Missing(),
val userCount: Int
val userCount: OptionalInt = OptionalInt.Missing,
) {
public companion object {
public fun from(event: DiscordGuildScheduledEvent): GuildScheduledEventData = GuildScheduledEventData(
Expand All @@ -50,4 +45,3 @@ public data class GuildScheduledEventData(
)
}
}

25 changes: 12 additions & 13 deletions core/src/main/kotlin/entity/GuildScheduledEvent.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package dev.kord.core.entity

import dev.kord.common.entity.GuildScheduledEventEntityMetadata
import dev.kord.common.entity.GuildScheduledEventStatus
import dev.kord.common.entity.ScheduledEntityType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.StageInstancePrivacyLevel
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.unwrap
import dev.kord.common.entity.optional.value
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.GuildScheduledEventBehavior
Expand Down Expand Up @@ -45,10 +42,12 @@ public class GuildScheduledEvent(
get() = data.channelId

/**
* The id of the user that created the scheduled event
* The id of the user that created the scheduled event.
*
* This is only available for events created after 2021-10-25.
*/
public val creatorId: Snowflake?
get() = data.creatorId.value
get() = data.creatorId

/**
* The name of this event.
Expand All @@ -75,9 +74,9 @@ public class GuildScheduledEvent(
get() = data.scheduledEndTime

/**
* The [privacy level][StageInstancePrivacyLevel] of this event.
* The [privacy level][GuildScheduledEventPrivacyLevel] of this event.
*/
public val privacyLevel: StageInstancePrivacyLevel
public val privacyLevel: GuildScheduledEventPrivacyLevel
get() = data.privacyLevel

/**
Expand All @@ -98,17 +97,17 @@ public class GuildScheduledEvent(
/**
* The [entity metadata][GuildScheduledEventEntityMetadata] for the scheduled event
*/
public val entityMetadata: GuildScheduledEventEntityMetadata
public val entityMetadata: GuildScheduledEventEntityMetadata?
get() = data.entityMetadata

public val creator: User?
get() = data.creator.unwrap { User(it, kord, supplier) }

/**
* The amount of users subscribed to this event.
* The number of users subscribed to this event.
*/
public val userCount: Int
get() = data.userCount
public val userCount: Int?
get() = data.userCount.value

/**
* Requests the [Guild] this event is on.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package dev.kord.rest.builder.guild

import dev.kord.common.entity.GuildScheduledEventEntityMetadata
import dev.kord.common.entity.GuildScheduledEventPrivacyLevel
import dev.kord.common.entity.ScheduledEntityType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.StageInstancePrivacyLevel
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.GuildScheduledEventCreateRequest
import kotlinx.datetime.Instant

public class ScheduledEventCreateBuilder(
public val name: String,
public val privacyLevel: StageInstancePrivacyLevel,
public val scheduledStartTime: Instant,
public val entityType: ScheduledEntityType,
) : RequestBuilder<GuildScheduledEventCreateRequest> {
public var name: String,
public var privacyLevel: GuildScheduledEventPrivacyLevel,
public var scheduledStartTime: Instant,
public var entityType: ScheduledEntityType,
) : AuditRequestBuilder<GuildScheduledEventCreateRequest> {
override var reason: String? = null

private var _channelId: OptionalSnowflake = OptionalSnowflake.Missing
public var channelId: Snowflake? by ::_channelId.delegate()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import dev.kord.common.entity.*
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.ScheduledEventModifyRequest
import kotlinx.datetime.Instant

public class ScheduledEventModifyBuilder : RequestBuilder<ScheduledEventModifyRequest> {
private var _channelId: OptionalSnowflake = OptionalSnowflake.Missing
public class ScheduledEventModifyBuilder : AuditRequestBuilder<ScheduledEventModifyRequest> {
override var reason: String? = null

private var _channelId: OptionalSnowflake? = OptionalSnowflake.Missing
public var channelId: Snowflake? by ::_channelId.delegate()

private var _name: Optional<String> = Optional.Missing()
public var name: String? by ::_name.delegate()

private var _privacyLevel: Optional<StageInstancePrivacyLevel> = Optional.Missing()
public var privacyLevel: StageInstancePrivacyLevel? by ::_privacyLevel.delegate()
private var _privacyLevel: Optional<GuildScheduledEventPrivacyLevel> = Optional.Missing()
public var privacyLevel: GuildScheduledEventPrivacyLevel? by ::_privacyLevel.delegate()

private var _scheduledStartTime: Optional<Instant> = Optional.Missing()
public var scheduledStartTime: Instant? by ::_scheduledStartTime.delegate()
Expand Down
15 changes: 0 additions & 15 deletions rest/src/main/kotlin/json/request/GuildRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,6 @@ public data class GuildWelcomeScreenModifyRequest(
val description: Optional<String> = Optional.Missing()
)

@Serializable
public data class GuildScheduledEventCreateRequest(
val channelId: OptionalSnowflake = OptionalSnowflake.Missing,
val entityMetadata: Optional<GuildScheduledEventEntityMetadata> = Optional.Missing(),
val name: String,
@SerialName("privacy_level")
val privacyLevel: StageInstancePrivacyLevel,
@SerialName("scheduled_start_time")
val scheduledStartTime: Instant,
@SerialName("scheduled_end_time")
val scheduledEndTime: Optional<Instant>,
val description: Optional<String> = Optional.Missing(),
@SerialName("entity_type")
val entityType: ScheduledEntityType
)

@Serializable
public data class GuildScheduledEventUsersResponse(val users: List<DiscordOptionallyMemberUser>)
Loading