Skip to content

Commit

Permalink
Add the a user_has_access helper function to interactions module.
Browse files Browse the repository at this point in the history
This returns whether the given user is in the allowed_users list, or has a role from allowed_roles.
  • Loading branch information
ChrisLovering committed Sep 2, 2023
1 parent 1309de6 commit 9a2ff14
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
Changelog
=========

- :release:`10.3.0 <2nd September 2023>`
- :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.2.0 <28th August 2023>`
- :support:`192` Bump Discord.py to :literal-url:`2.3.2 <https://github.com/Rapptz/discord.py/releases/tag/v2.3.2>`.
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydis_core"
version = "10.2.0"
version = "10.3.0"
description = "PyDis core provides core functionality and utility to the bots of the Python Discord community."
authors = ["Python Discord <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 9a2ff14

Please sign in to comment.