Skip to content

Commit

Permalink
Merge pull request Pycord-Development#465 from JDJGInc/master
Browse files Browse the repository at this point in the history
adds with_count to fetch_guild
  • Loading branch information
Lulalaby authored Nov 16, 2021
2 parents b1af06c + 2d42833 commit 203eb31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 8 additions & 2 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ async def fetch_template(self, code: Union[Template, str]) -> Template:
data = await self.http.get_template(code)
return Template(data=data, state=self._connection) # type: ignore

async def fetch_guild(self, guild_id: int, /) -> Guild:
async def fetch_guild(self, guild_id: int, /, *, with_counts=True) -> Guild:
"""|coro|
Retrieves a :class:`.Guild` from an ID.
Expand All @@ -1215,6 +1215,12 @@ async def fetch_guild(self, guild_id: int, /) -> Guild:
guild_id: :class:`int`
The guild's ID to fetch from.
with_counts: :class:`bool`
Whether to include count information in the guild. This fills the
:attr:`.Guild.approximate_member_count` and :attr:`.Guild.approximate_presence_count`
fields.
.. versionadded:: 2.0
Raises
------
:exc:`.Forbidden`
Expand All @@ -1227,7 +1233,7 @@ async def fetch_guild(self, guild_id: int, /) -> Guild:
:class:`.Guild`
The guild from the ID.
"""
data = await self.http.get_guild(guild_id)
data = await self.http.get_guild(guild_id, with_counts = with_counts)
return Guild(data=data, state=self._connection)

async def create_guild(
Expand Down
18 changes: 18 additions & 0 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ class Guild(Hashable):
nsfw_level: :class:`NSFWLevel`
The guild's NSFW level.
.. versionadded:: 2.0
approximate_member_count: Optional[:class:`int`]
The approximate number of members in the guild. This is ``None`` unless the guild is obtained
using :meth:`Client.fetch_guild` with ``with_counts=True``.
.. versionadded:: 2.0
approximate_presence_count: Optional[:class:`int`]
The approximate number of members currently active in the guild.
This includes idle, dnd, online, and invisible members. Offline members are excluded.
This is ``None`` unless the guild is obtained using :meth:`Client.fetch_guild`
with ``with_counts=True``.
.. versionadded:: 2.0
"""

Expand Down Expand Up @@ -296,6 +310,8 @@ class Guild(Hashable):
'_public_updates_channel_id',
'_stage_instances',
'_threads',
"approximate_member_count",
"approximate_presence_count",
)

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

self._stage_instances: Dict[int, StageInstance] = {}
for s in guild.get('stage_instances', []):
Expand Down
5 changes: 3 additions & 2 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,8 +1100,9 @@ def get_guilds(
def leave_guild(self, guild_id: Snowflake) -> Response[None]:
return self.request(Route('DELETE', '/users/@me/guilds/{guild_id}', guild_id=guild_id))

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

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

0 comments on commit 203eb31

Please sign in to comment.