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

Sealed message types #282

Merged
merged 5 commits into from
May 14, 2021
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
75 changes: 55 additions & 20 deletions common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -719,42 +719,77 @@ data class AllRemovedMessageReactions(
)

@Serializable(with = MessageType.MessageTypeSerializer::class)
enum class MessageType(val code: Int) {
sealed class MessageType(val code: Int) {
/** The default code for unknown values. */
Unknown(Int.MIN_VALUE),
Default(0),
RecipientAdd(1),
RecipientRemove(2),
Call(3),
ChannelNameChange(4),
ChannelIconChange(5),
ChannelPinnedMessage(6),
GuildMemberJoin(7),
UserPremiumGuildSubscription(8),
UserPremiumGuildSubscriptionTierOne(9),
UserPremiumGuildSubscriptionTwo(10),
UserPremiumGuildSubscriptionThree(11),
ChannelFollowAdd(12),
GuildDiscoveryDisqualified(14),
class Unknown(code: Int) : MessageType(code)
object Default : MessageType(0)
object RecipientAdd : MessageType(1)
object RecipientRemove : MessageType(2)
object Call : MessageType(3)
object ChannelNameChange : MessageType(4)
object ChannelIconChange : MessageType(5)
object ChannelPinnedMessage : MessageType(6)
object GuildMemberJoin : MessageType(7)
object UserPremiumGuildSubscription : MessageType(8)
object UserPremiumGuildSubscriptionTierOne : MessageType(9)
object UserPremiumGuildSubscriptionTwo : MessageType(10)
object UserPremiumGuildSubscriptionThree : MessageType(11)
object ChannelFollowAdd : MessageType(12)
object GuildDiscoveryDisqualified : MessageType(14)

@Suppress("SpellCheckingInspection")
GuildDiscoveryRequalified(15),
Reply(19);
object GuildDiscoveryRequalified : MessageType(15)
object GuildDiscoveryGracePeriodInitialWarning : MessageType(16)
object GuildDiscoveryGracePeriodFinalWarning : MessageType(17)
object ThreadCreated : MessageType(18)
object Reply : MessageType(19)
object ApplicationCommand : MessageType(20)
object ThreadStarterMessage : MessageType(21)
object GuildInviteReminder : MessageType(22)

companion object MessageTypeSerializer : KSerializer<MessageType> {
internal object MessageTypeSerializer : KSerializer<MessageType> {

override val descriptor: SerialDescriptor
get() = PrimitiveSerialDescriptor("type", PrimitiveKind.INT)

override fun deserialize(decoder: Decoder): MessageType {
val code = decoder.decodeInt()
return values().firstOrNull { it.code == code } ?: Unknown
return values.firstOrNull { it.code == code } ?: Unknown(code)
}

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

companion object {
val values: Set<MessageType>
get() = setOf(
Default,
RecipientAdd,
RecipientRemove,
Call,
ChannelNameChange,
ChannelIconChange,
ChannelPinnedMessage,
GuildMemberJoin,
UserPremiumGuildSubscription,
UserPremiumGuildSubscriptionTierOne,
UserPremiumGuildSubscriptionTwo,
UserPremiumGuildSubscriptionThree,
ChannelFollowAdd,
GuildDiscoveryDisqualified,
GuildDiscoveryRequalified,
Reply,
GuildDiscoveryGracePeriodInitialWarning,
GuildDiscoveryGracePeriodFinalWarning,
ThreadCreated,
ApplicationCommand,
ThreadStarterMessage,
GuildInviteReminder,

)
}
}

@Serializable(with = AllowedMentionType.Serializer::class)
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/kotlin/Unsafe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.kord.common.annotation.KordUnsafe
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.*
import dev.kord.core.behavior.channel.*
import dev.kord.rest.service.InteractionService

/**
* A class that exposes the creation of `{Entity}Behavior` classes.
Expand Down Expand Up @@ -71,4 +72,20 @@ class Unsafe(private val kord: Kord) {
return "Unsafe"
}

fun guildApplicationCommand(
guildId: Snowflake,
applicationId: Snowflake,
commandId: Snowflake,
service: InteractionService = kord.rest.interaction
): GuildApplicationCommandBehavior =
GuildApplicationCommandBehavior(guildId, applicationId, commandId, service)

fun globalApplicationCommand(
applicationId: Snowflake,
commandId: Snowflake,
service: InteractionService = kord.rest.interaction
): GlobalApplicationCommandBehavior =
GlobalApplicationCommandBehavior(applicationId, commandId, service)


}
33 changes: 33 additions & 0 deletions core/src/main/kotlin/behavior/GlobalApplicationCommandBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,37 @@ interface GuildApplicationCommandBehavior : ApplicationCommandBehavior {
override suspend fun delete() {
service.deleteGuildApplicationCommand(applicationId, guildId, id)
}

}

@KordPreview
fun GuildApplicationCommandBehavior(
guildId: Snowflake,
applicationId: Snowflake,
id: Snowflake,
service: InteractionService
): GuildApplicationCommandBehavior = object : GuildApplicationCommandBehavior {
override val guildId: Snowflake
get() = guildId
override val applicationId: Snowflake
get() = applicationId
override val service: InteractionService
get() = service
override val id: Snowflake
get() = id
}


@KordPreview
fun GlobalApplicationCommandBehavior(
applicationId: Snowflake,
id: Snowflake,
service: InteractionService
): GlobalApplicationCommandBehavior = object : GlobalApplicationCommandBehavior {
override val applicationId: Snowflake
get() = applicationId
override val service: InteractionService
get() = service
override val id: Snowflake
get() = id
}