From 4f1e5e32893028fad42248b68e26f75254d2aa9e Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Fri, 25 Jun 2021 20:14:38 +0300 Subject: [PATCH] commit #333 --- CHANGELOG.md | 10 +++++++ common/src/main/kotlin/entity/Interactions.kt | 1 + core/api/core.api | 1 + .../kotlin/entity/interaction/Interaction.kt | 11 +++++--- rest/api/rest.api | 26 ++++++++++++------- .../interaction/ApplicationCommandBuilders.kt | 14 ++++++++-- .../builder/interaction/OptionsBuilder.kt | 13 ++++++++++ 7 files changed, 61 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e895e42333..b1ddc2f1dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 0.7.2 + +## Additions + +* Allow adding a mentionable argument to commands + +## Fixes + +* Mentionable arguments now correctly retrieve their entity. + # 0.7.1 ## Breaking diff --git a/common/src/main/kotlin/entity/Interactions.kt b/common/src/main/kotlin/entity/Interactions.kt index 571e4c11e09..740aa920fdf 100644 --- a/common/src/main/kotlin/entity/Interactions.kt +++ b/common/src/main/kotlin/entity/Interactions.kt @@ -91,6 +91,7 @@ sealed class ApplicationCommandOptionType(val type: Int) { 6 -> User 7 -> Channel 8 -> Role + 9 -> Mentionable else -> Unknown(type) } } diff --git a/core/api/core.api b/core/api/core.api index cdbf4e1f467..318c865ee1b 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -5786,6 +5786,7 @@ public final class dev/kord/core/entity/interaction/InteractionKt { public static final fun channel (Ldev/kord/core/entity/interaction/OptionValue;)Ldev/kord/core/entity/channel/ResolvedChannel; public static final fun int (Ldev/kord/core/entity/interaction/OptionValue;)I public static final fun member (Ldev/kord/core/entity/interaction/OptionValue;)Ldev/kord/core/entity/Member; + public static final fun mentionable (Ldev/kord/core/entity/interaction/OptionValue;)Ldev/kord/core/entity/Entity; public static final fun role (Ldev/kord/core/entity/interaction/OptionValue;)Ldev/kord/core/entity/Role; public static final fun string (Ldev/kord/core/entity/interaction/OptionValue;)Ljava/lang/String; public static final fun user (Ldev/kord/core/entity/interaction/OptionValue;)Ldev/kord/core/entity/User; diff --git a/core/src/main/kotlin/entity/interaction/Interaction.kt b/core/src/main/kotlin/entity/interaction/Interaction.kt index bd1424603fc..096d1784ddb 100644 --- a/core/src/main/kotlin/entity/interaction/Interaction.kt +++ b/core/src/main/kotlin/entity/interaction/Interaction.kt @@ -303,11 +303,11 @@ fun OptionValue(value: CommandArgument<*>, resolvedObjects: ResolvedObjects?): O is CommandArgument.MentionableArgument -> { val channel = resolvedObjects?.channels.orEmpty()[value.value] - val user = resolvedObjects?.channels.orEmpty()[value.value] + val user = resolvedObjects?.users.orEmpty()[value.value] val member = resolvedObjects?.members.orEmpty()[value.value] - val role = resolvedObjects?.members.orEmpty()[value.value] + val role = resolvedObjects?.roles.orEmpty()[value.value] - OptionValue.MentionableOptionValue((channel ?: user ?: member ?: role)!!) + OptionValue.MentionableOptionValue((channel ?: member ?: user ?: role)!!) } is CommandArgument.RoleArgument -> { @@ -459,3 +459,8 @@ fun OptionValue<*>.boolean() = value as Boolean @KordPreview fun OptionValue<*>.int() = value as Int + +@KordPreview +fun OptionValue<*>.mentionable(): Entity { + return value as Entity +} diff --git a/rest/api/rest.api b/rest/api/rest.api index 46f00d8e9ac..98cc2c2d90f 100644 --- a/rest/api/rest.api +++ b/rest/api/rest.api @@ -625,6 +625,8 @@ public abstract class dev/kord/rest/builder/interaction/BaseApplicationBuilder { public final fun group (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public final fun int (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public static synthetic fun int$default (Ldev/kord/rest/builder/interaction/BaseApplicationBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V + public final fun mentionable (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V + public static synthetic fun mentionable$default (Ldev/kord/rest/builder/interaction/BaseApplicationBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V public final fun role (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public static synthetic fun role$default (Ldev/kord/rest/builder/interaction/BaseApplicationBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V public abstract fun setOptions (Ljava/util/List;)V @@ -695,10 +697,10 @@ public final class dev/kord/rest/builder/interaction/EphemeralFollowupMessageCre public fun getComponents ()Ljava/util/List; public fun getContent ()Ljava/lang/String; public fun getEmbeds ()Ljava/util/List; - public fun getTts ()Ljava/lang/Boolean; + public final fun getTts ()Ljava/lang/Boolean; public fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V public fun setContent (Ljava/lang/String;)V - public fun setTts (Ljava/lang/Boolean;)V + public final fun setTts (Ljava/lang/Boolean;)V public fun toRequest ()Ldev/kord/rest/json/request/MultipartFollowupMessageCreateRequest; public synthetic fun toRequest ()Ljava/lang/Object; } @@ -709,10 +711,10 @@ public final class dev/kord/rest/builder/interaction/EphemeralFollowupMessageMod public fun getComponents ()Ljava/util/List; public fun getContent ()Ljava/lang/String; public fun getEmbeds ()Ljava/util/List; - public fun getTts ()Ljava/lang/Boolean; public fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V + public fun setComponents (Ljava/util/List;)V public fun setContent (Ljava/lang/String;)V - public fun setTts (Ljava/lang/Boolean;)V + public fun setEmbeds (Ljava/util/List;)V public fun toRequest ()Ldev/kord/rest/json/request/FollowupMessageModifyRequest; public synthetic fun toRequest ()Ljava/lang/Object; } @@ -748,9 +750,7 @@ public abstract interface class dev/kord/rest/builder/interaction/FollowupMessag public abstract fun getComponents ()Ljava/util/List; public abstract fun getContent ()Ljava/lang/String; public abstract fun getEmbeds ()Ljava/util/List; - public abstract fun getTts ()Ljava/lang/Boolean; public abstract fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V - public abstract fun setTts (Ljava/lang/Boolean;)V } public final class dev/kord/rest/builder/interaction/FollowupMessageBuildersKt { @@ -771,6 +771,10 @@ public final class dev/kord/rest/builder/interaction/IntChoiceBuilder : dev/kord public synthetic fun choice (Ljava/lang/String;Ljava/lang/Object;)V } +public final class dev/kord/rest/builder/interaction/MentionableBuilder : dev/kord/rest/builder/interaction/OptionsBuilder { + public fun (Ljava/lang/String;Ljava/lang/String;)V +} + public abstract class dev/kord/rest/builder/interaction/OptionsBuilder : dev/kord/rest/builder/RequestBuilder { public synthetic fun (Ljava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/ApplicationCommandOptionType;Lkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getDefault ()Ljava/lang/Boolean; @@ -794,10 +798,10 @@ public final class dev/kord/rest/builder/interaction/PublicFollowupMessageCreate public fun getContent ()Ljava/lang/String; public fun getEmbeds ()Ljava/util/List; public final fun getFiles ()Ljava/util/List; - public fun getTts ()Ljava/lang/Boolean; + public final fun getTts ()Ljava/lang/Boolean; public fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V public fun setContent (Ljava/lang/String;)V - public fun setTts (Ljava/lang/Boolean;)V + public final fun setTts (Ljava/lang/Boolean;)V public fun toRequest ()Ldev/kord/rest/json/request/MultipartFollowupMessageCreateRequest; public synthetic fun toRequest ()Ljava/lang/Object; } @@ -811,10 +815,10 @@ public final class dev/kord/rest/builder/interaction/PublicFollowupMessageModify public fun getContent ()Ljava/lang/String; public fun getEmbeds ()Ljava/util/List; public final fun getFiles ()Ljava/util/List; - public fun getTts ()Ljava/lang/Boolean; public fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V + public fun setComponents (Ljava/util/List;)V public fun setContent (Ljava/lang/String;)V - public fun setTts (Ljava/lang/Boolean;)V + public fun setEmbeds (Ljava/util/List;)V public fun toRequest ()Ldev/kord/rest/json/request/MultipartFollowupMessageModifyRequest; public synthetic fun toRequest ()Ljava/lang/Object; } @@ -872,6 +876,8 @@ public final class dev/kord/rest/builder/interaction/SubCommandBuilder : dev/kor public static synthetic fun channel$default (Ldev/kord/rest/builder/interaction/SubCommandBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V public final fun int (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public static synthetic fun int$default (Ldev/kord/rest/builder/interaction/SubCommandBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V + public final fun mentionable (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V + public static synthetic fun mentionable$default (Ldev/kord/rest/builder/interaction/SubCommandBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V public final fun role (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public static synthetic fun role$default (Ldev/kord/rest/builder/interaction/SubCommandBuilder;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V public final fun string (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V diff --git a/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt b/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt index a97c6345323..4764981f759 100644 --- a/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt +++ b/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt @@ -85,6 +85,15 @@ sealed class BaseApplicationBuilder { if (options == null) options = mutableListOf() options!!.add(ChannelBuilder(name, description).apply(builder)) } + + @OptIn(ExperimentalContracts::class) + inline fun mentionable(name: String, description: String, builder: MentionableBuilder.() -> Unit = {}) { + contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } + if (options == null) options = mutableListOf() + options!!.add(MentionableBuilder(name, description).apply(builder)) + + } + } @@ -102,7 +111,8 @@ class ApplicationCommandCreateBuilder( var defaultPermission: Boolean? by ::_defaultPermission.delegate() override fun toRequest(): ApplicationCommandCreateRequest { - return ApplicationCommandCreateRequest(name, + return ApplicationCommandCreateRequest( + name, description, _options.mapList { it.toRequest() }, _defaultPermission ) @@ -154,7 +164,7 @@ class ApplicationCommandModifyBuilder : BaseApplicationBuilder(), return ApplicationCommandModifyRequest( _name, _description, - _options.mapList { it.toRequest() }, + _options.mapList { it.toRequest() }, _defaultPermission ) diff --git a/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt b/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt index dc4ca128297..936843c03ca 100644 --- a/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt +++ b/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt @@ -101,6 +101,10 @@ class RoleBuilder(name: String, description: String) : class ChannelBuilder(name: String, description: String) : OptionsBuilder(name, description, ApplicationCommandOptionType.Channel) +@KordPreview +class MentionableBuilder(name: String, description: String) : + OptionsBuilder(name, description, ApplicationCommandOptionType.Mentionable) + @KordDsl @KordPreview sealed class BaseCommandOptionBuilder( @@ -169,6 +173,15 @@ class SubCommandBuilder(name: String, description: String) : if (options == null) options = mutableListOf() options!!.add(ChannelBuilder(name, description).apply(builder)) } + + @OptIn(ExperimentalContracts::class) + inline fun mentionable(name: String, description: String, builder: MentionableBuilder.() -> Unit = {}) { + contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } + if (options == null) options = mutableListOf() + options!!.add(MentionableBuilder(name, description).apply(builder)) + + } + } @KordDsl