Skip to content

Commit

Permalink
We tested this some tonight, with caching on and off, and it seems to…
Browse files Browse the repository at this point in the history
… work. It's not the most elegant solution or the fastes for sure.

Snab had mentioned making `HasAnyRole.required_roles` a property and coercing all the roles to ids for faster comparision. We might add that in later.
  • Loading branch information
Patchwork Collective committed Oct 25, 2021
1 parent 5395052 commit 424bfca
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tanjun/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,19 @@ async def __call__(self, ctx: tanjun_abc.Context, /) -> bool:
if not ctx.member:
return self._handle_result(False)

member_roles = ctx.member.get_roles()
guild_roles = ctx.cache.get_roles_view_for_guild(ctx.member.guild_id) if ctx.cache else None
if not guild_roles:
guild_roles = await ctx.rest.fetch_roles(ctx.member.guild_id)
member_roles = [role for role in guild_roles if role.id in ctx.member.role_ids]
else:
member_roles = [guild_roles.get(role) for role in ctx.member.role_ids]

return self._handle_result(any(map(self._check_roles, member_roles)))

return self._handle_result(any(map(self.check_roles, member_roles)))
def _check_roles(self, member_role: typing.Union[int, hikari.Role]) -> bool:
if isinstance(member_role, int):
return any(member_role == check for check in self.required_roles)

def check_roles(self, member_role: hikari.Role) -> bool:
return any(member_role.id == check or member_role.name == check for check in self.required_roles)


Expand Down

0 comments on commit 424bfca

Please sign in to comment.