Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always set Guild._member_count #2145

Merged
merged 6 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2091](https://github.com/Pycord-Development/pycord/pull/2091))
- Fixed `AttributeError` when accessing a `Select`'s values when it hasn't been
interacted with. ([#2104](https://github.com/Pycord-Development/pycord/pull/2104))
- Fixed `Guild._member_count` sometimes not being set.
([#2145](https://github.com/Pycord-Development/pycord/pull/2145))
- Fixed `Thread.applied_tags` not being updated.
([#2146](https://github.com/Pycord-Development/pycord/pull/2146))

Expand Down
24 changes: 10 additions & 14 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def __repr__(self) -> str:
("name", self.name),
("shard_id", self.shard_id),
("chunked", self.chunked),
("member_count", getattr(self, "_member_count", None)),
("member_count", self._member_count),
)
inner = " ".join("%s=%r" % t for t in attrs)
return f"<Guild {inner}>"
Expand Down Expand Up @@ -441,11 +441,11 @@ def _remove_role(self, role_id: int, /) -> Role:
return role

def _from_data(self, guild: GuildPayload) -> None:
# according to Stan, this is always available even if the guild is unavailable
# I don't have this guarantee when someone updates the guild.
member_count = guild.get("member_count", None)
if member_count is not None:
self._member_count: int = member_count
member_count = guild.get("member_count")
# Either the payload includes member_count, or it hasn't been set yet.
# Prevents valid _member_count from suddenly changing to None
if member_count is not None or not hasattr(self, "_member_count"):
self._member_count: int | None = member_count

self.name: str = guild.get("name")
self.verification_level: VerificationLevel = try_enum(
Expand Down Expand Up @@ -532,7 +532,7 @@ def _from_data(self, guild: GuildPayload) -> None:

self._sync(guild)
self._large: bool | None = (
None if member_count is None else self._member_count >= 250
None if self._member_count is None else self._member_count >= 250
)

self.owner_id: int | None = utils._get_as_snowflake(guild, "owner_id")
Expand Down Expand Up @@ -598,10 +598,7 @@ def large(self) -> bool:
members, which for this library is set to the maximum of 250.
"""
if self._large is None:
try:
return self._member_count >= 250
except AttributeError:
return len(self._members) >= 250
return (self._member_count or len(self._members)) >= 250
return self._large

@property
Expand Down Expand Up @@ -1002,10 +999,9 @@ def chunked(self) -> bool:
If this value returns ``False``, then you should request for
offline members.
"""
count = getattr(self, "_member_count", None)
if count is None:
if self._member_count is None:
return False
return count == len(self._members)
return self._member_count == len(self._members)

@property
def shard_id(self) -> int:
Expand Down
8 changes: 2 additions & 6 deletions discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,10 +1139,8 @@ def parse_guild_member_add(self, data) -> None:
if self.member_cache_flags.joined:
guild._add_member(member)

try:
if guild._member_count is not None:
guild._member_count += 1
except AttributeError:
pass

self.dispatch("member_join", member)

Expand All @@ -1152,10 +1150,8 @@ def parse_guild_member_remove(self, data) -> None:

guild = self._get_guild(int(data["guild_id"]))
if guild is not None:
try:
if guild._member_count is not None:
guild._member_count -= 1
except AttributeError:
pass

member = guild.get_member(user.id)
if member is not None:
Expand Down