Skip to content

Commit

Permalink
Add initial draft for entry point commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Oct 22, 2024
1 parent a6a8e6d commit 5e242fa
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public sealed class ApplicationCommandType(
*/
public object Message : ApplicationCommandType(3)

/**
* A UI-based command that represents the primary way to invoke an app's
* [Activity](https://discord.com/developers/docs/activities/overview)
*/
public object PrimaryEntryPoint : ApplicationCommandType(4)

internal object Serializer : KSerializer<ApplicationCommandType> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.kord.common.entity.ApplicationCommandType",
Expand All @@ -79,6 +85,7 @@ public sealed class ApplicationCommandType(
ChatInput,
User,
Message,
PrimaryEntryPoint,
)
}

Expand All @@ -90,6 +97,7 @@ public sealed class ApplicationCommandType(
1 -> ChatInput
2 -> User
3 -> Message
4 -> PrimaryEntryPoint
else -> Unknown(value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
@file:Suppress(names = arrayOf("IncorrectFormatting", "ReplaceArrayOfWithLiteral",
"SpellCheckingInspection", "GrazieInspection"))

package dev.kord.common.entity

import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlinx.serialization.KSerializer
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

/**
* See [EntryPointCommandHandlerType]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/interactions/application-commands#application-command-object-entry-point-command-handler-types).
*/
@Serializable(with = EntryPointCommandHandlerType.Serializer::class)
public sealed class EntryPointCommandHandlerType(
/**
* The raw value used by Discord.
*/
public val `value`: Int,
) {
final override fun equals(other: Any?): Boolean = this === other ||
(other is EntryPointCommandHandlerType && this.value == other.value)

final override fun hashCode(): Int = value.hashCode()

final override fun toString(): String =
if (this is Unknown) "EntryPointCommandHandlerType.Unknown(value=$value)"
else "EntryPointCommandHandlerType.${this::class.simpleName}"

/**
* An unknown [EntryPointCommandHandlerType].
*
* This is used as a fallback for [EntryPointCommandHandlerType]s that haven't been added to
* Kord yet.
*/
public class Unknown internal constructor(
`value`: Int,
) : EntryPointCommandHandlerType(value)

/**
* The app handles the interaction using an interaction token
*/
public object AppHandler : EntryPointCommandHandlerType(1)

/**
* Discord handles the interaction by launching an Activity and sending a follow-up message
* without coordinating with the app
*/
public object DiscordLaunchActivity : EntryPointCommandHandlerType(2)

internal object Serializer : KSerializer<EntryPointCommandHandlerType> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.kord.common.entity.EntryPointCommandHandlerType",
PrimitiveKind.INT)

override fun serialize(encoder: Encoder, `value`: EntryPointCommandHandlerType) {
encoder.encodeInt(value.value)
}

override fun deserialize(decoder: Decoder): EntryPointCommandHandlerType =
from(decoder.decodeInt())
}

public companion object {
/**
* A [List] of all known [EntryPointCommandHandlerType]s.
*/
public val entries: List<EntryPointCommandHandlerType> by lazy(mode = PUBLICATION) {
listOf(
AppHandler,
DiscordLaunchActivity,
)
}

/**
* Returns an instance of [EntryPointCommandHandlerType] with
* [EntryPointCommandHandlerType.value] equal to the specified [value].
*/
public fun from(`value`: Int): EntryPointCommandHandlerType = when (value) {
1 -> AppHandler
2 -> DiscordLaunchActivity
else -> Unknown(value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public sealed class InteractionResponseType(
*/
public object Modal : InteractionResponseType(9)

/**
* Launch the Activity associated with the app. Only available for apps with Activities enabled
*/
public object LaunchActivity : InteractionResponseType(12)

internal object Serializer : KSerializer<InteractionResponseType> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.kord.common.entity.InteractionResponseType",
Expand All @@ -105,6 +110,7 @@ public sealed class InteractionResponseType(
UpdateMessage,
ApplicationCommandAutoCompleteResult,
Modal,
LaunchActivity,
)
}

Expand All @@ -120,6 +126,7 @@ public sealed class InteractionResponseType(
7 -> UpdateMessage
8 -> ApplicationCommandAutoCompleteResult
9 -> Modal
12 -> LaunchActivity
else -> Unknown(type)
}
}
Expand Down
17 changes: 17 additions & 0 deletions common/src/commonMain/kotlin/entity/Interactions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@
"Message", intValue = 3,
kDoc = "A UI-based command that shows up when you right-click or tap on a message.",
),
Entry(
"PrimaryEntryPoint", intValue = 4,
kDoc = "A UI-based command that represents the primary way to invoke an app's [Activity](https://discord.com/developers/docs/activities/overview)"
)
],
)

@file:Generate(
INT_KORD_ENUM, name = "EntryPointCommandHandlerType",
docUrl = "https://discord.com/developers/docs/interactions/application-commands#application-command-object-entry-point-command-handler-types",
entries = [
Entry("AppHandler", intValue = 1, kDoc = "The app handles the interaction using an interaction token"),
Entry(
"DiscordLaunchActivity", intValue = 2,
kDoc = "Discord handles the interaction by launching an Activity and sending a follow-up message without coordinating with the app"
)
]
)

@file:Generate(
INT_KORD_ENUM, name = "ApplicationCommandOptionType", valueName = "type",
docUrl = "https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-type",
Expand Down Expand Up @@ -62,6 +78,7 @@
kDoc = "Respond to an autocomplete interaction with suggested choices.",
),
Entry("Modal", intValue = 9, kDoc = "Respond to an interaction with a popup modal."),
Entry("LaunchActivity", intValue = 12, kDoc = "\tLaunch the Activity associated with the app. Only available for apps with Activities enabled")
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package dev.kord.core.behavior.interaction.response

import dev.kord.common.entity.InteractionResponseType
import dev.kord.common.entity.Snowflake
import dev.kord.core.KordObject
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.interaction.Interaction
import dev.kord.core.entity.interaction.followup.FollowupMessage
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.json.request.InteractionResponseCreateRequest
import dev.kord.rest.request.RestRequestException

/**
Expand Down Expand Up @@ -44,5 +46,16 @@ public sealed interface InteractionResponseBehavior : KordObject, Strategizable
public suspend fun getFollowupMessage(messageId: Snowflake): FollowupMessage =
supplier.getFollowupMessage(applicationId, token, messageId)

/**
* Opens the [Activity](https://discord.com/developers/docs/activities/overview) of this application.
* **Note:** This requires activities to be enabled for this application
*/
public suspend fun openActivity() {
kord.rest.interaction.createInteractionResponse(
applicationId, token,
InteractionResponseCreateRequest(InteractionResponseType.LaunchActivity)
)
}

override fun withStrategy(strategy: EntitySupplyStrategy<*>): InteractionResponseBehavior
}

0 comments on commit 5e242fa

Please sign in to comment.