2
2
Contains functions to check permissions of users, channels and other.
3
3
"""
4
4
5
+ from typing import Callable
6
+
5
7
import disnake
6
8
from disnake .ext import commands
7
9
16
18
17
19
18
20
class PermissionsCheck :
19
- """Class containing methods to check permissions"""
21
+ """Class containing methods to check permissions
22
+
23
+ Methods using `check_template` are used as decorators for commands or as normal functions.
24
+ """
20
25
21
26
@classmethod
22
27
def is_bot_admin (
23
28
cls ,
24
29
ctx : commands .Context | disnake .ApplicationCommandInteraction = None ,
25
30
raise_exception : bool = True ,
26
- ) -> bool :
27
- """
28
- Check if the user is a bot admin. Works as a function or a command decorator.
29
-
30
- When `ctx` is passed, it works as a function.
31
- When `ctx` is not passed, it acts as a decorator for a command.
32
-
33
- Returns
34
- -------
35
- bool
36
- True if user is an admin, False otherwise.
37
- Exception
38
- Throws `PermissionError` if `raise_exception=True` and user doesn't have permissions.
39
- """
40
-
41
- def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
42
- if ctx .author .id in config .admin_ids :
43
- return True
44
- if raise_exception :
45
- raise PermissionError (message = Messages .bot_admin_only )
46
- return False
47
-
48
- if ctx :
49
- # If ctx is passed, return the result of the predicate
50
- return predicate (ctx )
51
- else :
52
- # If ctx is not passed, act as a decorator for command
53
- return commands .check (predicate )
31
+ ):
32
+ """Check if the user is a bot admin."""
33
+ return cls .check_template (
34
+ ctx ,
35
+ lambda ctx : ctx .author .id in config .admin_ids ,
36
+ raise_exception ,
37
+ PermissionError (Messages .bot_admin_only ),
38
+ )
54
39
55
40
@classmethod
56
41
def is_mod_plus (
57
42
cls ,
58
43
ctx : commands .Context | disnake .ApplicationCommandInteraction = None ,
59
44
raise_exception : bool = True ,
60
- ) -> bool :
61
- """Check if user has permission for command mod or above.
62
-
63
- When `ctx` is passed, it works as a function.
64
- When `ctx` is not passed, it acts as a decorator for a command.
65
-
66
- Returns
67
- -------
68
- bool
69
- True if user is an admin, False otherwise.
70
- Exception
71
- Throws `PermissionError` if `raise_exception=True` and user doesn't have permissions.
72
- """
73
-
74
- def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
75
- if cls .is_bot_admin (ctx ):
76
- return True
77
- if cls .role_check (ctx , PRIVILEGED_ROLES [:- 2 ]):
78
- return True
79
-
80
- if raise_exception :
81
- raise PermissionError (message = Messages .mod_plus_only )
82
- return False
83
-
84
- if ctx :
85
- # If ctx is passed, return the result of the predicate
86
- return predicate (ctx )
87
- else :
88
- # If ctx is not passed, act as a decorator for command
89
- return commands .check (predicate )
45
+ ):
46
+ """Check if user has permission for command mod or above."""
47
+ return cls .check_template (
48
+ ctx ,
49
+ lambda ctx : (cls .is_bot_admin (ctx ) or cls .role_check (ctx , PRIVILEGED_ROLES [:- 2 ])),
50
+ raise_exception ,
51
+ PermissionError (Messages .mod_plus_only ),
52
+ )
90
53
91
54
@classmethod
92
55
def is_submod_plus (
93
56
cls ,
94
57
ctx : commands .Context | disnake .ApplicationCommandInteraction = None ,
95
58
raise_exception : bool = True ,
96
- ) -> bool :
97
- """Check if user has permission for command submod or above.
59
+ ):
60
+ """Check if user has permission for command submod or above."""
98
61
99
- When `ctx` is passed, it works as a function.
100
- When `ctx` is not passed, it acts as a decorator for a command.
101
-
102
- Returns
103
- -------
104
- bool
105
- True if user is an admin, False otherwise.
106
- Exception
107
- Throws `PermissionError` if `raise_exception=True` and user doesn't have permissions.
108
- """
109
-
110
- def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
111
- if cls .is_bot_admin (ctx ):
112
- return True
113
- if cls .role_check (ctx , PRIVILEGED_ROLES [:- 1 ]):
114
- return True
115
-
116
- if raise_exception :
117
- raise PermissionError (message = Messages .submod_plus_only )
118
- return False
119
-
120
- if ctx :
121
- # If ctx is passed, return the result of the predicate
122
- return predicate (ctx )
123
- else :
124
- # If ctx is not passed, act as a decorator for command
125
- return commands .check (predicate )
62
+ return cls .check_template (
63
+ ctx ,
64
+ lambda ctx : cls .is_bot_admin (ctx ) or cls .role_check (ctx , PRIVILEGED_ROLES [:- 1 ]),
65
+ raise_exception ,
66
+ PermissionError (Messages .submod_plus_only ),
67
+ )
126
68
127
69
@classmethod
128
70
def is_helper_plus (
129
71
cls ,
130
72
ctx : commands .Context | disnake .ApplicationCommandInteraction = None ,
131
73
raise_exception : bool = True ,
132
- ) -> bool :
133
- """Check if user has permission for command helper or above.
134
-
135
- When `ctx` is passed, it works as a function.
136
- When `ctx` is not passed, it acts as a decorator for a command.
137
-
138
- Returns
139
- -------
140
- bool
141
- True if user is an admin, False otherwise.
142
- Exception
143
- Throws `PermissionError` if `raise_exception=True` and user doesn't have permissions.
144
- """
145
-
146
- def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
147
- if cls .is_bot_admin (ctx ):
148
- return True
149
- if cls .role_check (ctx , PRIVILEGED_ROLES ):
150
- return True
151
-
152
- if raise_exception :
153
- raise PermissionError (message = Messages .helper_plus_only )
154
- return False
155
-
156
- if ctx :
157
- # If ctx is passed, return the result of the predicate
158
- return predicate (ctx )
159
- else :
160
- # If ctx is not passed, act as a decorator for command
161
- return commands .check (predicate )
74
+ ):
75
+ """Check if user has permission for command helper or above."""
76
+ return cls .check_template (
77
+ ctx ,
78
+ lambda ctx : cls .is_bot_admin (ctx ) or cls .role_check (ctx , PRIVILEGED_ROLES ),
79
+ raise_exception ,
80
+ PermissionError (Messages .helper_plus_only ),
81
+ )
162
82
163
83
@classmethod
164
84
def role_check (
@@ -184,46 +104,28 @@ def is_in_modroom(
184
104
cls ,
185
105
ctx : commands .Context | disnake .ApplicationCommandInteraction = None ,
186
106
raise_exception : bool = True ,
187
- ) -> bool :
107
+ ):
188
108
"""Check if the command is invoked in modroom"""
189
-
190
- def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
191
- if ctx .channel .id == config .mod_room :
192
- return True
193
-
194
- if raise_exception :
195
- raise InvalidRoomError (Messages .specific_room_only (room = config .mod_room ))
196
- return False
197
-
198
- if ctx :
199
- # If ctx is passed, return the result of the predicate
200
- return predicate (ctx )
201
- else :
202
- # If ctx is not passed, act as a decorator for command
203
- return commands .check (predicate )
109
+ return cls .check_template (
110
+ ctx ,
111
+ lambda ctx : ctx .channel .id == config .mod_room ,
112
+ raise_exception ,
113
+ InvalidRoomError (Messages .specific_room_only (room = config .mod_room )),
114
+ )
204
115
205
116
@classmethod
206
117
def is_in_voteroom (
207
118
cls ,
208
119
ctx : commands .Context | disnake .ApplicationCommandInteraction = None ,
209
120
raise_exception : bool = True ,
210
- ) -> bool :
121
+ ):
211
122
"""Check if the command is invoked in voteroom"""
212
-
213
- def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
214
- if ctx .channel .id == config .vote_room :
215
- return True
216
-
217
- if raise_exception :
218
- raise InvalidRoomError (Messages .specific_room_only (room = config .vote_room ))
219
- return False
220
-
221
- if ctx :
222
- # If ctx is passed, return the result of the predicate
223
- return predicate (ctx )
224
- else :
225
- # If ctx is not passed, act as a decorator for command
226
- return commands .check (predicate )
123
+ return cls .check_template (
124
+ ctx ,
125
+ lambda ctx : ctx .channel .id == config .vote_room ,
126
+ raise_exception ,
127
+ InvalidRoomError (Messages .specific_room_only (room = config .vote_room )),
128
+ )
227
129
228
130
@classmethod
229
131
def is_botroom (cls , inter : disnake .ApplicationCommandInteraction ) -> bool :
@@ -249,3 +151,38 @@ def is_botroom(cls, inter: disnake.ApplicationCommandInteraction) -> bool:
249
151
return False
250
152
251
153
return True
154
+
155
+ @classmethod
156
+ def check_template (
157
+ cls ,
158
+ ctx : commands .Context | disnake .ApplicationCommandInteraction ,
159
+ check : Callable ,
160
+ raise_exception : bool ,
161
+ exception : Exception ,
162
+ ) -> bool :
163
+ """Check if the command is invoked in voteroom
164
+
165
+ When `ctx` is passed, it works as a function.
166
+ When `ctx` is not passed, it acts as a decorator for a command.
167
+
168
+ Returns
169
+ -------
170
+ bool
171
+ based on the passed check
172
+ Exception
173
+ Throws `Exception` if `raise_exception=True` and `check=False`
174
+ """
175
+
176
+ def predicate (ctx : commands .Context | disnake .ApplicationCommandInteraction ) -> bool :
177
+ if check (ctx ):
178
+ return True
179
+ if raise_exception :
180
+ raise exception
181
+ return False
182
+
183
+ if ctx :
184
+ # If ctx is passed, return the result of the predicate
185
+ return predicate (ctx )
186
+ else :
187
+ # If ctx is not passed, act as a decorator for command
188
+ return commands .check (predicate )
0 commit comments