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

Improve slash command API and add support for components #310

Merged
merged 23 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4975ced
Make slash command creation eager
BartArys May 8, 2021
0773907
Fix typo in InteractionBehavior
BartArys May 8, 2021
90290f1
Don't compute supplier in InteractionBehavior
BartArys May 9, 2021
f791645
Specify withStrategy for Interactions
BartArys May 9, 2021
05bad6a
Introduce type to command options
BartArys May 10, 2021
f2aab31
Add KordDsl to builders
BartArys May 10, 2021
62b0561
Add allowedMentions builder functions
BartArys May 14, 2021
a1b3fa6
Add permission edits to guild commands
BartArys May 25, 2021
a440dc7
Make full member available for guild contexts
BartArys May 30, 2021
e047ba9
Support buttons/components (#303)
DRSchlaubi Jun 9, 2021
d64c7d3
Add core versions of components
BartArys Jun 11, 2021
5237de5
Restructure and document ButtonBuilder
BartArys Jun 11, 2021
50ec6e8
Remove ActionRowContainerBuilder
BartArys Jun 11, 2021
5958cab
Make ComponentInteraction message nullable
BartArys Jun 11, 2021
7c68dc2
Add missing components to interaction builders
BartArys Jun 11, 2021
22c8a17
Add missing ComponentInteraction behavior
BartArys Jun 11, 2021
fa3adbb
Fix withStrategy for ComponentInteractionBehavior
BartArys Jun 11, 2021
79938e8
Implement ComponentInteractionBehavior
BartArys Jun 11, 2021
f1573a6
Move component builders directory
BartArys Jun 11, 2021
de13dfa
Merge branch '0.7.x' into changes/0.7.x/slash-command-improvements
BartArys Jun 12, 2021
89ee61f
Fix interaction embeds optionality
BartArys Jun 12, 2021
175e06e
Make CommandInteraction#guildId optional
BartArys Jun 12, 2021
f1e310e
Make MessageModifyBuilder components vals
BartArys Jun 12, 2021
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
5 changes: 2 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.apache.commons.codec.binary.Base64
buildscript {
repositories {
jcenter()
mavenCentral()
maven(url = "https://plugins.gradle.org/m2/")
}
dependencies {
Expand All @@ -20,7 +21,7 @@ buildscript {

plugins {
id("org.jetbrains.kotlin.jvm") version Versions.kotlin
id("org.jetbrains.dokka") version "1.4.0"
id("org.jetbrains.dokka") version "1.4.30"
id("org.ajoberstar.git-publish") version "2.1.3"

signing
Expand All @@ -32,7 +33,6 @@ apply(plugin = "binary-compatibility-validator")

repositories {
mavenCentral()
jcenter()
mavenLocal()
}

Expand Down Expand Up @@ -218,7 +218,6 @@ tasks {
dokkaHtmlMultiModule.configure {
dependsOn(clean)
outputDirectory.set(file(dokkaOutputDir))
documentationFileName.set("DokkaDescription.md")
}


Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

dependencies {
implementation(kotlin("gradle-plugin-api", version = "1.4.0"))
implementation(kotlin("gradle-plugin-api", version = "1.4.21"))
implementation(gradleApi())
implementation(localGroovy())
}
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/Compiler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
object CompilerArguments {
const val inlineClasses = "-XXLanguage:+InlineClasses"
const val coroutines = "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
const val time = "-Xopt-in=kotlin.time.ExperimentalTime"
const val stdLib = "-Xopt-in=kotlin.ExperimentalStdlibApi"
Expand Down
16 changes: 9 additions & 7 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
object Versions {
const val kotlin = "1.4.32"
const val kotlinxSerialization = "1.1.0"
const val ktor = "1.5.2"
const val kotlinxCoroutines = "1.4.2"
const val kotlinLogging = "2.0.4"
const val atomicFu = "0.15.1"
const val binaryCompatibilityValidator = "0.4.0"
const val kotlin = "1.5.10"
const val kotlinxSerialization = "1.2.1"
const val ktor = "1.6.0"
const val kotlinxCoroutines = "1.5.0"
const val kotlinLogging = "2.0.6"
const val dateTime = "0.2.1"
const val atomicFu = "0.16.1"
const val binaryCompatibilityValidator = "0.5.0"

//test deps
const val kotlinTest = kotlin
Expand All @@ -28,6 +29,7 @@ object Dependencies {
"org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinxSerialization}"
const val `kotlinx-coroutines` = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinxCoroutines}"
const val `kotlinx-atomicfu` = "org.jetbrains.kotlinx:atomicfu-jvm:${Versions.atomicFu}"
const val `kotlinx-datetime` = "org.jetbrains.kotlinx:kotlinx-datetime:${Versions.dateTime}"

const val `kotlin-logging` = "io.github.microutils:kotlin-logging:${Versions.kotlinLogging}"

Expand Down
5 changes: 4 additions & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ configurations {
}
}

dependencies {
api(Dependencies.`kotlinx-datetime`)
}

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = Jvm.target
freeCompilerArgs = listOf(
CompilerArguments.inlineClasses,
CompilerArguments.coroutines,
CompilerArguments.time,
CompilerArguments.optIn
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/kotlin/entity/DiscordActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ data class DiscordActivity(
val assets: Optional<DiscordActivityAssets> = Optional.Missing(),
val secrets: Optional<DiscordActivitySecrets> = Optional.Missing(),
val instance: OptionalBoolean = OptionalBoolean.Missing,
val flags: Optional<ActivityFlags> = Optional.Missing()
val flags: Optional<ActivityFlags> = Optional.Missing(),
val buttons: Optional<List<String>> = Optional.Missing()
)

enum class ActivityFlag(val value: Int) {
Expand Down
140 changes: 140 additions & 0 deletions common/src/main/kotlin/entity/DiscordComponent.kt
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)
}
}
4 changes: 2 additions & 2 deletions common/src/main/kotlin/entity/DiscordEmoji.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ data class DiscordUpdatedEmojis(
*/
@Serializable
data class DiscordPartialEmoji(
val id: Snowflake?,
val name: String?,
val id: Snowflake? = null,
val name: String? = null,
val animated: OptionalBoolean = OptionalBoolean.Missing,
)
5 changes: 4 additions & 1 deletion common/src/main/kotlin/entity/DiscordGuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ data class DiscordUnavailableGuild(
* @param approximateMemberCount The approximate number of members in this guild, returned from the `GET /guild/<id>` endpoint when `with_counts` is `true`.
* @param approximatePresenceCount The approximate number of non-offline members in this guild, returned from the `GET /guild/<id>` endpoint when `with_counts` is `true`.
* @param welcomeScreen The welcome screen of a Community guild, shown to new members.
* @param nsfw true if this guild is [designated as NSFW](https://support.discord.com/hc/en-us/articles/1500005389362-NSFW-Server-Designation)
*/
@Serializable
data class DiscordGuild(
Expand Down Expand Up @@ -150,7 +151,9 @@ data class DiscordGuild(
val approximateMemberCount: OptionalInt = OptionalInt.Missing,
@SerialName("approximate_presence_count")
val approximatePresenceCount: OptionalInt = OptionalInt.Missing,

@SerialName("welcome_screen")
val welcomeScreen: Optional<DiscordWelcomeScreen> = Optional.Missing(),
val nsfw: Boolean
)

/**
Expand Down
Loading