Skip to content

Commit 87d4c1c

Browse files
Refactor permissions
1 parent 88a688b commit 87d4c1c

File tree

3 files changed

+134
-99
lines changed

3 files changed

+134
-99
lines changed

discord/commands/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525

2626
from .commands import *
2727
from .context import *
28+
from .errors import *

discord/commands/commands.py

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import asyncio
2929
import types
30-
from discord.types.channel import TextChannel, VoiceChannel
3130
import functools
3231
import inspect
3332
from collections import OrderedDict
@@ -39,8 +38,9 @@
3938
from ..message import Message
4039
from .context import ApplicationContext
4140
from ..utils import find, get_or_fetch, async_all
42-
from ..errors import DiscordException, NotFound, ValidationError, ClientException
41+
from ..errors import ValidationError, ClientException
4342
from .errors import ApplicationCommandError, CheckFailure, ApplicationCommandInvokeError
43+
from .permissions import Permission, has_role, has_any_role, is_user, is_owner, permission
4444

4545
__all__ = (
4646
"_BaseCommand",
@@ -958,100 +958,4 @@ def validate_chat_input_description(description: Any):
958958
if len(description) > 100 or len(description) < 1:
959959
raise ValidationError(
960960
"Description of a chat input command must be less than 100 characters and non empty."
961-
)
962-
963-
# Slash Command Permissions
964-
class Permission:
965-
def __init__(self, id: Union[int, str], type: int, permission: bool = True, guild_id: int = None):
966-
self.id = id
967-
self.type = type
968-
self.permission = permission
969-
self.guild_id = guild_id
970-
971-
def to_dict(self) -> Dict[int, int, bool]:
972-
return {"id": self.id, "type": self.type, "permission": self.permission}
973-
974-
def permission(role_id: int = None, user_id: int = None, permission: bool = True, guild_id: int = None):
975-
def decorator(func: Callable):
976-
if not role_id is None:
977-
app_cmd_perm = Permission(role_id, 1, permission, guild_id)
978-
elif not user_id is None:
979-
app_cmd_perm = Permission(user_id, 2, permission, guild_id)
980-
else:
981-
raise ValueError("role_id or user_id must be specified!")
982-
983-
# Create __app_cmd_perms__
984-
if not hasattr(func, '__app_cmd_perms__'):
985-
func.__app_cmd_perms__ = []
986-
987-
# Append
988-
func.__app_cmd_perms__.append(app_cmd_perm)
989-
990-
return func
991-
992-
return decorator
993-
994-
def has_role(item: Union[int, str], guild_id: int = None):
995-
def decorator(func: Callable):
996-
# Create __app_cmd_perms__
997-
if not hasattr(func, '__app_cmd_perms__'):
998-
func.__app_cmd_perms__ = []
999-
1000-
# Permissions (Will Convert ID later in register_commands if needed)
1001-
app_cmd_perm = Permission(item, 1, True, guild_id) #{"id": item, "type": 1, "permission": True}
1002-
1003-
# Append
1004-
func.__app_cmd_perms__.append(app_cmd_perm)
1005-
1006-
return func
1007-
1008-
return decorator
1009-
1010-
def has_any_role(*items: Union[int, str], guild_id: int = None):
1011-
def decorator(func: Callable):
1012-
# Create __app_cmd_perms__
1013-
if not hasattr(func, '__app_cmd_perms__'):
1014-
func.__app_cmd_perms__ = []
1015-
1016-
# Permissions (Will Convert ID later in register_commands if needed)
1017-
for item in items:
1018-
app_cmd_perm = Permission(item, 1, True, guild_id) #{"id": item, "type": 1, "permission": True}
1019-
1020-
# Append
1021-
func.__app_cmd_perms__.append(app_cmd_perm)
1022-
1023-
return func
1024-
1025-
return decorator
1026-
1027-
def is_user(user: int, guild_id: int = None):
1028-
def decorator(func: Callable):
1029-
# Create __app_cmd_perms__
1030-
if not hasattr(func, '__app_cmd_perms__'):
1031-
func.__app_cmd_perms__ = []
1032-
1033-
# Permissions (Will Convert ID later in register_commands if needed)
1034-
app_cmd_perm = Permission(user, 2, True, guild_id) #{"id": user, "type": 2, "permission": True}
1035-
1036-
# Append
1037-
func.__app_cmd_perms__.append(app_cmd_perm)
1038-
1039-
return func
1040-
1041-
return decorator
1042-
1043-
def is_owner(guild_id: int = None):
1044-
def decorator(func: Callable):
1045-
# Create __app_cmd_perms__
1046-
if not hasattr(func, '__app_cmd_perms__'):
1047-
func.__app_cmd_perms__ = []
1048-
1049-
# Permissions (Will Convert ID later in register_commands if needed)
1050-
app_cmd_perm = Permission("owner", 2, True, guild_id) #{"id": "owner", "type": 2, "permission": True}
1051-
1052-
# Append
1053-
func.__app_cmd_perms__.append(app_cmd_perm)
1054-
1055-
return func
1056-
1057-
return decorator
961+
)

discord/commands/permissions.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015-2021 Rapptz
5+
Copyright (c) 2021-present Pycord Development
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a
8+
copy of this software and associated documentation files (the "Software"),
9+
to deal in the Software without restriction, including without limitation
10+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
and/or sell copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.
24+
"""
25+
26+
from typing import Union, Dict, Callable
27+
28+
__all__ = (
29+
"Permission",
30+
"has_role",
31+
"has_any_role",
32+
"is_user",
33+
"is_owner",
34+
"permission",
35+
)
36+
37+
class Permission:
38+
def __init__(self, id: Union[int, str], type: int, permission: bool = True, guild_id: int = None):
39+
self.id = id
40+
self.type = type
41+
self.permission = permission
42+
self.guild_id = guild_id
43+
44+
def to_dict(self) -> Dict[str, Union[int, bool]]:
45+
return {"id": self.id, "type": self.type, "permission": self.permission}
46+
47+
def permission(role_id: int = None, user_id: int = None, permission: bool = True, guild_id: int = None):
48+
def decorator(func: Callable):
49+
if not role_id is None:
50+
app_cmd_perm = Permission(role_id, 1, permission, guild_id)
51+
elif not user_id is None:
52+
app_cmd_perm = Permission(user_id, 2, permission, guild_id)
53+
else:
54+
raise ValueError("role_id or user_id must be specified!")
55+
56+
# Create __app_cmd_perms__
57+
if not hasattr(func, '__app_cmd_perms__'):
58+
func.__app_cmd_perms__ = []
59+
60+
# Append
61+
func.__app_cmd_perms__.append(app_cmd_perm)
62+
63+
return func
64+
65+
return decorator
66+
67+
def has_role(item: Union[int, str], guild_id: int = None):
68+
def decorator(func: Callable):
69+
# Create __app_cmd_perms__
70+
if not hasattr(func, '__app_cmd_perms__'):
71+
func.__app_cmd_perms__ = []
72+
73+
# Permissions (Will Convert ID later in register_commands if needed)
74+
app_cmd_perm = Permission(item, 1, True, guild_id) #{"id": item, "type": 1, "permission": True}
75+
76+
# Append
77+
func.__app_cmd_perms__.append(app_cmd_perm)
78+
79+
return func
80+
81+
return decorator
82+
83+
def has_any_role(*items: Union[int, str], guild_id: int = None):
84+
def decorator(func: Callable):
85+
# Create __app_cmd_perms__
86+
if not hasattr(func, '__app_cmd_perms__'):
87+
func.__app_cmd_perms__ = []
88+
89+
# Permissions (Will Convert ID later in register_commands if needed)
90+
for item in items:
91+
app_cmd_perm = Permission(item, 1, True, guild_id) #{"id": item, "type": 1, "permission": True}
92+
93+
# Append
94+
func.__app_cmd_perms__.append(app_cmd_perm)
95+
96+
return func
97+
98+
return decorator
99+
100+
def is_user(user: int, guild_id: int = None):
101+
def decorator(func: Callable):
102+
# Create __app_cmd_perms__
103+
if not hasattr(func, '__app_cmd_perms__'):
104+
func.__app_cmd_perms__ = []
105+
106+
# Permissions (Will Convert ID later in register_commands if needed)
107+
app_cmd_perm = Permission(user, 2, True, guild_id) #{"id": user, "type": 2, "permission": True}
108+
109+
# Append
110+
func.__app_cmd_perms__.append(app_cmd_perm)
111+
112+
return func
113+
114+
return decorator
115+
116+
def is_owner(guild_id: int = None):
117+
def decorator(func: Callable):
118+
# Create __app_cmd_perms__
119+
if not hasattr(func, '__app_cmd_perms__'):
120+
func.__app_cmd_perms__ = []
121+
122+
# Permissions (Will Convert ID later in register_commands if needed)
123+
app_cmd_perm = Permission("owner", 2, True, guild_id) #{"id": "owner", "type": 2, "permission": True}
124+
125+
# Append
126+
func.__app_cmd_perms__.append(app_cmd_perm)
127+
128+
return func
129+
130+
return decorator

0 commit comments

Comments
 (0)