|
1 | 1 | from collections.abc import Sequence
|
2 | 2 | from typing import Literal
|
3 | 3 |
|
4 |
| -from discord import ButtonStyle, HTTPException, Interaction, Message, NotFound, ui |
| 4 | +from discord import ButtonStyle, HTTPException, Interaction, Member, Message, NotFound, User, ui |
5 | 5 |
|
6 | 6 | from pydis_core.utils.logging import get_logger
|
7 | 7 | from pydis_core.utils.scheduling import create_task
|
8 | 8 |
|
9 | 9 | log = get_logger(__name__)
|
10 | 10 |
|
11 | 11 |
|
| 12 | +def user_has_access( |
| 13 | + user: User | Member, |
| 14 | + *, |
| 15 | + allowed_users: Sequence[int] = (), |
| 16 | + allowed_roles: Sequence[int] = (), |
| 17 | +) -> bool: |
| 18 | + """ |
| 19 | + Return whether the user is in the allowed_users list, or has a role from allowed_roles. |
| 20 | +
|
| 21 | + Args: |
| 22 | + user: The user to check |
| 23 | + allowed_users: A sequence of user ids that are allowed access |
| 24 | + allowed_roles: A sequence of role ids that are allowed access |
| 25 | + """ |
| 26 | + if user.id in allowed_users or any(role.id in allowed_roles for role in getattr(user, "roles", [])): |
| 27 | + return True |
| 28 | + return False |
| 29 | + |
| 30 | + |
12 | 31 | async def _handle_modify_message(message: Message, action: Literal["edit", "delete"]) -> None:
|
13 | 32 | """Remove the view from, or delete the given message depending on the specified action."""
|
14 | 33 | try:
|
@@ -60,18 +79,13 @@ async def interaction_check(self, interaction: Interaction) -> bool:
|
60 | 79 | Args:
|
61 | 80 | interaction: The interaction that occurred.
|
62 | 81 | """
|
63 |
| - if interaction.user.id in self.allowed_users: |
64 |
| - log.trace( |
65 |
| - "Allowed interaction by %s (%d) on %d as they are an allowed user.", |
66 |
| - interaction.user, |
67 |
| - interaction.user.id, |
68 |
| - interaction.message.id, |
69 |
| - ) |
70 |
| - return True |
71 |
| - |
72 |
| - if any(role.id in self.allowed_roles for role in getattr(interaction.user, "roles", [])): |
| 82 | + if user_has_access( |
| 83 | + interaction.user, |
| 84 | + allowed_users=self.allowed_users, |
| 85 | + allowed_roles=self.allowed_roles, |
| 86 | + ): |
73 | 87 | log.trace(
|
74 |
| - "Allowed interaction by %s (%d)on %d as they have an allowed role.", |
| 88 | + "Allowed interaction by %s (%d) on %d as they are an allowed user or have an allowed role.", |
75 | 89 | interaction.user,
|
76 | 90 | interaction.user.id,
|
77 | 91 | interaction.message.id,
|
|
0 commit comments