Skip to content

Commit 7a690eb

Browse files
committed
changes to template
1 parent 8f71928 commit 7a690eb

File tree

1 file changed

+87
-150
lines changed

1 file changed

+87
-150
lines changed

utils/checks.py

+87-150
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Contains functions to check permissions of users, channels and other.
33
"""
44

5+
from typing import Callable
6+
57
import disnake
68
from disnake.ext import commands
79

@@ -16,149 +18,67 @@
1618

1719

1820
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+
"""
2025

2126
@classmethod
2227
def is_bot_admin(
2328
cls,
2429
ctx: commands.Context | disnake.ApplicationCommandInteraction = None,
2530
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+
)
5439

5540
@classmethod
5641
def is_mod_plus(
5742
cls,
5843
ctx: commands.Context | disnake.ApplicationCommandInteraction = None,
5944
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+
)
9053

9154
@classmethod
9255
def is_submod_plus(
9356
cls,
9457
ctx: commands.Context | disnake.ApplicationCommandInteraction = None,
9558
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."""
9861

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+
)
12668

12769
@classmethod
12870
def is_helper_plus(
12971
cls,
13072
ctx: commands.Context | disnake.ApplicationCommandInteraction = None,
13173
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+
)
16282

16383
@classmethod
16484
def role_check(
@@ -184,46 +104,28 @@ def is_in_modroom(
184104
cls,
185105
ctx: commands.Context | disnake.ApplicationCommandInteraction = None,
186106
raise_exception: bool = True,
187-
) -> bool:
107+
):
188108
"""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+
)
204115

205116
@classmethod
206117
def is_in_voteroom(
207118
cls,
208119
ctx: commands.Context | disnake.ApplicationCommandInteraction = None,
209120
raise_exception: bool = True,
210-
) -> bool:
121+
):
211122
"""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+
)
227129

228130
@classmethod
229131
def is_botroom(cls, inter: disnake.ApplicationCommandInteraction) -> bool:
@@ -249,3 +151,38 @@ def is_botroom(cls, inter: disnake.ApplicationCommandInteraction) -> bool:
249151
return False
250152

251153
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

Comments
 (0)