Skip to content

Commit

Permalink
Merge branch '0.7.x' into kotlin-1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
BartArys authored May 23, 2021
2 parents 81694e7 + 6ed1921 commit b550a90
Show file tree
Hide file tree
Showing 25 changed files with 351 additions and 7 deletions.
12 changes: 12 additions & 0 deletions common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.kord.common.entity

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
Expand Down Expand Up @@ -102,6 +103,7 @@ data class DiscordMessage(
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("referenced_message")
val referencedMessage: Optional<DiscordMessage?> = Optional.Missing(),
val interaction: Optional<DiscordMessageInteraction> = Optional.Missing()
)

/**
Expand Down Expand Up @@ -242,6 +244,7 @@ data class DiscordPartialMessage(
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("referenced_message")
val referencedMessage: Optional<DiscordMessage?> = Optional.Missing(),
val interaction: Optional<DiscordMessageInteraction> = Optional.Missing(),
)

@Serializable
Expand Down Expand Up @@ -827,3 +830,12 @@ data class AllowedMentions(
@SerialName("replied_user")
val repliedUser: OptionalBoolean = OptionalBoolean.Missing
)

@KordPreview
@Serializable
data class DiscordMessageInteraction(
val id: Snowflake,
val type: InteractionType,
val name: String,
val user: DiscordUser
)
23 changes: 23 additions & 0 deletions common/src/main/kotlin/entity/DiscordStageInstance.kt
Original file line number Diff line number Diff line change
@@ -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
)
5 changes: 4 additions & 1 deletion core/src/main/kotlin/Unsafe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -87,5 +91,4 @@ class Unsafe(private val kord: Kord) {
): GlobalApplicationCommandBehavior =
GlobalApplicationCommandBehavior(applicationId, commandId, service)


}
45 changes: 45 additions & 0 deletions core/src/main/kotlin/behavior/StageInstanceBehavior.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
}

suspend fun asStageInstance(): StageInstance = supplier.getStageInstance(channelId)

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

override fun toString(): String {
return "StageInstanceBehavior(id=$id, channelId=$id, kord=$kord, supplier=$supplier)"
}
}
16 changes: 15 additions & 1 deletion core/src/main/kotlin/behavior/channel/StageChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ 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.entity.channel.VoiceChannel
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
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
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
Expand All @@ -30,6 +33,17 @@ interface StageChannelBehavior : BaseVoiceChannelBehavior {
return StageChannelBehavior(id, guildId, kord, strategy.supply(kord))
}

suspend fun createStageInstance(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? = supplier.getStageInstanceOrNull(id)

suspend fun getStageInstance(): StageInstance = supplier.getStageInstance(id)

}

/**
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/kotlin/cache/data/MessageData.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.kord.core.cache.data

import cache.data.MessageInteractionData
import dev.kord.cache.api.data.description
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.*
Expand Down Expand Up @@ -35,6 +36,7 @@ data class MessageData(
val flags: Optional<MessageFlags> = Optional.Missing(),
val stickers: Optional<List<MessageStickerData>> = Optional.Missing(),
val referencedMessage: Optional<MessageData?> = Optional.Missing(),
val interaction: Optional<MessageInteractionData> = Optional.Missing()
) {

fun plus(selfId: Snowflake, reaction: MessageReactionAddData): MessageData {
Expand Down Expand Up @@ -70,6 +72,9 @@ data class MessageData(
partialMessage.mentionedChannels.mapList { it.id }.switchOnMissing(mentionedChannels.value.orEmpty())
.coerceToMissing()
val stickers = partialMessage.stickers.mapList { MessageStickerData.from(it) }.switchOnMissing(this.stickers)
val referencedMessage = partialMessage.referencedMessage.mapNullable { it?.toData() ?: referencedMessage.value }
val interaction =
partialMessage.interaction.map { MessageInteractionData.from(it) }.switchOnMissing(interaction)

return MessageData(
id,
Expand Down Expand Up @@ -97,6 +102,8 @@ data class MessageData(
messageReference,
flags,
stickers = stickers,
referencedMessage = referencedMessage,
interaction = interaction
)
}

Expand Down Expand Up @@ -130,7 +137,8 @@ data class MessageData(
messageReference.map { MessageReferenceData.from(it) },
flags,
stickers.mapList { MessageStickerData.from(it) },
referencedMessage.mapNotNull { from(it) }
referencedMessage.mapNotNull { from(it) },
interaction.map { MessageInteractionData.from(it) }
)
}
}
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/kotlin/cache/data/MessageInteractionData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cache.data;

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.DiscordMessageInteraction
import dev.kord.common.entity.InteractionType
import dev.kord.common.entity.Snowflake
import dev.kord.core.cache.data.UserData
import dev.kord.core.cache.data.toData
import kotlinx.serialization.Serializable

@KordPreview
@Serializable
data class MessageInteractionData(
val id:Snowflake,
val type:InteractionType,
val name:String,
val user: Snowflake
) {
companion object {
fun from(entity: DiscordMessageInteraction): MessageInteractionData = with(entity) {
MessageInteractionData(id, type, name, user.id)
}
}
}
19 changes: 19 additions & 0 deletions core/src/main/kotlin/cache/data/StageInstanceData.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
10 changes: 10 additions & 0 deletions core/src/main/kotlin/entity/Message.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.kord.core.entity

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.MessageType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.mapNullable
import dev.kord.common.entity.optional.orEmpty
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
Expand All @@ -13,6 +16,7 @@ import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.core.entity.interaction.MessageInteraction
import dev.kord.core.entity.interaction.Interaction
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
Expand Down Expand Up @@ -178,6 +182,12 @@ class Message(
*/
val mentionedUserBehaviors: Set<UserBehavior> get() = data.mentions.map { UserBehavior(it, kord) }.toSet()

/**
* The [MessageInteraction] sent on this message object when it is a response to an [dev.kord.core.entity.interaction.Interaction].
*/
@KordPreview
val interaction: MessageInteraction? get() = data.interaction.mapNullable { MessageInteraction(it, kord) }.value

/**
* The [users][User] mentioned in this message.
*
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/kotlin/entity/StageInstance.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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<*>): StageInstanceBehavior =
StageInstance(data, kord, strategy.supply(kord))

}
5 changes: 3 additions & 2 deletions core/src/main/kotlin/entity/VoiceState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.kord.common.annotation.DeprecatedSinceKord
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import dev.kord.core.KordObject
import dev.kord.core.behavior.channel.BaseVoiceChannelBehavior
import dev.kord.core.cache.data.VoiceStateData
import dev.kord.core.entity.channel.VoiceChannel
import dev.kord.core.supplier.EntitySupplier
Expand Down Expand Up @@ -51,15 +52,15 @@ class VoiceState(
*/
@DeprecatedSinceKord("0.7.0")
@Deprecated("Use getChannelOrNull instead.", ReplaceWith("getChannelOrNull"), DeprecationLevel.ERROR)
suspend fun getChannel(): VoiceChannel? = channelId?.let { supplier.getChannelOfOrNull(it) }
suspend fun getChannel(): BaseVoiceChannelBehavior? = channelId?.let { supplier.getChannelOfOrNull(it) }

/**
* Requests to get the voice channel through the [strategy],
* returns null if the [VoiceChannel] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun getChannelOrNull(): VoiceChannel? = channelId?.let { supplier.getChannelOfOrNull(it) }
suspend fun getChannelOrNull(): BaseVoiceChannelBehavior? = channelId?.let { supplier.getChannelOfOrNull(it) }


/**
Expand Down
67 changes: 67 additions & 0 deletions core/src/main/kotlin/entity/interaction/MessageInteraction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.kord.core.entity.interaction

import cache.data.MessageInteractionData
import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.InteractionType
import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Message
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.User
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy

/**
* An instance of [MessageInteraction](https://discord.com/developers/docs/interactions/slash-commands#messageinteraction)
* This is sent on the [Message] object when the message is a response to an [Interaction].
*/
@KordPreview
class MessageInteraction(
val data: MessageInteractionData,
override val kord: Kord,
override val supplier: EntitySupplier = kord.defaultSupplier
) : KordEntity, Strategizable {
/**
* [id][Interaction.id] of the [Interaction] this message is responding to.
*/
override val id: Snowflake get() = data.id

/**
* the [name][ApplicationCommand.name] of the [ApplicationCommand] that triggered this message.
*/
val name: String get() = data.name

/**
* The [UserBehavior] of the [user][Interaction.user] who invoked the [Interaction]
*/
val user: UserBehavior get() = UserBehavior(data.id, kord)

/**
* the [InteractionType] of the interaction [MessageInteraction].
*/
val type: InteractionType get() = data.type

/**
* Requests the [User] of this interaction message.
*
* @throws RequestException if something went wrong while retrieving the user.
* @throws EntityNotFoundException if the user was null.
*/
suspend fun getUser(): User = supplier.getUser(user.id)

/**
* Requests to get the user of this interaction message,
* returns null if the [User] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun getUserOrNull(): User? = supplier.getUserOrNull(user.id)

override fun withStrategy(strategy: EntitySupplyStrategy<*>): MessageInteraction {
return MessageInteraction(data, kord, strategy.supply(kord))
}
}
Loading

0 comments on commit b550a90

Please sign in to comment.