From dce1d2a4c74dfe566c315c4b9dacc05d8161d7e9 Mon Sep 17 00:00:00 2001 From: alluding <149972831+alluding@users.noreply.github.com> Date: Fri, 5 Apr 2024 07:49:06 -0400 Subject: [PATCH] update `position` Update `position` to efficiently handle larger guilds, since previously it sorted the entire list of guild members before indexing for the current member, which could become problematic. --- discord/member.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/discord/member.py b/discord/member.py index 1b6cc10..b2b93a4 100644 --- a/discord/member.py +++ b/discord/member.py @@ -487,8 +487,20 @@ def raw_status(self) -> str: @property def position(self) -> int: - """ Integer: the member's join position in the guild """ - return sorted(self.guild.members,key=lambda x: x.joined_at).index(self)+1 + """Returns the join position of the member in the guild. + + If the member's join position cannot be determined (e.g., if the member is not part + of any guild), it returns 0. + + Returns: + int: The join position of the member in the guild. + """ + guild: Guild | None = getattr(self, "guild", None) + return ( + 0 + if not guild + else sum(m.joined_at < self.joined_at for m in guild.members) + 1 + ) @status.setter def status(self, value: Status) -> None: