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