Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tanjun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async def main() -> None:
"with_owner_check",
"with_author_permission_check",
"with_own_permission_check",
"with_any_role_check"
# clients.py
"clients",
"as_loader",
Expand Down
73 changes: 73 additions & 0 deletions tanjun/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"with_owner_check",
"with_author_permission_check",
"with_own_permission_check",
"with_any_role_check",
]

import abc
Expand Down Expand Up @@ -787,3 +788,75 @@ def with_check(check: tanjun_abc.CheckSig, /) -> collections.Callable[[CommandT]
A command decorator callback which adds the check.
"""
return lambda command: command.add_check(check)


class HasAnyRoleCheck:
__slots__ = (
"_halt_execution",
"_error_message",
"required_roles",
)

def __init__(
self,
roles: list[hikari.SnowflakeishOr[hikari.Role]] = list,
*,
error_message: str = "You do not have the required roles to use this command!",
halt_execution: bool = True,
) -> None:
self._halt_execution = halt_execution
self._error_message = error_message
self.required_roles = roles

async def __call__(self, ctx: tanjun_abc.Context, /) -> bool:

if not ctx.member:
return _handle_result(False, "You must be a member to use this!", True)

member_roles = ctx.member.get_roles()

result = any(self.check_roles(member_role) for member_role in member_roles)
return _handle_result(result, self._error_message, self._halt_execution)

def check_roles(self, member_role: hikari.Role) -> bool:
for check in self.required_roles:
if isinstance(check, int):
if member_role.id == check:
return True
if member_role.name == check:
return True
return False


def with_any_role_check(
Comment thread
patchwork-systems marked this conversation as resolved.
roles: list[hikari.SnowflakeishOr[hikari.Role]] = list,
*,
error_message: str = "You do not have the required roles to use this command!",
halt_execution: bool = True,
) -> collections.Callable[[CommandT], CommandT]:
"""Add a generic check to a command.

Parameters
----------
roles : list[hikari.SnowflakeishOr[hikari.Role]]
The author must have at least one (1) role in this list. (Role.name and Role.id are checked).

Other Parameters
----------------
error_message : Optional[str]
The error message raised if the member does not have a required role.

Defaults to 'You do not have the required roles to use this command!'

halt_execution : bool
Whether this check should raise `tanjun.errors.HaltExecution` to end the execution search
when it fails instead of returning `False`.

Defaults to `False`.

Returns
-------
collections.abc.Callable[[CommandT], CommandT]
A command decorator callback which adds the check.
"""
return lambda command: command.add_check(HasAnyRoleCheck(roles))