-
Notifications
You must be signed in to change notification settings - Fork 81
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
Improve slash command API and add support for components #310
Merged
+1,615
−259
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4975ced
Make slash command creation eager
BartArys 0773907
Fix typo in InteractionBehavior
BartArys 90290f1
Don't compute supplier in InteractionBehavior
BartArys f791645
Specify withStrategy for Interactions
BartArys 05bad6a
Introduce type to command options
BartArys f2aab31
Add KordDsl to builders
BartArys 62b0561
Add allowedMentions builder functions
BartArys a1b3fa6
Add permission edits to guild commands
BartArys a440dc7
Make full member available for guild contexts
BartArys e047ba9
Support buttons/components (#303)
DRSchlaubi d64c7d3
Add core versions of components
BartArys 5237de5
Restructure and document ButtonBuilder
BartArys 50ec6e8
Remove ActionRowContainerBuilder
BartArys 5958cab
Make ComponentInteraction message nullable
BartArys 7c68dc2
Add missing components to interaction builders
BartArys 22c8a17
Add missing ComponentInteraction behavior
BartArys fa3adbb
Fix withStrategy for ComponentInteractionBehavior
BartArys 79938e8
Implement ComponentInteractionBehavior
BartArys f1573a6
Move component builders directory
BartArys de13dfa
Merge branch '0.7.x' into changes/0.7.x/slash-command-improvements
BartArys 89ee61f
Fix interaction embeds optionality
BartArys 175e06e
Make CommandInteraction#guildId optional
BartArys f1e310e
Make MessageModifyBuilder components vals
BartArys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
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 kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
/** | ||
* Represent a [intractable component within a message sent in Discord](https://discord.com/developers/docs/interactions/message-components#what-are-components). | ||
* | ||
* @property type the [ComponentType] of the component | ||
* @property style the [ButtonStyle] of the component (if it is a button) | ||
* @property style the text that appears on the button (if the component is a button) | ||
* @property emoji an [DiscordPartialEmoji] that appears on the button (if the component is a button) | ||
* @property customId a developer-defined identifier for the button, max 100 characters | ||
* @property url a url for link-style buttons | ||
* @property disabled whether the button is disabled, default `false` | ||
* @property components a list of child components (for action rows) | ||
*/ | ||
@KordPreview | ||
@Serializable | ||
data class DiscordComponent( | ||
val type: ComponentType, | ||
val style: Optional<ButtonStyle> = Optional.Missing(), | ||
val label: Optional<String> = Optional.Missing(), | ||
val emoji: Optional<DiscordPartialEmoji> = Optional.Missing(), | ||
@SerialName("custom_id") | ||
val customId: Optional<String> = Optional.Missing(), | ||
val url: Optional<String> = Optional.Missing(), | ||
val disabled: OptionalBoolean = OptionalBoolean.Missing, | ||
val components: Optional<List<DiscordComponent>> = Optional.Missing() | ||
) | ||
|
||
/** | ||
* Representation of different [DiscordComponent] types. | ||
* | ||
* @property value the raw type value used by the Discord API | ||
*/ | ||
@KordPreview | ||
@Serializable(with = ComponentType.Serializer::class) | ||
sealed class ComponentType(val value: Int) { | ||
|
||
/** | ||
* Fallback type used for types that haven't been added to Kord yet. | ||
*/ | ||
class Unknown(value: Int) : ComponentType(value) | ||
|
||
/** | ||
* A container for other components. | ||
*/ | ||
object ActionRow : ComponentType(1) | ||
|
||
/** | ||
* A clickable button. | ||
*/ | ||
object Button : ComponentType(2) | ||
|
||
companion object Serializer : KSerializer<ComponentType> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ComponentType", PrimitiveKind.INT) | ||
|
||
override fun deserialize(decoder: Decoder): ComponentType = | ||
when (val value = decoder.decodeInt()) { | ||
1 -> ActionRow | ||
2 -> Button | ||
else -> Unknown(value) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: ComponentType) = encoder.encodeInt(value.value) | ||
} | ||
} | ||
|
||
/** | ||
* Representation of different ButtonStyles. | ||
* | ||
* A cheat sheet on how the styles look like can be found [here](https://discord.com/assets/7bb017ce52cfd6575e21c058feb3883b.png) | ||
* | ||
* @see ComponentType.Button | ||
*/ | ||
@KordPreview | ||
@Serializable(with = ButtonStyle.Serializer::class) | ||
sealed class ButtonStyle(val value: Int) { | ||
|
||
/** | ||
* A fallback style used for styles that haven't been added to Kord yet. | ||
*/ | ||
class Unknown(value: Int) : ButtonStyle(value) | ||
|
||
/** | ||
* Blurple. | ||
* Requires: [DiscordComponent.customId] | ||
*/ | ||
object Primary : ButtonStyle(1) | ||
|
||
/** | ||
* Grey. | ||
* Requires: [DiscordComponent.customId] | ||
*/ | ||
object Secondary : ButtonStyle(2) | ||
|
||
/** | ||
* Green | ||
* Requires: [DiscordComponent.customId] | ||
*/ | ||
object Success : ButtonStyle(3) | ||
|
||
/** | ||
* Red. | ||
* Requires: [DiscordComponent.customId] | ||
*/ | ||
object Danger : ButtonStyle(4) | ||
|
||
/** | ||
* Grey, navigates to an URL. | ||
* Requires: [DiscordComponent.url] | ||
*/ | ||
object Link : ButtonStyle(5) | ||
|
||
companion object Serializer : KSerializer<ButtonStyle> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ButtonStyle", PrimitiveKind.INT) | ||
|
||
override fun deserialize(decoder: Decoder): ButtonStyle = | ||
when (val value = decoder.decodeInt()) { | ||
1 -> Primary | ||
2 -> Secondary | ||
3 -> Success | ||
4 -> Danger | ||
5 -> Link | ||
else -> Unknown(value) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: ButtonStyle) = encoder.encodeInt(value.value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
U sure this works? buildSrc is really slow at upgrading the Kotlin version and if it's to new it get's mad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version seems to exist, the CI doesn't fail because of it and building it locally with
gradle clean build --no-build-cache
doesn't seem to produce any errors.If you have any issues building this yourself because of this, please do share a stacktrace and I'll look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's not the problem the version exist as soon as the new gradle plugin version is out
We need to wait for the gradle release adding support for the Kotlin version but if the build works for you i guess it works now