aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar D0rs4n <[email protected]>2021-08-02 18:28:05 +0200
committerGravatar D0rs4n <[email protected]>2021-08-02 18:28:05 +0200
commit20997b425168890636a04b270e7433fa82c60fa6 (patch)
tree6e979c25d1d311e77065ee2b451257e0c93fb1af
parentUpdate join_role_stats and NonExistentError to be clear (diff)
Optimize Information Cog's join_role_stats and get_member counts
-rw-r--r--bot/errors.py3
-rw-r--r--bot/exts/info/information.py20
2 files changed, 10 insertions, 13 deletions
diff --git a/bot/errors.py b/bot/errors.py
index 69c588f4a..5209997f9 100644
--- a/bot/errors.py
+++ b/bot/errors.py
@@ -52,5 +52,6 @@ class NonExistentRoleError(ValueError):
"""
def __init__(self, role_id: int):
+ super().__init__(f"Could not fetch data for role {role_id}")
+
self.role_id = role_id
- super().__init__(f"Could not fetch data for role {self.role_id}")
diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py
index 62fde5473..2621e15b1 100644
--- a/bot/exts/info/information.py
+++ b/bot/exts/info/information.py
@@ -43,31 +43,27 @@ class Information(Cog):
return channel_counter
@staticmethod
- def join_role_stats(role_ids: list[int], name: str, guild: Guild) -> dict[str, int]:
+ def join_role_stats(role_ids: list[int], guild: Guild, name: Optional[str] = None) -> dict[str, int]:
"""Return a dictionary with the number of `members` of each role given, and the `name` for this joined group."""
- members = []
+ members = int()
for role_id in role_ids:
if (role := guild.get_role(role_id)) is not None:
- members.append(role.members)
+ members += len(role.members)
else:
raise NonExistentRoleError(role_id)
- return {name: len(set(members))}
+ return {name or role.name.title(): members}
@staticmethod
def get_member_counts(guild: Guild) -> dict[str, int]:
"""Return the total number of members for certain roles in `guild`."""
role_ids = [constants.Roles.helpers, constants.Roles.mod_team, constants.Roles.admins,
constants.Roles.owners, constants.Roles.contributors]
- roles = []
- for role_id in role_ids:
- if (role := guild.get_role(role_id)) is not None:
- roles.append(role)
- else:
- raise NonExistentRoleError(role_id)
- role_stats = {role.name.title(): len(role.members) for role in roles}
+ role_stats = {}
+ for role_id in role_ids:
+ role_stats.update(Information.join_role_stats([role_id], guild))
role_stats.update(
- **Information.join_role_stats([constants.Roles.project_leads, constants.Roles.domain_leads], "Leads", guild)
+ Information.join_role_stats([constants.Roles.project_leads, constants.Roles.domain_leads], guild, "Leads")
)
return role_stats