Skip to content

Commit 0c829e9

Browse files
committed
♻️ Add GuildRoleCounts class for mapping role IDs to member counts
1 parent 600bc52 commit 0c829e9

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

discord/guild.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
overload,
4242
)
4343

44+
from typing_extensions import override
45+
4446
from . import abc, utils
4547
from .asset import Asset
4648
from .automod import AutoModAction, AutoModRule, AutoModTriggerMetadata
@@ -94,7 +96,7 @@
9496
from .welcome_screen import WelcomeScreen, WelcomeScreenChannel
9597
from .widget import Widget
9698

97-
__all__ = ("BanEntry", "Guild")
99+
__all__ = ("BanEntry", "Guild", "GuildRoleCounts")
98100

99101
MISSING = utils.MISSING
100102

@@ -146,6 +148,39 @@ class _GuildLimit(NamedTuple):
146148
filesize: int
147149

148150

151+
class GuildRoleCounts(dict[int, int]):
152+
"""A dictionary subclass that maps role IDs to their member counts.
153+
154+
This class allows accessing member counts by either role ID (:class:`int`) or by
155+
a Snowflake object (which has an ``.id`` attribute).
156+
157+
.. versionadded:: 2.7
158+
"""
159+
160+
@override
161+
def __getitem__(self, key: int | abc.Snowflake) -> int:
162+
"""Get the member count for a role.
163+
164+
Parameters
165+
----------
166+
key: Union[:class:`int`, :class:`~discord.abc.Snowflake`]
167+
The role ID or a Snowflake object (e.g., a :class:`Role`).
168+
169+
Returns
170+
-------
171+
:class:`int`
172+
The member count for the role.
173+
174+
Raises
175+
------
176+
KeyError
177+
The role ID was not found.
178+
"""
179+
if isinstance(key, abc.Snowflake):
180+
key = key.id
181+
return super().__getitem__(key)
182+
183+
149184
class Guild(Hashable):
150185
"""Represents a Discord guild.
151186
@@ -1119,17 +1154,20 @@ def get_role(self, role_id: int, /) -> Role | None:
11191154
"""
11201155
return self._roles.get(role_id)
11211156

1122-
async def fetch_roles_member_counts(self) -> dict[int, int]:
1157+
async def fetch_roles_member_counts(self) -> GuildRoleCounts:
11231158
"""|coro|
11241159
Fetches a mapping of role IDs to their member counts for this guild.
11251160
1161+
.. versionadded:: 2.7
1162+
11261163
Returns
11271164
-------
1128-
Dict[:class:`int`, :class:`int`]
1129-
A mapping of role IDs to their member counts.
1165+
:class:`GuildRoleCounts`
1166+
A mapping of role IDs to their member counts. Can be accessed
1167+
with either role IDs (:class:`int`) or Snowflake objects (e.g., :class:`Role`).
11301168
"""
11311169
r = await self._state.http.get_roles_member_counts(self.id)
1132-
return {int(role_id): count for role_id, count in r.items()}
1170+
return GuildRoleCounts({int(role_id): count for role_id, count in r.items()})
11331171

11341172
@property
11351173
def default_role(self) -> Role:

docs/api/models.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ Role
231231
.. autoclass:: RoleColours
232232
:members:
233233

234+
.. attributetable:: GuildRoleCounts
235+
236+
.. autoclass:: GuildRoleCounts()
237+
:members:
238+
234239
Scheduled Event
235240
~~~~~~~~~~~~~~~
236241

0 commit comments

Comments
 (0)