Skip to content

Commit 203eb31

Browse files
authored
Merge pull request #465 from JDJGInc/master
adds with_count to fetch_guild
2 parents b1af06c + 2d42833 commit 203eb31

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

discord/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ async def fetch_template(self, code: Union[Template, str]) -> Template:
11961196
data = await self.http.get_template(code)
11971197
return Template(data=data, state=self._connection) # type: ignore
11981198

1199-
async def fetch_guild(self, guild_id: int, /) -> Guild:
1199+
async def fetch_guild(self, guild_id: int, /, *, with_counts=True) -> Guild:
12001200
"""|coro|
12011201
12021202
Retrieves a :class:`.Guild` from an ID.
@@ -1215,6 +1215,12 @@ async def fetch_guild(self, guild_id: int, /) -> Guild:
12151215
guild_id: :class:`int`
12161216
The guild's ID to fetch from.
12171217
1218+
with_counts: :class:`bool`
1219+
Whether to include count information in the guild. This fills the
1220+
:attr:`.Guild.approximate_member_count` and :attr:`.Guild.approximate_presence_count`
1221+
fields.
1222+
1223+
.. versionadded:: 2.0
12181224
Raises
12191225
------
12201226
:exc:`.Forbidden`
@@ -1227,7 +1233,7 @@ async def fetch_guild(self, guild_id: int, /) -> Guild:
12271233
:class:`.Guild`
12281234
The guild from the ID.
12291235
"""
1230-
data = await self.http.get_guild(guild_id)
1236+
data = await self.http.get_guild(guild_id, with_counts = with_counts)
12311237
return Guild(data=data, state=self._connection)
12321238

12331239
async def create_guild(

discord/guild.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ class Guild(Hashable):
252252
nsfw_level: :class:`NSFWLevel`
253253
The guild's NSFW level.
254254
255+
.. versionadded:: 2.0
256+
257+
approximate_member_count: Optional[:class:`int`]
258+
The approximate number of members in the guild. This is ``None`` unless the guild is obtained
259+
using :meth:`Client.fetch_guild` with ``with_counts=True``.
260+
261+
.. versionadded:: 2.0
262+
263+
approximate_presence_count: Optional[:class:`int`]
264+
The approximate number of members currently active in the guild.
265+
This includes idle, dnd, online, and invisible members. Offline members are excluded.
266+
This is ``None`` unless the guild is obtained using :meth:`Client.fetch_guild`
267+
with ``with_counts=True``.
268+
255269
.. versionadded:: 2.0
256270
"""
257271

@@ -296,6 +310,8 @@ class Guild(Hashable):
296310
'_public_updates_channel_id',
297311
'_stage_instances',
298312
'_threads',
313+
"approximate_member_count",
314+
"approximate_presence_count",
299315
)
300316

301317
_PREMIUM_GUILD_LIMITS: ClassVar[Dict[Optional[int], _GuildLimit]] = {
@@ -464,6 +480,8 @@ def _from_data(self, guild: GuildPayload) -> None:
464480
self._rules_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'rules_channel_id')
465481
self._public_updates_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'public_updates_channel_id')
466482
self.nsfw_level: NSFWLevel = try_enum(NSFWLevel, guild.get('nsfw_level', 0))
483+
self.approximate_presence_count = guild.get('approximate_presence_count')
484+
self.approximate_member_count = guild.get('approximate_member_count')
467485

468486
self._stage_instances: Dict[int, StageInstance] = {}
469487
for s in guild.get('stage_instances', []):

discord/http.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,8 +1100,9 @@ def get_guilds(
11001100
def leave_guild(self, guild_id: Snowflake) -> Response[None]:
11011101
return self.request(Route('DELETE', '/users/@me/guilds/{guild_id}', guild_id=guild_id))
11021102

1103-
def get_guild(self, guild_id: Snowflake) -> Response[guild.Guild]:
1104-
return self.request(Route('GET', '/guilds/{guild_id}', guild_id=guild_id))
1103+
def get_guild(self, guild_id: Snowflake, *, with_counts = True) -> Response[guild.Guild]:
1104+
params = {'with_counts': int(with_counts)}
1105+
return self.request(Route('GET', '/guilds/{guild_id}', guild_id=guild_id), params=params)
11051106

11061107
def delete_guild(self, guild_id: Snowflake) -> Response[None]:
11071108
return self.request(Route('DELETE', '/guilds/{guild_id}', guild_id=guild_id))

0 commit comments

Comments
 (0)