From d45b93fea8f41a8393e3b04e864d2967a0a2b2c7 Mon Sep 17 00:00:00 2001 From: schlaubi Date: Fri, 14 May 2021 09:19:49 +0200 Subject: [PATCH 1/9] Add low-level implementation of stage instances --- .../kotlin/entity/DiscordStageInstance.kt | 23 +++++++++++++++ common/src/main/kotlin/entity/Interactions.kt | 2 ++ .../kotlin/json/request/ChannelRequests.kt | 1 + .../json/request/StageInstanceRequests.kt | 15 ++++++++++ rest/src/main/kotlin/route/Route.kt | 12 ++++++++ rest/src/main/kotlin/service/RestClient.kt | 1 + .../kotlin/service/StageInstanceService.kt | 28 +++++++++++++++++++ 7 files changed, 82 insertions(+) create mode 100644 common/src/main/kotlin/entity/DiscordStageInstance.kt create mode 100644 rest/src/main/kotlin/json/request/StageInstanceRequests.kt create mode 100644 rest/src/main/kotlin/service/StageInstanceService.kt diff --git a/common/src/main/kotlin/entity/DiscordStageInstance.kt b/common/src/main/kotlin/entity/DiscordStageInstance.kt new file mode 100644 index 000000000000..826eb9a4f79c --- /dev/null +++ b/common/src/main/kotlin/entity/DiscordStageInstance.kt @@ -0,0 +1,23 @@ +package dev.kord.common.entity + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + + +/** + * A _Stage Instance_ holds information about a live stage. + * + * @property id The id of this Stage instance + * @property guildId The guild id of the associated Stage channel + * @property channelId The id of the associated Stage channel + * @property topic The topic of the Stage instance (1-120 characters) + */ +@Serializable +data class DiscordStageInstance( + val id: Snowflake, + @SerialName("guild_id") + val guildId: Snowflake, + @SerialName("channel_id") + val channelId: Snowflake, + val topic: String +) diff --git a/common/src/main/kotlin/entity/Interactions.kt b/common/src/main/kotlin/entity/Interactions.kt index 80f4130d3714..77681c94c702 100644 --- a/common/src/main/kotlin/entity/Interactions.kt +++ b/common/src/main/kotlin/entity/Interactions.kt @@ -73,6 +73,7 @@ sealed class ApplicationCommandOptionType(val type: Int) { object User : ApplicationCommandOptionType(6) object Channel : ApplicationCommandOptionType(7) object Role : ApplicationCommandOptionType(8) + object Mentionable : ApplicationCommandOptionType(9) class Unknown(type: Int) : ApplicationCommandOptionType(type) companion object; @@ -92,6 +93,7 @@ sealed class ApplicationCommandOptionType(val type: Int) { 6 -> User 7 -> Channel 8 -> Role + 9 -> Mentionable else -> Unknown(type) } } diff --git a/rest/src/main/kotlin/json/request/ChannelRequests.kt b/rest/src/main/kotlin/json/request/ChannelRequests.kt index 421a6a5f1751..bc3abe81114b 100644 --- a/rest/src/main/kotlin/json/request/ChannelRequests.kt +++ b/rest/src/main/kotlin/json/request/ChannelRequests.kt @@ -3,6 +3,7 @@ package dev.kord.rest.json.request import dev.kord.common.entity.Overwrite import dev.kord.common.entity.OverwriteType import dev.kord.common.entity.Permissions +import dev.kord.common.entity.Snowflake import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.OptionalBoolean import dev.kord.common.entity.optional.OptionalInt diff --git a/rest/src/main/kotlin/json/request/StageInstanceRequests.kt b/rest/src/main/kotlin/json/request/StageInstanceRequests.kt new file mode 100644 index 000000000000..bab42ad0592a --- /dev/null +++ b/rest/src/main/kotlin/json/request/StageInstanceRequests.kt @@ -0,0 +1,15 @@ +package dev.kord.rest.json.request + +import dev.kord.common.entity.Snowflake +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class StageInstanceCreateRequest( + @SerialName("channel_id") + val channelId: Snowflake, + val topic: String +) + +@Serializable +data class StageInstanceUpdateRequest(val topic: String) diff --git a/rest/src/main/kotlin/route/Route.kt b/rest/src/main/kotlin/route/Route.kt index 38fb000d9e3b..bf51c0d36533 100644 --- a/rest/src/main/kotlin/route/Route.kt +++ b/rest/src/main/kotlin/route/Route.kt @@ -646,6 +646,18 @@ sealed class Route( object OthersVoiceStatePatch: Route(HttpMethod.Patch, "/guilds/${GuildId}/voice-states/${UserId}", NoStrategy) + object StageInstanceGet : + Route(HttpMethod.Get, "/stage-instances/$ChannelId", DiscordStageInstance.serializer()) + + object StageInstancePost : + Route(HttpMethod.Post, "/stage-instances", DiscordStageInstance.serializer()) + + object StageInstancePatch : + Route(HttpMethod.Patch, "/stage-instances/$ChannelId", DiscordStageInstance.serializer()) + + object StageInstanceDelete : + Route(HttpMethod.Delete, "/stage-instances/$ChannelId", NoStrategy) + companion object { val baseUrl = "https://discord.com/api/$restVersion" } diff --git a/rest/src/main/kotlin/service/RestClient.kt b/rest/src/main/kotlin/service/RestClient.kt index 8828a168a69e..f3a68e551dbc 100644 --- a/rest/src/main/kotlin/service/RestClient.kt +++ b/rest/src/main/kotlin/service/RestClient.kt @@ -22,6 +22,7 @@ class RestClient(requestHandler: RequestHandler) : RestService(requestHandler) { val application: ApplicationService = ApplicationService(requestHandler) val template: TemplateService = TemplateService(requestHandler) val interaction: InteractionService = InteractionService(requestHandler) + val stageInstance: StageInstanceService = StageInstanceService(requestHandler) /** * Sends a request to the given [route]. This function exposes a direct call to the Discord api and allows diff --git a/rest/src/main/kotlin/service/StageInstanceService.kt b/rest/src/main/kotlin/service/StageInstanceService.kt new file mode 100644 index 000000000000..c714bbae4b11 --- /dev/null +++ b/rest/src/main/kotlin/service/StageInstanceService.kt @@ -0,0 +1,28 @@ +package dev.kord.rest.service + +import dev.kord.common.entity.DiscordStageInstance +import dev.kord.common.entity.Snowflake +import dev.kord.rest.json.request.StageInstanceCreateRequest +import dev.kord.rest.json.request.StageInstanceUpdateRequest +import dev.kord.rest.request.RequestHandler +import dev.kord.rest.route.Route + +class StageInstanceService(requestHandler: RequestHandler) : RestService(requestHandler) { + suspend fun getStageInstance(channelId: Snowflake): DiscordStageInstance = call(Route.StageInstanceGet) { + keys[Route.ChannelId] = channelId + } + + suspend fun createStageInstance(request: StageInstanceCreateRequest): DiscordStageInstance = + call(Route.StageInstancePost) { + body(StageInstanceCreateRequest.serializer(), request) + } + + suspend fun updateStageInstance(request: StageInstanceUpdateRequest): DiscordStageInstance = + call(Route.StageInstancePost) { + body(StageInstanceUpdateRequest.serializer(), request) + } + + suspend fun deleteStageInstance(channelId: Snowflake): Unit = call(Route.StageInstanceDelete) { + keys[Route.ChannelId] = channelId + } +} From f983fcc36ee750fc47f059b5fb9b60fdc764a020 Mon Sep 17 00:00:00 2001 From: schlaubi Date: Fri, 14 May 2021 09:21:58 +0200 Subject: [PATCH 2/9] Add helper functions --- .../src/main/kotlin/service/StageInstanceService.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/rest/src/main/kotlin/service/StageInstanceService.kt b/rest/src/main/kotlin/service/StageInstanceService.kt index c714bbae4b11..2bb66354cbff 100644 --- a/rest/src/main/kotlin/service/StageInstanceService.kt +++ b/rest/src/main/kotlin/service/StageInstanceService.kt @@ -17,8 +17,10 @@ class StageInstanceService(requestHandler: RequestHandler) : RestService(request body(StageInstanceCreateRequest.serializer(), request) } - suspend fun updateStageInstance(request: StageInstanceUpdateRequest): DiscordStageInstance = + suspend fun updateStageInstance(channelId: Snowflake, request: StageInstanceUpdateRequest): DiscordStageInstance = call(Route.StageInstancePost) { + keys[Route.ChannelId] = channelId + body(StageInstanceUpdateRequest.serializer(), request) } @@ -26,3 +28,12 @@ class StageInstanceService(requestHandler: RequestHandler) : RestService(request keys[Route.ChannelId] = channelId } } + +suspend fun StageInstanceService.createStageInstance(channelId: Snowflake, topic: String) = createStageInstance( + StageInstanceCreateRequest(channelId, topic) +) + +suspend fun StageInstanceService.updateStageInstance(channelId: Snowflake, topic: String) = updateStageInstance( + channelId, + StageInstanceUpdateRequest(topic) +) From ca912bd3d41ad43b3f4d7c577c42d338b284a56a Mon Sep 17 00:00:00 2001 From: schlaubi Date: Fri, 14 May 2021 09:38:34 +0200 Subject: [PATCH 3/9] Add core entities and api representations --- core/src/main/kotlin/Unsafe.kt | 4 ++ .../kotlin/behavior/StageInstanceBehavior.kt | 39 +++++++++++++++++++ .../behavior/channel/StageChannelBehavior.kt | 10 +++++ .../kotlin/cache/data/StageInstanceData.kt | 19 +++++++++ core/src/main/kotlin/entity/StageInstance.kt | 20 ++++++++++ 5 files changed, 92 insertions(+) create mode 100644 core/src/main/kotlin/behavior/StageInstanceBehavior.kt create mode 100644 core/src/main/kotlin/cache/data/StageInstanceData.kt create mode 100644 core/src/main/kotlin/entity/StageInstance.kt diff --git a/core/src/main/kotlin/Unsafe.kt b/core/src/main/kotlin/Unsafe.kt index b09c1e206ba3..6483ed9ecd42 100644 --- a/core/src/main/kotlin/Unsafe.kt +++ b/core/src/main/kotlin/Unsafe.kt @@ -68,6 +68,10 @@ class Unsafe(private val kord: Kord) { fun webhook(id: Snowflake): WebhookBehavior = WebhookBehavior(id, kord) + fun stageInstance(id: Snowflake, channelId: Snowflake): StageInstanceBehavior = StageInstanceBehavior( + id, channelId, kord, kord.defaultSupplier + ) + override fun toString(): String { return "Unsafe" } diff --git a/core/src/main/kotlin/behavior/StageInstanceBehavior.kt b/core/src/main/kotlin/behavior/StageInstanceBehavior.kt new file mode 100644 index 000000000000..eef1ff96a37e --- /dev/null +++ b/core/src/main/kotlin/behavior/StageInstanceBehavior.kt @@ -0,0 +1,39 @@ +package dev.kord.core.behavior + +import dev.kord.common.entity.Snowflake +import dev.kord.core.Kord +import dev.kord.core.cache.data.StageInstanceData +import dev.kord.core.entity.KordEntity +import dev.kord.core.entity.StageInstance +import dev.kord.core.entity.Strategizable +import dev.kord.core.supplier.EntitySupplier +import dev.kord.core.supplier.EntitySupplyStrategy +import dev.kord.rest.json.request.StageInstanceUpdateRequest + +interface StageInstanceBehavior : KordEntity, Strategizable { + val channelId: Snowflake + + suspend fun delete(): Unit = kord.rest.stageInstance.deleteStageInstance(channelId) + + suspend fun update(topic: String): StageInstance { + val instance = kord.rest.stageInstance.updateStageInstance(channelId, StageInstanceUpdateRequest(topic)) + val data = StageInstanceData.from(instance) + + return StageInstance(data, kord, supplier) + } + + override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior = + StageInstanceBehavior(id, channelId, kord, strategy.supply(kord)) +} + +internal fun StageInstanceBehavior(id: Snowflake, channelId: Snowflake, kord: Kord, supplier: EntitySupplier) = + object : StageInstanceBehavior { + override val channelId: Snowflake + get() = channelId + override val kord: Kord + get() = kord + override val id: Snowflake + get() = id + override val supplier: EntitySupplier + get() = supplier + } diff --git a/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt b/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt index 2d93dcc7bd79..53aa460cc34c 100644 --- a/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt +++ b/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt @@ -3,6 +3,8 @@ package dev.kord.core.behavior.channel import dev.kord.common.entity.Snowflake import dev.kord.core.Kord import dev.kord.core.cache.data.ChannelData +import dev.kord.core.cache.data.StageInstanceData +import dev.kord.core.entity.StageInstance import dev.kord.core.entity.channel.Channel import dev.kord.core.entity.channel.StageChannel import dev.kord.core.entity.channel.VoiceChannel @@ -12,6 +14,7 @@ import dev.kord.rest.builder.channel.StageVoiceChannelModifyBuilder import dev.kord.rest.builder.guild.CurrentVoiceStateModifyBuilder import dev.kord.rest.builder.guild.VoiceStateModifyBuilder import dev.kord.rest.request.RestRequestException +import dev.kord.rest.service.createStageInstance import dev.kord.rest.service.modifyCurrentVoiceState import dev.kord.rest.service.modifyVoiceState import dev.kord.rest.service.patchStageVoiceChannel @@ -69,6 +72,13 @@ suspend fun StageChannelBehavior.edit(builder: StageVoiceChannelModifyBuilder.() return Channel.from(data, kord) as StageChannel } +suspend fun StageChannelBehavior.createStageInstance(channelId: Snowflake, topic: String): StageInstance { + val instance = kord.rest.stageInstance.createStageInstance(channelId, topic) + val data = StageInstanceData.from(instance) + + return StageInstance(data, kord, supplier) +} + fun StageChannelBehavior( id: Snowflake, guildId: Snowflake, diff --git a/core/src/main/kotlin/cache/data/StageInstanceData.kt b/core/src/main/kotlin/cache/data/StageInstanceData.kt new file mode 100644 index 000000000000..43e6af3ba954 --- /dev/null +++ b/core/src/main/kotlin/cache/data/StageInstanceData.kt @@ -0,0 +1,19 @@ +package dev.kord.core.cache.data + +import dev.kord.common.entity.DiscordStageInstance +import dev.kord.common.entity.Snowflake +import kotlinx.serialization.Serializable + +@Serializable +data class StageInstanceData( + val id: Snowflake, + val guildId: Snowflake, + val channelId: Snowflake, + val topic: String +) { + companion object { + fun from(stageInstance: DiscordStageInstance) = with(stageInstance) { + StageInstanceData(id, guildId, channelId, topic) + } + } +} diff --git a/core/src/main/kotlin/entity/StageInstance.kt b/core/src/main/kotlin/entity/StageInstance.kt new file mode 100644 index 000000000000..7740d9d95999 --- /dev/null +++ b/core/src/main/kotlin/entity/StageInstance.kt @@ -0,0 +1,20 @@ +package dev.kord.core.entity + +import dev.kord.common.entity.Snowflake +import dev.kord.core.Kord +import dev.kord.core.behavior.StageInstanceBehavior +import dev.kord.core.cache.data.StageInstanceData +import dev.kord.core.supplier.EntitySupplier +import dev.kord.core.supplier.EntitySupplyStrategy + +class StageInstance(val data: StageInstanceData, override val kord: Kord, override val supplier: EntitySupplier) : + StageInstanceBehavior { + override val id: Snowflake get() = data.id + val guildId: Snowflake get() = data.guildId + override val channelId: Snowflake get() = data.channelId + val topic: String get() = data.topic + + override fun withStrategy(strategy: EntitySupplyStrategy<*>): Strategizable = + StageInstance(data, kord, strategy.supply(kord)) + +} From 92651fabe06198edaa982dd3059f7a81ae20d779 Mon Sep 17 00:00:00 2001 From: schlaubi Date: Fri, 14 May 2021 09:40:37 +0200 Subject: [PATCH 4/9] Expose creation of StageInstanceBehavior to unsafe - Revert outdated change --- common/src/main/kotlin/entity/Interactions.kt | 2 -- core/src/main/kotlin/Unsafe.kt | 1 - 2 files changed, 3 deletions(-) diff --git a/common/src/main/kotlin/entity/Interactions.kt b/common/src/main/kotlin/entity/Interactions.kt index 77681c94c702..80f4130d3714 100644 --- a/common/src/main/kotlin/entity/Interactions.kt +++ b/common/src/main/kotlin/entity/Interactions.kt @@ -73,7 +73,6 @@ sealed class ApplicationCommandOptionType(val type: Int) { object User : ApplicationCommandOptionType(6) object Channel : ApplicationCommandOptionType(7) object Role : ApplicationCommandOptionType(8) - object Mentionable : ApplicationCommandOptionType(9) class Unknown(type: Int) : ApplicationCommandOptionType(type) companion object; @@ -93,7 +92,6 @@ sealed class ApplicationCommandOptionType(val type: Int) { 6 -> User 7 -> Channel 8 -> Role - 9 -> Mentionable else -> Unknown(type) } } diff --git a/core/src/main/kotlin/Unsafe.kt b/core/src/main/kotlin/Unsafe.kt index 6483ed9ecd42..c949920656d5 100644 --- a/core/src/main/kotlin/Unsafe.kt +++ b/core/src/main/kotlin/Unsafe.kt @@ -91,5 +91,4 @@ class Unsafe(private val kord: Kord) { ): GlobalApplicationCommandBehavior = GlobalApplicationCommandBehavior(applicationId, commandId, service) - } \ No newline at end of file From bde55fe1a8824def903944428fd3b9d1101265d1 Mon Sep 17 00:00:00 2001 From: schlaubi Date: Fri, 14 May 2021 10:01:05 +0200 Subject: [PATCH 5/9] Final additions - Add StageInstanceBehavior.asStageInstance - Fix compiler issue - Add StageChannelBehavior.getStageInstance() --- .../main/kotlin/behavior/StageInstanceBehavior.kt | 7 +++++++ .../kotlin/behavior/channel/StageChannelBehavior.kt | 12 ++++++++++-- core/src/main/kotlin/entity/StageInstance.kt | 5 ++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/behavior/StageInstanceBehavior.kt b/core/src/main/kotlin/behavior/StageInstanceBehavior.kt index eef1ff96a37e..e89482d25aa4 100644 --- a/core/src/main/kotlin/behavior/StageInstanceBehavior.kt +++ b/core/src/main/kotlin/behavior/StageInstanceBehavior.kt @@ -22,6 +22,13 @@ interface StageInstanceBehavior : KordEntity, Strategizable { return StageInstance(data, kord, supplier) } + suspend fun asStageInstance(): StageInstance { + val channel = kord.rest.stageInstance.getStageInstance(channelId) + val data = StageInstanceData.from(channel) + + return StageInstance(data, kord, supplier) + } + override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior = StageInstanceBehavior(id, channelId, kord, strategy.supply(kord)) } diff --git a/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt b/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt index 53aa460cc34c..958296d4608b 100644 --- a/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt +++ b/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt @@ -4,6 +4,7 @@ import dev.kord.common.entity.Snowflake import dev.kord.core.Kord import dev.kord.core.cache.data.ChannelData import dev.kord.core.cache.data.StageInstanceData +import dev.kord.core.entity.Guild import dev.kord.core.entity.StageInstance import dev.kord.core.entity.channel.Channel import dev.kord.core.entity.channel.StageChannel @@ -72,8 +73,15 @@ suspend fun StageChannelBehavior.edit(builder: StageVoiceChannelModifyBuilder.() return Channel.from(data, kord) as StageChannel } -suspend fun StageChannelBehavior.createStageInstance(channelId: Snowflake, topic: String): StageInstance { - val instance = kord.rest.stageInstance.createStageInstance(channelId, topic) +suspend fun StageChannelBehavior.createStageInstance(topic: String): StageInstance { + val instance = kord.rest.stageInstance.createStageInstance(id, topic) + val data = StageInstanceData.from(instance) + + return StageInstance(data, kord, supplier) +} + +suspend fun StageChannelBehavior.getStageInstance(): StageInstance { + val instance = kord.rest.stageInstance.getStageInstance(id) val data = StageInstanceData.from(instance) return StageInstance(data, kord, supplier) diff --git a/core/src/main/kotlin/entity/StageInstance.kt b/core/src/main/kotlin/entity/StageInstance.kt index 7740d9d95999..5987e4d95e09 100644 --- a/core/src/main/kotlin/entity/StageInstance.kt +++ b/core/src/main/kotlin/entity/StageInstance.kt @@ -7,14 +7,13 @@ import dev.kord.core.cache.data.StageInstanceData import dev.kord.core.supplier.EntitySupplier import dev.kord.core.supplier.EntitySupplyStrategy -class StageInstance(val data: StageInstanceData, override val kord: Kord, override val supplier: EntitySupplier) : - StageInstanceBehavior { +class StageInstance(val data: StageInstanceData, override val kord: Kord, override val supplier: EntitySupplier) : StageInstanceBehavior { override val id: Snowflake get() = data.id val guildId: Snowflake get() = data.guildId override val channelId: Snowflake get() = data.channelId val topic: String get() = data.topic - override fun withStrategy(strategy: EntitySupplyStrategy<*>): Strategizable = + override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior = StageInstance(data, kord, strategy.supply(kord)) } From af6c499cdf3c2b76508fc75bcd27a0b4b33f9920 Mon Sep 17 00:00:00 2001 From: schlaubi Date: Fri, 14 May 2021 10:26:30 +0200 Subject: [PATCH 6/9] Add StageInstances to EntitySupplier.kt --- .../kotlin/behavior/StageInstanceBehavior.kt | 4 +++ .../behavior/channel/StageChannelBehavior.kt | 30 +++++++++---------- .../exception/EntityNotFoundException.kt | 3 ++ .../kotlin/supplier/CacheEntitySupplier.kt | 2 ++ .../main/kotlin/supplier/EntitySupplier.kt | 7 ++++- .../kotlin/supplier/RestEntitySupplier.kt | 7 +++++ core/src/samples/kotlin/PingBot.kt | 15 ++++++++++ 7 files changed, 51 insertions(+), 17 deletions(-) diff --git a/core/src/main/kotlin/behavior/StageInstanceBehavior.kt b/core/src/main/kotlin/behavior/StageInstanceBehavior.kt index e89482d25aa4..b3a6a2464f77 100644 --- a/core/src/main/kotlin/behavior/StageInstanceBehavior.kt +++ b/core/src/main/kotlin/behavior/StageInstanceBehavior.kt @@ -43,4 +43,8 @@ internal fun StageInstanceBehavior(id: Snowflake, channelId: Snowflake, kord: Ko get() = id override val supplier: EntitySupplier get() = supplier + + override fun toString(): String { + return "StageInstanceBehavior(id=$id, channelId=$id, kord=$kord, supplier=$supplier)" + } } diff --git a/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt b/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt index 958296d4608b..d7d5cea1d973 100644 --- a/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt +++ b/core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt @@ -4,11 +4,9 @@ import dev.kord.common.entity.Snowflake import dev.kord.core.Kord import dev.kord.core.cache.data.ChannelData import dev.kord.core.cache.data.StageInstanceData -import dev.kord.core.entity.Guild import dev.kord.core.entity.StageInstance import dev.kord.core.entity.channel.Channel import dev.kord.core.entity.channel.StageChannel -import dev.kord.core.entity.channel.VoiceChannel import dev.kord.core.supplier.EntitySupplier import dev.kord.core.supplier.EntitySupplyStrategy import dev.kord.rest.builder.channel.StageVoiceChannelModifyBuilder @@ -34,6 +32,20 @@ interface StageChannelBehavior : BaseVoiceChannelBehavior { return StageChannelBehavior(id, guildId, kord, strategy.supply(kord)) } + + suspend fun tageInstance(topic: String): StageInstance { + val instance = kord.rest.stageInstance.createStageInstance(id, topic) + val data = StageInstanceData.from(instance) + + return StageInstance(data, kord, supplier) + } + + suspend fun getStageInstanceOrNull(): StageInstance? = + withStrategy(EntitySupplyStrategy.rest).supplier.getStageInstanceOrNull(id) + + suspend fun getStageInstance(): StageInstance = + withStrategy(EntitySupplyStrategy.rest).supplier.getStageInstance(id) + } /** @@ -73,20 +85,6 @@ suspend fun StageChannelBehavior.edit(builder: StageVoiceChannelModifyBuilder.() return Channel.from(data, kord) as StageChannel } -suspend fun StageChannelBehavior.createStageInstance(topic: String): StageInstance { - val instance = kord.rest.stageInstance.createStageInstance(id, topic) - val data = StageInstanceData.from(instance) - - return StageInstance(data, kord, supplier) -} - -suspend fun StageChannelBehavior.getStageInstance(): StageInstance { - val instance = kord.rest.stageInstance.getStageInstance(id) - val data = StageInstanceData.from(instance) - - return StageInstance(data, kord, supplier) -} - fun StageChannelBehavior( id: Snowflake, guildId: Snowflake, diff --git a/core/src/main/kotlin/exception/EntityNotFoundException.kt b/core/src/main/kotlin/exception/EntityNotFoundException.kt index 4a6ca68aed23..39c45e34699b 100644 --- a/core/src/main/kotlin/exception/EntityNotFoundException.kt +++ b/core/src/main/kotlin/exception/EntityNotFoundException.kt @@ -63,6 +63,9 @@ class EntityNotFoundException : Exception { fun welcomeScreenNotFound(guildId: Snowflake): Nothing = throw EntityNotFoundException("Welcome screen for guild $guildId was not found.") + fun stageInstanceNotFound(channelId: Snowflake): Nothing = + throw EntityNotFoundException("Stage instance for channel $channelId was not found") + } } \ No newline at end of file diff --git a/core/src/main/kotlin/supplier/CacheEntitySupplier.kt b/core/src/main/kotlin/supplier/CacheEntitySupplier.kt index 5f994729de54..351fd651fd3a 100644 --- a/core/src/main/kotlin/supplier/CacheEntitySupplier.kt +++ b/core/src/main/kotlin/supplier/CacheEntitySupplier.kt @@ -272,6 +272,8 @@ class CacheEntitySupplier(private val kord: Kord) : EntitySupplier { }.asFlow().map { Template(it, kord) } } + override suspend fun getStageInstanceOrNull(channelId: Snowflake): StageInstance? = null + override fun toString(): String { return "CacheEntitySupplier(cache=$cache)" } diff --git a/core/src/main/kotlin/supplier/EntitySupplier.kt b/core/src/main/kotlin/supplier/EntitySupplier.kt index 38dfae09b299..f059f4da729c 100644 --- a/core/src/main/kotlin/supplier/EntitySupplier.kt +++ b/core/src/main/kotlin/supplier/EntitySupplier.kt @@ -1,5 +1,6 @@ package dev.kord.core.supplier +import dev.kord.common.entity.ChannelType.Unknown import dev.kord.common.entity.Snowflake import dev.kord.common.exception.RequestException import dev.kord.core.entity.* @@ -8,7 +9,6 @@ import dev.kord.core.entity.channel.GuildChannel import dev.kord.core.entity.channel.MessageChannel import dev.kord.core.exception.EntityNotFoundException import kotlinx.coroutines.flow.Flow -import dev.kord.common.entity.ChannelType.Unknown /** * An abstraction that allows for requesting Discord entities. @@ -389,6 +389,11 @@ interface EntitySupplier { getTemplateOrNull(code) ?: EntityNotFoundException.templateNotFound(code) fun getTemplates(guildId: Snowflake): Flow