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

Application commands improvements #374

Merged
merged 8 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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 common/src/main/kotlin/entity/Interactions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ sealed class InteractionType(val type: Int) {
data class InteractionCallbackData(
val id: OptionalSnowflake = OptionalSnowflake.Missing,
val type: Optional<ApplicationCommandType> = Optional.Missing(),
@SerialName("target_user")
val targetUser: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("target_id")
val targetId: OptionalSnowflake = OptionalSnowflake.Missing,
val name: Optional<String> = Optional.Missing(),
val resolved: Optional<ResolvedObjects> = Optional.Missing(),
val options: Optional<List<Option>> = Optional.Missing(),
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/kotlin/cache/data/ComponentData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ data class ComponentData(
components.mapList { from(it) },
placeholder = placeholder,
minValues = minValues,
maxValues = maxValues
maxValues = maxValues,
options = options.mapList { SelectOptionData.from(it) }
)
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/cache/data/InteractionData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ data class ResolvedObjectsData(
data class ApplicationInteractionData(
val id: OptionalSnowflake = OptionalSnowflake.Missing,
val type: Optional<ApplicationCommandType> = Optional.Missing(),
val targetUser: OptionalSnowflake = OptionalSnowflake.Missing,
val targetId: OptionalSnowflake = OptionalSnowflake.Missing,
val name: Optional<String> = Optional.Missing(),
val options: Optional<List<OptionData>> = Optional.Missing(),
val resolvedObjectsData: Optional<ResolvedObjectsData> = Optional.Missing(),
Expand All @@ -99,7 +99,7 @@ data class ApplicationInteractionData(
ApplicationInteractionData(
id,
type,
targetUser,
targetId,
name,
options.map { it.map { OptionData.from(it) } },
resolved.map { ResolvedObjectsData.from(it, guildId) },
Expand Down
23 changes: 18 additions & 5 deletions core/src/main/kotlin/entity/interaction/ContextInteraction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import dev.kord.core.Kord
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.interaction.ApplicationCommandInteractionBehavior
import dev.kord.core.cache.data.InteractionData
import dev.kord.core.entity.Member
import dev.kord.core.entity.Message
import dev.kord.core.entity.User
import dev.kord.core.entity.application.GuildUserCommand
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import java.util.*
Expand Down Expand Up @@ -85,7 +82,15 @@ class GlobalChatInputCommandInteraction(
*/
sealed interface UserCommandInteraction : ApplicationCommandInteraction {

val users get() = resolvedObjects!!.users
val targetId: Snowflake get() = data.data.targetId.value!!

val targetBehavior: UserBehavior get() = UserBehavior(targetId, kord)

suspend fun getTarget(): User = supplier.getUser(targetId)

suspend fun getTargetOrNull(): User? = supplier.getUserOrNull(targetId)

val users get() = resolvedObjects!!.users!!
}

/**
Expand Down Expand Up @@ -129,7 +134,15 @@ class GlobalUserCommandInteraction(
*/
sealed interface MessageCommandInteraction : ApplicationCommandInteraction {

val messages get() = resolvedObjects!!.messages
val targetId: Snowflake get() = data.data.targetId.value!!

val targetBehavior: UserBehavior get() = UserBehavior(targetId, kord)

suspend fun getTarget(): User = supplier.getUser(targetId)

suspend fun getTargetOrNull(): User? = supplier.getUserOrNull(targetId)

val messages get() = resolvedObjects!!.messages!!

}

Expand Down
10 changes: 10 additions & 0 deletions core/src/main/kotlin/entity/interaction/Interaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ sealed interface InteractionCommand : KordObject {

val booleans: Map<String, Boolean> get() = filterOptions()

val users: Map<String, User> get() = filterOptions()

val members: Map<String, Member> get() = filterOptions()

val channels: Map<String, ResolvedChannel> get() = filterOptions()

val roles: Map<String, Role> get() = filterOptions()

val mentionables: Map<String, Entity> get() = filterOptions()
Comment on lines +122 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't these keys represent Snowflakes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those could be shorthanded from resolved. I think last time we talked about this we said that not all resolved values are in options

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would work fine, each of those should be a part of options


private inline fun <reified T> filterOptions(): Map<String, T> {
return buildMap {
options.onEach { (key, value) ->
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/gateway/handler/ThreadEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ThreadEventHandler(
is ChannelType.PublicNewsThread -> NewsChannelThreadDeleteEvent(channel, old as? NewsChannelThread, shard)
is ChannelType.PrivateThread,
is ChannelType.GuildText -> TextChannelThreadDeleteEvent(channel, old as? TextChannelThread, shard)
else -> UnknownChannelThreadDeleteEvent(channel, old as ThreadChannel, shard)
else -> UnknownChannelThreadDeleteEvent(channel, old as? ThreadChannel, shard)
}
coreFlow.emit(coreEvent)

Expand Down Expand Up @@ -118,4 +118,4 @@ class ThreadEventHandler(
val coreEvent = ThreadMembersUpdateEvent(data, kord, shard)
coreFlow.emit(coreEvent)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ class MultiApplicationCommandBuilder {
val commands = mutableListOf<ApplicationCommandCreateBuilder>()

@OptIn(ExperimentalContracts::class)
inline fun message(name: String, builder: MessageCommandCreateBuilder.() -> Unit) {
inline fun message(name: String, builder: MessageCommandCreateBuilder.() -> Unit = {}) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
commands += MessageCommandCreateBuilder(name).apply(builder)
}

@OptIn(ExperimentalContracts::class)
inline fun user(name: String, builder: UserCommandCreateBuilder.() -> Unit) {
inline fun user(name: String, builder: UserCommandCreateBuilder.() -> Unit = {}) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
commands += UserCommandCreateBuilder(name).apply(builder)
}
@OptIn(ExperimentalContracts::class)
inline fun input(
name: String,
description: String,
builder: ChatInputCreateBuilder.() -> Unit) {
builder: ChatInputCreateBuilder.() -> Unit = {}) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
commands += ChatInputCreateBuilder(name, description).apply(builder)
}
Expand Down