|
41 | 41 | overload, |
42 | 42 | ) |
43 | 43 |
|
| 44 | +from typing_extensions import override |
| 45 | + |
44 | 46 | from . import abc, utils |
45 | 47 | from .asset import Asset |
46 | 48 | from .automod import AutoModAction, AutoModRule, AutoModTriggerMetadata |
|
94 | 96 | from .welcome_screen import WelcomeScreen, WelcomeScreenChannel |
95 | 97 | from .widget import Widget |
96 | 98 |
|
97 | | -__all__ = ("BanEntry", "Guild") |
| 99 | +__all__ = ("BanEntry", "Guild", "GuildRoleCounts") |
98 | 100 |
|
99 | 101 | MISSING = utils.MISSING |
100 | 102 |
|
@@ -146,6 +148,39 @@ class _GuildLimit(NamedTuple): |
146 | 148 | filesize: int |
147 | 149 |
|
148 | 150 |
|
| 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 | + |
149 | 184 | class Guild(Hashable): |
150 | 185 | """Represents a Discord guild. |
151 | 186 |
|
@@ -1119,17 +1154,20 @@ def get_role(self, role_id: int, /) -> Role | None: |
1119 | 1154 | """ |
1120 | 1155 | return self._roles.get(role_id) |
1121 | 1156 |
|
1122 | | - async def fetch_roles_member_counts(self) -> dict[int, int]: |
| 1157 | + async def fetch_roles_member_counts(self) -> GuildRoleCounts: |
1123 | 1158 | """|coro| |
1124 | 1159 | Fetches a mapping of role IDs to their member counts for this guild. |
1125 | 1160 |
|
| 1161 | + .. versionadded:: 2.7 |
| 1162 | +
|
1126 | 1163 | Returns |
1127 | 1164 | ------- |
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`). |
1130 | 1168 | """ |
1131 | 1169 | 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()}) |
1133 | 1171 |
|
1134 | 1172 | @property |
1135 | 1173 | def default_role(self) -> Role: |
|
0 commit comments