Skip to content

Commit 9a2ff14

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 1309de6 commit 9a2ff14

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

docs/changelog.rst

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

7+
- :release:`10.3.0 <2nd September 2023>`
8+
- :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.
9+
710

811
- :release:`10.2.0 <28th August 2023>`
912
- :support:`192` Bump Discord.py to :literal-url:`2.3.2 <https://github.com/Rapptz/discord.py/releases/tag/v2.3.2>`.

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,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydis_core"
3-
version = "10.2.0"
3+
version = "10.3.0"
44
description = "PyDis core provides core functionality and utility to the bots of the Python Discord community."
55
authors = ["Python Discord <info@pythondiscord.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)