Skip to content

Add the a user_has_access helper function to interactions module. #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Changelog
=========

- :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.

- :release:`10.6.0 <30th January 2024>`
- :feature:`189` Add :obj:`pydis_core.utils.pagination.LinePaginator` which allows users to paginate over content using Embeds, with emoji reactions facilitating navigation.
- :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.
Expand Down
38 changes: 26 additions & 12 deletions pydis_core/utils/interactions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
from collections.abc import Sequence
from typing import Literal

from discord import ButtonStyle, HTTPException, Interaction, Message, NotFound, ui
from discord import ButtonStyle, HTTPException, Interaction, Member, Message, NotFound, User, ui

from pydis_core.utils.logging import get_logger
from pydis_core.utils.scheduling import create_task

log = get_logger(__name__)


def user_has_access(
user: User | Member,
*,
allowed_users: Sequence[int] = (),
allowed_roles: Sequence[int] = (),
) -> bool:
"""
Return whether the user is in the allowed_users list, or has a role from allowed_roles.

Args:
user: The user to check
allowed_users: A sequence of user ids that are allowed access
allowed_roles: A sequence of role ids that are allowed access
"""
if user.id in allowed_users or any(role.id in allowed_roles for role in getattr(user, "roles", [])):
return True
return False


async def _handle_modify_message(message: Message, action: Literal["edit", "delete"]) -> None:
"""Remove the view from, or delete the given message depending on the specified action."""
try:
Expand Down Expand Up @@ -60,18 +79,13 @@ async def interaction_check(self, interaction: Interaction) -> bool:
Args:
interaction: The interaction that occurred.
"""
if interaction.user.id in self.allowed_users:
log.trace(
"Allowed interaction by %s (%d) on %d as they are an allowed user.",
interaction.user,
interaction.user.id,
interaction.message.id,
)
return True

if any(role.id in self.allowed_roles for role in getattr(interaction.user, "roles", [])):
if user_has_access(
interaction.user,
allowed_users=self.allowed_users,
allowed_roles=self.allowed_roles,
):
log.trace(
"Allowed interaction by %s (%d)on %d as they have an allowed role.",
"Allowed interaction by %s (%d) on %d as they are an allowed user or have an allowed role.",
interaction.user,
interaction.user.id,
interaction.message.id,
Expand Down