Skip to content

Commit 424bfca

Browse files
author
Patchwork Collective
committed
We tested this some tonight, with caching on and off, and it seems to 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.
1 parent 5395052 commit 424bfca

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

tanjun/checks.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,19 @@ async def __call__(self, ctx: tanjun_abc.Context, /) -> bool:
316316
if not ctx.member:
317317
return self._handle_result(False)
318318

319-
member_roles = ctx.member.get_roles()
319+
guild_roles = ctx.cache.get_roles_view_for_guild(ctx.member.guild_id) if ctx.cache else None
320+
if not guild_roles:
321+
guild_roles = await ctx.rest.fetch_roles(ctx.member.guild_id)
322+
member_roles = [role for role in guild_roles if role.id in ctx.member.role_ids]
323+
else:
324+
member_roles = [guild_roles.get(role) for role in ctx.member.role_ids]
325+
326+
return self._handle_result(any(map(self._check_roles, member_roles)))
320327

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

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

326334

0 commit comments

Comments
 (0)