Skip to content

Commit

Permalink
add mentionable support (#333)
Browse files Browse the repository at this point in the history
* allow creating mentionables

* Fix incorrect parsing of mentionables

* Update changelog

* clarify fixes
  • Loading branch information
HopeBaron committed Jun 24, 2021
1 parent 188a492 commit ced4fe9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 0.7.2

## Additions

* Allow adding a mentionable argument to commands

## Fixes

* Mentionable arguments now correctly retrieve their entity.

# 0.7.1

## Breaking
Expand Down
11 changes: 8 additions & 3 deletions core/src/main/kotlin/entity/interaction/Interaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ fun OptionValue(value: CommandArgument<*>, resolvedObjects: ResolvedObjects?): O

is CommandArgument.MentionableArgument -> {
val channel = resolvedObjects?.channels.orEmpty()[value.value]
val user = resolvedObjects?.channels.orEmpty()[value.value]
val user = resolvedObjects?.users.orEmpty()[value.value]
val member = resolvedObjects?.members.orEmpty()[value.value]
val role = resolvedObjects?.members.orEmpty()[value.value]
val role = resolvedObjects?.roles.orEmpty()[value.value]

OptionValue.MentionableOptionValue((channel ?: user ?: member ?: role)!!)
OptionValue.MentionableOptionValue((channel ?: member ?: user ?: role)!!)
}

is CommandArgument.RoleArgument -> {
Expand Down Expand Up @@ -459,3 +459,8 @@ fun OptionValue<*>.boolean() = value as Boolean

@KordPreview
fun OptionValue<*>.int() = value as Int

@KordPreview
fun OptionValue<*>.mentionable(): Entity {
return value as Entity
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ sealed class BaseApplicationBuilder {
if (options == null) options = mutableListOf()
options!!.add(ChannelBuilder(name, description).apply(builder))
}

@OptIn(ExperimentalContracts::class)
inline fun mentionable(name: String, description: String, builder: MentionableBuilder.() -> Unit = {}) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
if (options == null) options = mutableListOf()
options!!.add(MentionableBuilder(name, description).apply(builder))

}

}


Expand All @@ -102,7 +111,8 @@ class ApplicationCommandCreateBuilder(
var defaultPermission: Boolean? by ::_defaultPermission.delegate()

override fun toRequest(): ApplicationCommandCreateRequest {
return ApplicationCommandCreateRequest(name,
return ApplicationCommandCreateRequest(
name,
description,
_options.mapList { it.toRequest() }, _defaultPermission
)
Expand Down Expand Up @@ -154,7 +164,7 @@ class ApplicationCommandModifyBuilder : BaseApplicationBuilder(),
return ApplicationCommandModifyRequest(
_name,
_description,
_options.mapList { it.toRequest() },
_options.mapList { it.toRequest() },
_defaultPermission
)

Expand Down
4 changes: 4 additions & 0 deletions rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class RoleBuilder(name: String, description: String) :
class ChannelBuilder(name: String, description: String) :
OptionsBuilder(name, description, ApplicationCommandOptionType.Channel)

@KordPreview
class MentionableBuilder(name: String, description: String) :
OptionsBuilder(name, description, ApplicationCommandOptionType.Mentionable)

@KordDsl
@KordPreview
class MentionableBuilder(name: String, description: String) :
Expand Down

0 comments on commit ced4fe9

Please sign in to comment.