Skip to content

Commit

Permalink
Fix exception when registering commands without contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 17, 2024
1 parent e979fd9 commit b6d2586
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions common/src/commonMain/kotlin/entity/optional/Optional.kt
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,8 @@ public fun Optional<Boolean>.toPrimitive(): OptionalBoolean = when (this) {
is Value -> OptionalBoolean.Value(value)
is Missing, is Null<*> -> OptionalBoolean.Missing
}

public fun <T> Optional<List<T>>.missingOnEmpty(): Optional<List<T>> = when (this) {
is Value -> if (value.isEmpty()) Missing() else this
is Missing, is Null<*> -> Missing()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.kord.common.entity.Permissions
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.mapList
import dev.kord.common.entity.optional.missingOnEmpty
import dev.kord.rest.json.request.ApplicationCommandCreateRequest
import dev.kord.rest.json.request.ApplicationCommandModifyRequest
import kotlin.contracts.InvocationKind
Expand Down Expand Up @@ -171,8 +172,8 @@ internal class ChatInputCreateBuilderImpl(
state.defaultMemberPermissions,
state.dmPermission,
@Suppress("DEPRECATION") state.defaultPermission,
integrationTypes = Optional.missingOnEmpty(state.integrationTypes),
contexts = missingOnEmpty(state.contexts),
integrationTypes = state.integrationTypes.missingOnEmpty(),
contexts = state.contexts.missingOnEmpty(),
nsfw = state.nsfw,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.kord.common.entity.ApplicationIntegrationType
import dev.kord.common.entity.InteractionContextType
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.missingOnEmpty
import dev.kord.rest.json.request.ApplicationCommandCreateRequest
import dev.kord.rest.json.request.ApplicationCommandModifyRequest

Expand Down Expand Up @@ -86,8 +87,8 @@ internal class MessageCommandCreateBuilderImpl(override var name: String) : Glob
dmPermission = state.dmPermission,
defaultMemberPermissions = state.defaultMemberPermissions,
defaultPermission = @Suppress("DEPRECATION") state.defaultPermission,
integrationTypes = state.integrationTypes,
contexts = state.contexts,
integrationTypes = state.integrationTypes.missingOnEmpty(),
contexts = state.contexts.missingOnEmpty(),
nsfw = state.nsfw,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.kord.common.entity.ApplicationIntegrationType
import dev.kord.common.entity.InteractionContextType
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.missingOnEmpty
import dev.kord.rest.json.request.ApplicationCommandCreateRequest
import dev.kord.rest.json.request.ApplicationCommandModifyRequest

Expand Down Expand Up @@ -81,8 +82,8 @@ internal class UserCommandCreateBuilderImpl(override var name: String) : GlobalU
defaultMemberPermissions = state.defaultMemberPermissions,
dmPermission = state.dmPermission,
defaultPermission = @Suppress("DEPRECATION") state.defaultPermission,
integrationTypes = state.integrationTypes,
contexts = state.contexts,
integrationTypes = state.integrationTypes.missingOnEmpty(),
contexts = state.contexts.missingOnEmpty(),
nsfw = state.nsfw,
)
}
Expand Down

0 comments on commit b6d2586

Please sign in to comment.