Skip to content

Commit 7d306e6

Browse files
committed
Add the a user_has_access helper function to interactions module.
This returns whether the given user is in the allowed_users list, or has a role from allowed_roles.
1 parent ba5a37c commit 7d306e6

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Changelog
55
=========
66

7+
- :feature:`194` Add the :obj:`pydis_core.utils.interactions.user_has_access` helper function, that returns whether the given user is in the allowed_users list, or has a role from allowed_roles.
8+
79
- :release:`10.6.0 <30th January 2024>`
810
- :feature:`189` Add :obj:`pydis_core.utils.pagination.LinePaginator` which allows users to paginate over content using Embeds, with emoji reactions facilitating navigation.
911
- :feature:`189` Add :obj:`pydis_core.utils.messages.reaction_check`, a predicate that dictates whether a user has the right to add a specific set of reactions based on certain criteria.

pydis_core/utils/interactions.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
from collections.abc import Sequence
22
from typing import Literal
33

4-
from discord import ButtonStyle, HTTPException, Interaction, Message, NotFound, ui
4+
from discord import ButtonStyle, HTTPException, Interaction, Member, Message, NotFound, User, ui
55

66
from pydis_core.utils.logging import get_logger
77
from pydis_core.utils.scheduling import create_task
88

99
log = get_logger(__name__)
1010

1111

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+
1231
async def _handle_modify_message(message: Message, action: Literal["edit", "delete"]) -> None:
1332
"""Remove the view from, or delete the given message depending on the specified action."""
1433
try:
@@ -60,18 +79,13 @@ async def interaction_check(self, interaction: Interaction) -> bool:
6079
Args:
6180
interaction: The interaction that occurred.
6281
"""
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+
):
7387
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.",
7589
interaction.user,
7690
interaction.user.id,
7791
interaction.message.id,

0 commit comments

Comments
 (0)