Skip to content

Commit

Permalink
Add endpoint to search members
Browse files Browse the repository at this point in the history
  • Loading branch information
BartArys committed Nov 27, 2020
1 parent 75bc038 commit e234983
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.gitlab.kordlib.core.behavior

import com.gitlab.kordlib.cache.api.query
import com.gitlab.kordlib.common.annotation.DeprecatedSinceKord
import com.gitlab.kordlib.common.annotation.KordExperimental
import com.gitlab.kordlib.common.entity.DiscordUser
import com.gitlab.kordlib.common.entity.Snowflake
import com.gitlab.kordlib.common.entity.optional.Optional
import com.gitlab.kordlib.common.entity.optional.unwrap
import com.gitlab.kordlib.common.exception.RequestException
import com.gitlab.kordlib.core.Kord
import com.gitlab.kordlib.core.cache.data.*
Expand Down Expand Up @@ -150,7 +153,7 @@ interface GuildBehavior : Entity, Strategizable {
*/
val voiceStates: Flow<VoiceState>
get() = kord.cache
.query<VoiceStateData> { idEq( VoiceStateData::guildId, id) }
.query<VoiceStateData> { idEq(VoiceStateData::guildId, id) }
.asFlow()
.map { VoiceState(it, kord) }

Expand Down Expand Up @@ -249,6 +252,28 @@ interface GuildBehavior : Entity, Strategizable {
*/
suspend fun getMember(userId: Snowflake): Member = supplier.getMember(id, userId)

/**
* Requests to get up to [limit] members whose [Member.username] or [Member.nickname] match the [query].
* The [limit] accepts a maximum value of `1000` and a minimum of `1`.
*
* This property is not resolvable through cache and will always use the [RestClient] instead.
*
* The returned flow is lazily executed, any [RequestException] will be thrown on
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*
* This function is not part of the officially documented Discord API and may be removed/altered/stop working in the future.
*/
@KordExperimental
suspend fun getMembers(query: String, limit: Int = 1000): Flow<Member> = flow {
kord.rest.guild.getGuildMembers(id, query, limit).forEach {
emit(Member(
MemberData.from(userId = it.user.unwrap(DiscordUser::id)!!, guildId = id, it),
UserData.from(it.user.value!!),
kord
))
}
}

/**
* Requests to get the [Member] represented by the [userId],
* returns null if the [Member] isn't present.
Expand Down
5 changes: 5 additions & 0 deletions rest/src/main/kotlin/com/gitlab/kordlib/rest/route/Route.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gitlab.kordlib.rest.route

import com.gitlab.kordlib.common.annotation.DeprecatedSinceKord
import com.gitlab.kordlib.common.annotation.KordExperimental
import com.gitlab.kordlib.common.annotation.KordPreview
import com.gitlab.kordlib.common.entity.*
import com.gitlab.kordlib.rest.json.optional
Expand Down Expand Up @@ -183,6 +184,10 @@ sealed class Route<T>(
object GuildMembersGet
: Route<List<DiscordGuildMember>>(HttpMethod.Get, "/guilds/$GuildId/members", ListSerializer(DiscordGuildMember.serializer()))

@KordExperimental
object GuildMembersSearchGet //https://github.com/discord/discord-api-docs/pull/1577
: Route<List<DiscordGuildMember>>(HttpMethod.Get, "/guilds/$GuildId/members/search", ListSerializer(DiscordGuildMember.serializer()))

object GuildMemberPut
: Route<DiscordGuildMember?>(HttpMethod.Put, "/guilds/$GuildId/members/$UserId", DiscordGuildMember.serializer().optional)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gitlab.kordlib.rest.service

import com.gitlab.kordlib.common.annotation.DeprecatedSinceKord
import com.gitlab.kordlib.common.annotation.KordExperimental
import com.gitlab.kordlib.common.entity.*
import com.gitlab.kordlib.rest.builder.ban.BanCreateBuilder
import com.gitlab.kordlib.rest.builder.channel.*
Expand Down Expand Up @@ -104,6 +105,18 @@ class GuildService(requestHandler: RequestHandler) : RestService(requestHandler)
parameter("limit", "$limit")
}

/**
* Requests members with a username or nickname matching [query].
*
* @param limit limits the maximum amount of members returned. Max `1000`, defaults to `1`.
*/
@KordExperimental
suspend fun getGuildMembers(guildId: Snowflake, query: String, limit: Int = 1) = call(Route.GuildMembersSearchGet) {
keys[Route.GuildId] = guildId
parameter("query", query)
parameter("limit", "$limit")
}

suspend fun addGuildMember(guildId: Snowflake, userId: Snowflake, token: String, builder: MemberAddBuilder.() -> Unit) = call(Route.GuildMemberPut) {
keys[Route.GuildId] = guildId
keys[Route.UserId] = userId
Expand Down

0 comments on commit e234983

Please sign in to comment.