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 2 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
4 changes: 2 additions & 2 deletions 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.21"))
implementation(kotlin("gradle-plugin-api", version = "1.5.0"))
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

implementation(gradleApi())
implementation(localGroovy())
}
}
5 changes: 2 additions & 3 deletions common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ data class DiscordMessage(
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("referenced_message")
val referencedMessage: Optional<DiscordMessage?> = Optional.Missing(),
val interaction: Optional<DiscordMessageInteraction> = Optional.Missing(),

/*
* don't trust the docs:
* This is a list even though the docs say it's a component
*/
val components: Optional<List<DiscordComponent>> = Optional.Missing()
val components: Optional<List<DiscordComponent>> = Optional.Missing(),
val interaction: Optional<DiscordMessageInteraction> = Optional.Missing()
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ fun GuildApplicationCommandBehavior(
get() = id
}


@KordPreview
fun GlobalApplicationCommandBehavior(
applicationId: Snowflake,
Expand All @@ -124,4 +123,4 @@ fun GlobalApplicationCommandBehavior(
get() = service
override val id: Snowflake
get() = id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import dev.kord.common.entity.MessageFlags
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.mapList
import dev.kord.common.entity.optional.optional
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.component.ActionRowBuilder
import dev.kord.rest.builder.component.MessageComponentBuilder
import dev.kord.rest.builder.message.AllowedMentionsBuilder
Expand All @@ -28,7 +26,6 @@ class EphemeralInteractionResponseModifyBuilder : BaseInteractionResponseModifyB

override val embeds: MutableList<EmbedBuilder> = mutableListOf()


private var _allowedMentions: Optional<AllowedMentionsBuilder> = Optional.Missing()
override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()

Expand All @@ -37,7 +34,7 @@ class EphemeralInteractionResponseModifyBuilder : BaseInteractionResponseModifyB
@OptIn(ExperimentalContracts::class)
inline fun allowedMentions(builder: AllowedMentionsBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
allowedMentions = AllowedMentionsBuilder().apply(builder).build()
allowedMentions = (allowedMentions ?: AllowedMentionsBuilder()).apply(builder)
}

@OptIn(ExperimentalContracts::class)
Expand All @@ -50,13 +47,15 @@ class EphemeralInteractionResponseModifyBuilder : BaseInteractionResponseModifyB
components.add(ActionRowBuilder().apply(builder))
}

override fun toRequest(): InteractionResponseModifyRequest {
return InteractionResponseModifyRequest(
content = _content,
allowedMentions = _allowedMentions,
components = Optional.missingOnEmpty(components.map { it.build() })
override fun toRequest(): MultipartInteractionResponseModifyRequest {
return MultipartInteractionResponseModifyRequest(
InteractionResponseModifyRequest(
content = _content,
allowedMentions = _allowedMentions.map { it.build() },
components = Optional.missingOnEmpty(components.map { it.build() }),
embeds = embeds.map { it.toRequest() }
)
)

}
}

Expand All @@ -82,7 +81,7 @@ class EphemeralInteractionResponseCreateBuilder : BaseInteractionResponseCreateB
val data = InteractionApplicationCommandCallbackData(
content = _content,
flags = flags,
embeds = embeds.map { it.toRequest() }
embeds = Optional.missingOnEmpty(embeds.map { it.toRequest() })
)
return MultipartInteractionResponseCreateRequest(
InteractionResponseCreateRequest(type, data.optional())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import dev.kord.common.entity.InteractionResponseType
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.RequestBuilder
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.optional
import dev.kord.rest.builder.component.ActionRowBuilder
import dev.kord.rest.builder.component.MessageComponentBuilder
import dev.kord.rest.builder.message.AllowedMentionsBuilder
Expand All @@ -17,11 +18,14 @@ import kotlinx.coroutines.withContext
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@KordPreview
@KordDsl
class PublicInteractionResponseCreateBuilder :
BaseInteractionResponseCreateBuilder {
BaseInteractionResponseCreateBuilder {
private var _content: Optional<String> = Optional.Missing()
override var content: String? by ::_content.delegate()

Expand All @@ -40,7 +44,6 @@ class PublicInteractionResponseCreateBuilder :
val files: MutableList<Pair<String, InputStream>> = mutableListOf()



/**
* Configures the mentions that should trigger a mention (aka ping). Not calling this function will result in the default behavior
* (ping everything), calling this function but not configuring it before the request is build will result in all
Expand Down Expand Up @@ -87,7 +90,7 @@ class PublicInteractionResponseCreateBuilder :
type,
InteractionApplicationCommandCallbackData(
content = _content,
embeds = embeds.map { it.toRequest() },
embeds = Optional.missingOnEmpty(embeds.map { it.toRequest() }),
allowedMentions = _allowedMentions.map { it.build() },
tts = _tts,
components = Optional.missingOnEmpty(components.map { it.build() })
Expand All @@ -106,7 +109,7 @@ class PublicInteractionResponseModifyBuilder :
private var _content: Optional<String> = Optional.Missing()
override var content: String? by ::_content.delegate()

override var embeds: MutableList<EmbedBuilder> = mutableListOf()
override val embeds: MutableList<EmbedBuilder> = mutableListOf()

private var _allowedMentions: Optional<AllowedMentionsBuilder> = Optional.Missing()
override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()
Expand All @@ -115,26 +118,6 @@ class PublicInteractionResponseModifyBuilder :

val components: MutableList<MessageComponentBuilder> = mutableListOf()

/**
* Configures the mentions that should trigger a mention (aka ping). Not calling this function will result in the default behavior
* (ping everything), calling this function but not configuring it before the request is build will result in all
* pings being ignored.
*/
@OptIn(ExperimentalContracts::class)
inline fun allowedMentions(block: AllowedMentionsBuilder.() -> Unit = {}) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
allowedMentions = (allowedMentions ?: AllowedMentionsBuilder()).apply(block)
}



@OptIn(ExperimentalContracts::class)
inline fun embed(builder: EmbedBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
if (embeds == null) embeds = mutableListOf()
embeds!! += EmbedBuilder().apply(builder)
}

fun addFile(name: String, content: InputStream) {
files += name to content
}
Expand Down
2 changes: 1 addition & 1 deletion rest/src/main/kotlin/builder/message/EmbedBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class EmbedBuilder : RequestBuilder<EmbedRequest> {
Optional.Value("embed"),
_description,
_url,
_timestamp.map { it.toString() },
_timestamp,
_color,
_footer.map { it.toRequest() },
_image.map { EmbedImageRequest(it) },
Expand Down
2 changes: 1 addition & 1 deletion rest/src/main/kotlin/json/request/InteractionsRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ data class MultipartInteractionResponseCreateRequest(
class InteractionApplicationCommandCallbackData(
val tts: OptionalBoolean = OptionalBoolean.Missing,
val content: Optional<String> = Optional.Missing(),
val embeds: List<EmbedRequest> = emptyList(),
val embeds: Optional<List<EmbedRequest>> = Optional.Missing(),
@SerialName("allowed_mentions")
val allowedMentions: Optional<AllowedMentions> = Optional.Missing(),
val flags: Optional<MessageFlags> = Optional.Missing(),
Expand Down