Skip to content

Commit

Permalink
feat: add missing with_counts parameter to fetch_guilds method (Pycor…
Browse files Browse the repository at this point in the history
…d-Development#2615)

* refactor: Add missing `with_counts` parameter to guild retrieval methods

* Default with_counts to True in fetch_guilds

* Being dumb enough to forget about making it an int

* style(pre-commit): auto fixes from pre-commit.com hooks

* Update discord/client.py

Co-authored-by: Dorukyum <[email protected]>
Signed-off-by: Dark <[email protected]>

* Update discord/iterators.py

Co-authored-by: Dorukyum <[email protected]>
Signed-off-by: Dark <[email protected]>

* Update CHANGELOG.md

* Update discord/client.py

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: Dark <[email protected]>

* Update discord/iterators.py

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: Dark <[email protected]>

---------

Signed-off-by: Dark <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Dorukyum <[email protected]>
Co-authored-by: JustaSqu1d <[email protected]>
  • Loading branch information
4 people authored Nov 3, 2024
1 parent 05cf45e commit 48c5950
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2587](https://github.com/Pycord-Development/pycord/pull/2587/))
- Added optional `filter` parameter to `utils.basic_autocomplete()`.
([#2590](https://github.com/Pycord-Development/pycord/pull/2590))
- Added missing `with_counts` parameter to `fetch_guilds` method.
([#2615](https://github.com/Pycord-Development/pycord/pull/2615))

### Fixed

Expand Down
10 changes: 9 additions & 1 deletion discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ def fetch_guilds(
limit: int | None = 100,
before: SnowflakeTime = None,
after: SnowflakeTime = None,
with_counts: bool = True,
) -> GuildIterator:
"""Retrieves an :class:`.AsyncIterator` that enables receiving your guilds.
Expand Down Expand Up @@ -1489,6 +1490,11 @@ def fetch_guilds(
Retrieve guilds after this date or object.
If a datetime is provided, it is recommended to use a UTC aware datetime.
If the datetime is naive, it is assumed to be local time.
with_counts: :class:`bool`
Whether to include member count information in guilds. This fills the
:attr:`.Guild.approximate_member_count` and :attr:`.Guild.approximate_presence_count`
fields.
Defaults to ``True``.
Yields
------
Expand All @@ -1515,7 +1521,9 @@ def fetch_guilds(
All parameters are optional.
"""
return GuildIterator(self, limit=limit, before=before, after=after)
return GuildIterator(
self, limit=limit, before=before, after=after, with_counts=with_counts
)

async def fetch_template(self, code: Template | str) -> Template:
"""|coro|
Expand Down
3 changes: 3 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ def get_guilds(
limit: int,
before: Snowflake | None = None,
after: Snowflake | None = None,
with_counts: bool = True,
) -> Response[list[guild.Guild]]:
params: dict[str, Any] = {
"limit": limit,
Expand All @@ -1454,6 +1455,8 @@ def get_guilds(
params["before"] = before
if after:
params["after"] = after
if with_counts:
params["with_counts"] = int(with_counts)

return self.request(Route("GET", "/users/@me/guilds"), params=params)

Expand Down
16 changes: 13 additions & 3 deletions discord/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,14 @@ class GuildIterator(_AsyncIterator["Guild"]):
Object before which all guilds must be.
after: Optional[Union[:class:`abc.Snowflake`, :class:`datetime.datetime`]]
Object after which all guilds must be.
with_counts: :class:`bool`
Whether to include member count information in guilds. This fills the
:attr:`.Guild.approximate_member_count` and :attr:`.Guild.approximate_presence_count`
fields.
Defaults to ``True``.
"""

def __init__(self, bot, limit, before=None, after=None):
def __init__(self, bot, limit, before=None, after=None, with_counts=True):
if isinstance(before, datetime.datetime):
before = Object(id=time_snowflake(before, high=False))
if isinstance(after, datetime.datetime):
Expand All @@ -597,6 +602,7 @@ def __init__(self, bot, limit, before=None, after=None):
self.limit = limit
self.before = before
self.after = after
self.with_counts = with_counts

self._filter = None

Expand Down Expand Up @@ -654,7 +660,9 @@ async def _retrieve_guilds(self, retrieve) -> list[Guild]:
async def _retrieve_guilds_before_strategy(self, retrieve):
"""Retrieve guilds using before parameter."""
before = self.before.id if self.before else None
data: list[GuildPayload] = await self.get_guilds(retrieve, before=before)
data: list[GuildPayload] = await self.get_guilds(
retrieve, before=before, with_counts=self.with_counts
)
if len(data):
if self.limit is not None:
self.limit -= retrieve
Expand All @@ -664,7 +672,9 @@ async def _retrieve_guilds_before_strategy(self, retrieve):
async def _retrieve_guilds_after_strategy(self, retrieve):
"""Retrieve guilds using after parameter."""
after = self.after.id if self.after else None
data: list[GuildPayload] = await self.get_guilds(retrieve, after=after)
data: list[GuildPayload] = await self.get_guilds(
retrieve, after=after, with_counts=self.with_counts
)
if len(data):
if self.limit is not None:
self.limit -= retrieve
Expand Down

0 comments on commit 48c5950

Please sign in to comment.