Skip to content
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06bfa06
Added cog_has_role function
EmmmaTech Dec 25, 2021
5f2037e
Added all cog permission functions
EmmmaTech Dec 25, 2021
4573a18
Fixed some circular imports
EmmmaTech Dec 25, 2021
c82bb90
Whoops forgot the cog include
EmmmaTech Dec 25, 2021
6757c38
Merge branch 'Pycord-Development:master' into master
EmmmaTech Dec 26, 2021
ef672d9
Merge branch 'Pycord-Development:master' into master
EmmmaTech Dec 26, 2021
92e7cff
Merge branch 'Pycord-Development:master' into master
EmmmaTech Dec 30, 2021
e094c7a
Add cog permission decorators to __all__
Dorukyum Dec 30, 2021
6978c2d
Fixing conflicts
EmmmaTech Dec 31, 2021
b25e1d8
Still attempting to fix conflicts
EmmmaTech Dec 31, 2021
2fd2cae
Merge branch 'master' into master
EmmmaTech Dec 31, 2021
6ecf811
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 4, 2022
d3ed5ed
Redid the cog permission commands to be decorators
EmmmaTech Jan 4, 2022
73c28e6
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 6, 2022
4ecef49
Updated docstring and added cog_permissions
EmmmaTech Jan 7, 2022
ad2db19
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 7, 2022
232027d
Merge branch 'master' of https://github.com/EmreTech/pycord
EmmmaTech Jan 7, 2022
91df144
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 12, 2022
67569b8
Changed cog-* decorators to take Cog class as input
EmmmaTech Jan 12, 2022
a93ea9f
Merge branch 'master' of https://github.com/EmreTech/pycord
EmmmaTech Jan 12, 2022
f202d0f
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 12, 2022
185e36e
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 23, 2022
233c35a
Merge branch 'Pycord-Development:master' into master
EmmmaTech Jan 27, 2022
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
158 changes: 157 additions & 1 deletion discord/commands/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
DEALINGS IN THE SOFTWARE.
"""

from typing import Union, Dict, Callable
from ..cog import Cog

from typing import Union, Dict, Callable, TypeVar

__all__ = (
"CommandPermission",
Expand All @@ -32,8 +34,14 @@
"is_user",
"is_owner",
"permission",
"cog_has_role",
"cog_has_any_role",
"cog_is_user",
"cog_is_owner",
)

CogT = TypeVar('CogT', bound='Cog')

class CommandPermission:
"""The class used in the application command decorators
to hash permission data into a dictionary using the
Expand Down Expand Up @@ -104,6 +112,45 @@ def decorator(func: Callable):

return decorator

def cog_permission(role_id: int = None, user_id: int = None, permission: bool = True, guild_id: int = None):
"""Modified version of permission that applies to all functions in a cog.

This method is meant to be used as a decorator.

.. versionadded:: 2.0

Parameters
-----------
role_id: :class:`int`
An integer which represents the id of the role that the
permission may be tied to.
user_id: :class:`int`
An integer which represents the id of the user that the
permission may be tied to.
permission: :class:`bool`
A boolean representing the permission's value.
guild_id: :class:`int`
The integer which represents the id of the guild that the
permission may be tied to.
"""
def decorator(parent: CogT):
if not role_id is None:
app_cmd_perm = CommandPermission(role_id, 1, permission, guild_id)
elif not user_id is None:
app_cmd_perm = CommandPermission(user_id, 2, permission, guild_id)
else:
raise ValueError("role_id or user_id must be specified!")

# Loop through every command in the cog
for command in parent.__cog_commands__:
# Create __app_cmd_perms__
if not hasattr(command, "__app_cmd_perms__"):
command.__app_cmd_perms__ = []

command.__app_cmd_perms__.append(app_cmd_perm)

return decorator

def has_role(item: Union[int, str], guild_id: int = None):
"""The method used to specify application command role restrictions.

Expand Down Expand Up @@ -135,6 +182,34 @@ def decorator(func: Callable):

return decorator

def cog_has_role(item: Union[int, str], guild_id: int = None):
"""Modified version of has_role that applies to all functions in a cog.

This method is meant to be used as a decorator.

.. versionadded:: 2.0

Parameters
-----------
item: Union[:class:`int`, :class:`str`]
An integer or string that represent the id or name of the role
that the permission is tied to.
guild_id: :class:`int`
The integer which represents the id of the guild that the
permission may be tied to.
"""
def decorator(parent: CogT):
# Loop through every command in the cog
for command in parent.__cog_commands__:
# Create __app_cmd_perms__
if not hasattr(command, "__app_cmd_perms__"):
command.__app_cmd_perms__ = []

app_cmd_perm = CommandPermission(item, 1, True, guild_id)
command.__app_cmd_perms__.append(app_cmd_perm)

return decorator

def has_any_role(*items: Union[int, str], guild_id: int = None):
"""The method used to specify multiple application command role restrictions,
The application command runs if the invoker has **any** of the specified roles.
Expand Down Expand Up @@ -168,6 +243,35 @@ def decorator(func: Callable):

return decorator

def cog_has_any_role(*items: Union[int, str], guild_id: int = None):
"""Modified version of has_any_role that applies to all functions in a cog.

This method is meant to be used as a decorator.

.. versionadded:: 2.0

Parameters
-----------
*items: Union[:class:`int`, :class:`str`]
The integers or strings that represent the ids or names of the roles
that the permission is tied to.
guild_id: :class:`int`
The integer which represents the id of the guild that the
permission may be tied to.
"""
def decorator(parent: CogT):
for command in parent.__cog_commands__:
# Create __app_cmd_perms__
if not hasattr(command, '__app_cmd_perms__'):
command.__app_cmd_perms__ = []

for item in items:
app_cmd_perm = CommandPermission(item, 1, True, guild_id)

command.__app_cmd_perms__.append(app_cmd_perm)

return decorator

def is_user(user: int, guild_id: int = None):
"""The method used to specify application command user restrictions.

Expand Down Expand Up @@ -198,6 +302,33 @@ def decorator(func: Callable):

return decorator

def cog_is_user(user: int, guild_id: int = None):
"""Modified version of is_user that applies to all functions in a cog.

This method is meant to be used as a decorator.

.. versionadded:: 2.0

Parameters
-----------
user: :class:`int`
An integer that represent the id of the user that the permission is tied to.
guild_id: :class:`int`
The integer which represents the id of the guild that the
permission may be tied to.
"""
def decorator(parent: CogT):
for command in parent.__cog_commands__:
# Create __app_cmd_perms__
if not hasattr(command, '__app_cmd_perms__'):
command.__app_cmd_perms__ = []

app_cmd_perm = CommandPermission(user, 2, True, guild_id)

command.__app_cmd_perms__.append(app_cmd_perm)

return decorator

def is_owner(guild_id: int = None):
"""The method used to limit application commands exclusively
to the owner of the bot.
Expand Down Expand Up @@ -226,3 +357,28 @@ def decorator(func: Callable):
return func

return decorator

def cog_is_owner(guild_id: int = None):
"""Modified version of is_owner that applies to all functions in a cog.

This method is meant to be used as a decorator.

.. versionadded:: 2.0

Parameters
-----------
guild_id: :class:`int`
The integer which represents the id of the guild that the
permission may be tied to.
"""
def decorator(parent: CogT):
for command in parent.__cog_commands__:
# Create __app_cmd_perms__
if not hasattr(command, '__app_cmd_perms__'):
command.__app_cmd_perms__ = []

app_cmd_perm = CommandPermission("owner", 2, True, guild_id)

command.__app_cmd_perms__.append(app_cmd_perm)

return decorator