Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Option issues should now be fixed + fix to user being null #7
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail authored Mar 18, 2023
2 parents 50ea1a0 + 2c9a2e2 commit 9978048
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Kotlin](https://img.shields.io/badge/kotlin-1.8.10-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![codecov](https://codecov.io/gh/YDWK/YDWK/branch/master/graph/badge.svg?token=LKIA8T6N6J)](https://codecov.io/gh/YDWK/YDWK)
[![yde](https://img.shields.io/badge/YDE--Version-v1.0.5-blue)](https://github.com/YDWK/YDE/releases/tag/v1.0.5)
[![yde](https://img.shields.io/badge/YDE--Version-v1.0.6-blue)](https://github.com/YDWK/YDE/releases/tag/v1.0.6)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
version= 1.0.6-SNAPSHOT
version= 1.0.6

jvmVersion = 1.8.10
pluginAllOpenVersion = 1.8.10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ class InteractionImpl(
return null
}

override val user: User? =
if (json.has("user")) UserImpl(json["user"], json["user"]["id"].asLong(), yde) else null
override val user: User
get() {
return if (json.has("user")) UserImpl(json["user"], json["user"]["id"].asLong(), yde)
else if (json.has("member"))
UserImpl(json["member"]["user"], json["member"]["user"]["id"].asLong(), yde)
else throw IllegalStateException("No user or member found in interaction")
}

override val token: String = json["token"].asText()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class ApplicationCommandImpl(
override val targetId: GetterSnowFlake?
get() = if (json.has("target_id")) GetterSnowFlake.of(json["target_id"].asLong()) else null

override val user: User? = interaction.user
override val user: User = interaction.user

override val member: Member? = interaction.member

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import io.github.ydwk.yde.impl.interaction.application.ApplicationCommandImpl
import io.github.ydwk.yde.impl.interaction.application.ApplicationCommandOptionImpl
import io.github.ydwk.yde.impl.interaction.application.sub.ReplyImpl
import io.github.ydwk.yde.interaction.Interaction
import io.github.ydwk.yde.interaction.application.ApplicationCommandOption
import io.github.ydwk.yde.interaction.application.sub.Reply
import io.github.ydwk.yde.interaction.application.type.SlashCommand
import io.github.ydwk.yde.util.EntityToStringBuilder
Expand All @@ -57,67 +56,85 @@ class SlashCommandImpl(yde: YDE, json: JsonNode, idAsLong: Long, interaction: In
override val options: List<SlashOptionGetter>
get() {
val map: MutableMap<Long, GenericEntity> = mutableMapOf()
val options: JsonNode? = json["options"]
val resolved: JsonNode? = json["resolved"]

if (resolved == null) {
return emptyList()
return if (resolved == null) {
handleOptions(options, map)
} else {
handleResolved(resolved, map, options)
}
}

resolved["users"]?.let {
it.fields().forEach { (id, node) ->
map[id.toLong()] = UserImpl(node, node["id"].asLong(), yde)
private fun handleResolved(
resolved: JsonNode,
map: MutableMap<Long, GenericEntity>,
options: JsonNode?
): List<SlashOptionGetter> {
resolved["users"]?.let {
it.fields().forEach { (id, node) ->
map[id.toLong()] = UserImpl(node, node["id"].asLong(), yde)
}
}
resolved["attachments"]?.let {
it.fields().forEach { (id, node) ->
map[id.toLong()] = AttachmentImpl(yde, node, node["id"].asLong())
}
}

if (guild != null) {
resolved["members"]?.let {
it.fields().forEach { (id, node) ->
resolved["users"]?.let { users ->
val user = users[id]
val member =
MemberImpl(
yde as YDEImpl,
node,
guild,
UserImpl(user, user["id"].asLong(), yde))

val newMember = yde.memberCache.getOrPut(member)
map[id.toLong()] = newMember
}
}
resolved["attachments"]?.let {
it.fields().forEach { (id, node) ->
map[id.toLong()] = AttachmentImpl(yde, node, node["id"].asLong())
}
}

resolved["roles"]?.let {
it.fields().forEach { (id, node) ->
map[id.toLong()] = RoleImpl(yde, node, node["id"].asLong())
}
}

if (guild != null) {
resolved["members"]?.let {
it.fields().forEach { (id, node) ->
resolved["users"]?.let { users ->
val user = users[id]
val member =
MemberImpl(
yde as YDEImpl,
node,
guild,
UserImpl(user, user["id"].asLong(), yde))

val newMember = yde.memberCache.getOrPut(member)
map[id.toLong()] = newMember
}
}
resolved["channels"]?.let {
it.fields().forEach { (id, node) ->
val channelType = ChannelType.fromInt(node["type"].asInt())
if (ChannelType.isGuildChannel(channelType)) {
map[id.toLong()] = GuildChannelImpl(yde, node, node["id"].asLong())
} else {
map[id.toLong()] = DmChannelImpl(yde, node, node["id"].asLong())
}
}
}
}

resolved["roles"]?.let {
it.fields().forEach { (id, node) ->
map[id.toLong()] = RoleImpl(yde, node, node["id"].asLong())
}
}
return handleOptions(options, map)
}

resolved["channels"]?.let {
it.fields().forEach { (id, node) ->
val channelType = ChannelType.fromInt(node["type"].asInt())
if (ChannelType.isGuildChannel(channelType)) {
map[id.toLong()] = GuildChannelImpl(yde, node, node["id"].asLong())
} else {
map[id.toLong()] = DmChannelImpl(yde, node, node["id"].asLong())
}
}
}
}
private fun handleOptions(
options: JsonNode?,
map: MutableMap<Long, GenericEntity>
): List<SlashOptionGetter> {
val list: MutableList<SlashOptionGetter> = mutableListOf()

return applicationOptions?.map { SlashOptionGetterImpl(it, map) } ?: emptyList()
}
options?.forEach { node ->
val option = ApplicationCommandOptionImpl(yde, node)
list.add(SlashOptionGetterImpl(option, map))
}
?: return emptyList()

// ignore
private val applicationOptions: List<ApplicationCommandOption>? =
if (json.has("options")) json["options"].map { ApplicationCommandOptionImpl(yde, it) }
else null
return list
}

override fun toString(): String {
return EntityToStringBuilder(yde, this).name(this.name).toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ interface Interaction : SnowFlake, GenericEntity {
*
* @return The user who invoked this interaction.
*/
val user: User?
val user: User

/**
* The token of this interaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ interface ApplicationCommand : SnowFlake, GenericCommandData {
*
* @return The user who invoked the command.
*/
val user: User?
val user: User

/**
* The member who invoked the command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,16 @@ interface SlashCommand : ApplicationCommand, Repliable {
val option = getOption(name)
return if (option == null) null else resolver(option)
}

/**
* The option with the specified name.
*
* @param name The name of the option.
* @param resolver The resolver to use.
* @param default The default value to return if the option is not present.
*/
fun <T> getOption(name: String, resolver: (SlashOptionGetter) -> T, default: T): T {
val option = getOption(name)
return if (option == null) default else resolver(option)
}
}

0 comments on commit 9978048

Please sign in to comment.