Skip to content

Commit

Permalink
Sealed message types (kordlib#282)
Browse files Browse the repository at this point in the history
* Expose the creation of application commands behavior

* Make message types sealed

* make Unknown a class

* Add missing message types

* make MessageTypeSerializer internal
  • Loading branch information
HopeBaron authored and DRSchlaubi committed May 26, 2021
1 parent 83ea7c9 commit 36bd219
Showing 1 changed file with 55 additions and 20 deletions.
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

0 comments on commit 36bd219

Please sign in to comment.