Skip to content

Commit

Permalink
Added HasAnyRole.ids_only as mentioned <#145 (comment)> in PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Patchwork Collective committed Feb 2, 2022
1 parent 9bc580d commit df3ba8d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tanjun/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ async def __call__(


class HasAnyRoleCheck(_Check):
__slots__ = ("required_roles",)
__slots__ = ("required_roles", "ids_only")

def __init__(
self,
Expand All @@ -523,17 +523,21 @@ def __init__(
) -> None:
super().__init__(error_message, halt_execution)
self.required_roles = roles
self.ids_only = all(isinstance(role, int) for role in self.required_roles)

async def __call__(self, ctx: tanjun_abc.Context, /) -> bool:
if not ctx.member:
return self._handle_result(False)

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]
if not self.ids_only:
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]
else:
member_roles = [guild_roles.get(role) for role in ctx.member.role_ids]
member_roles = ctx.member.role_ids

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

Expand Down

0 comments on commit df3ba8d

Please sign in to comment.