Skip to content

Commit

Permalink
Fix getGuildMembers for suppliers
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron committed Jun 25, 2020
1 parent a3aa6d6 commit 88cc780
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.5.3

## Fixes

* `getGuildMembers` returns the correct limit for both Cache and Rest suppliers.

# 0.5.2
## Additions
* Add missing mention property for unicode emojis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ interface GuildBehavior : Entity, Strategizable {
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*/
val members: Flow<Member>
get() = supplier.getGuildMembers(id, 1000)
get() = supplier.getGuildMembers(id)

/**
* Requests to get the present voice regions for this guild.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ class CacheEntitySupplier(private val kord: Kord) : EntitySupplier {
}.asFlow().map { Ban(it, kord) }

override fun getGuildMembers(guildId: Snowflake, limit: Int): Flow<Member> {
require(limit > 0) { "At least 1 item should be requested, but got $limit." }
return kord.cache.query<UserData>().asFlow().flatMapConcat { userData ->
kord.cache.query<MemberData> {
MemberData::userId eq userData.id
MemberData::guildId eq guildId
}.asFlow().map { Member(it, userData, kord) }
}.asFlow().map { Member(it, userData, kord) }.take(limit)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gitlab.kordlib.core.supplier

import com.gitlab.kordlib.common.entity.DiscordGuildMember
import com.gitlab.kordlib.common.entity.DiscordPartialGuild
import com.gitlab.kordlib.common.entity.Snowflake
import com.gitlab.kordlib.core.Kord
Expand Down Expand Up @@ -88,8 +89,9 @@ class RestEntitySupplier(val kord: Kord) : EntitySupplier {
}

override suspend fun getMemberOrNull(guildId: Snowflake, userId: Snowflake): Member? = catchNotFound {
val memberData = guild.getGuildMember(guildId = guildId.value, userId = userId.value).toData(guildId = guildId.value, userId = userId.value)
val userData = user.getUser(userId.value).toData()
val member = guild.getGuildMember(guildId = guildId.value, userId = userId.value)
val memberData = member.toData(guildId = guildId.value, userId = userId.value)
val userData = member.user!!.toData()
return Member(memberData, userData, kord)
}

Expand Down Expand Up @@ -158,11 +160,24 @@ class RestEntitySupplier(val kord: Kord) : EntitySupplier {
emit(Ban(BanData.from(guildId.value, banData), kord))
}

override fun getGuildMembers(guildId: Snowflake, limit: Int): Flow<Member> = flow {
for (memberData in guild.getGuildMembers(guildId.value))
emit(Member(memberData.toData(memberData.user!!.id, guildId.value), memberData.user!!.toData(), kord))
override fun getGuildMembers(guildId: Snowflake, limit: Int): Flow<Member> {
require(limit > 0) { "At least 1 item should be requested, but got $limit." }
val batchSize = min(1000, limit)

val flow = paginateForwards(idSelector = { it.user!!.id }, batchSize = batchSize) { position ->
kord.rest.guild.getGuildMembers(guildId = guildId.value, position = position, limit = batchSize)
}.map {
val userData = it.user!!.toData()
val memberData = it.toData(guildId = guildId.value, userId = it.user!!.id)
Member(memberData, userData, kord)
}


return if (limit != Int.MAX_VALUE) flow.take(limit)
else flow
}


override fun getGuildVoiceRegions(guildId: Snowflake): Flow<Region> = flow {
for (region in guild.getGuildVoiceRegions(guildId.value)) {
val data = RegionData.from(guildId.value, region)
Expand Down

0 comments on commit 88cc780

Please sign in to comment.