✨ Permission Templates
If your bot is on two or more servers and you want to apply the same permissions to a set of commands you can be a prisoner of this terrible monster:
# init.py
from discord_styled.permissions import only_allow_roles
@slash.slash(...)
@only_allow_roles([1234567890, 1234567891], [1234567892, 1234567893])
async def...
@slash.slash(...)
@only_allow_roles([1234567890, 1234567891], [1234567892, 1234567893])
async def...
@slash.slash(...)
@only_allow_roles([1234567890, 1234567891], [1234567892, 1234567893])
async def...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Creating a template
To avoid this problem let's create a templeate, first create a file called utils.py
, now let's add the following code:
# utils.py
from discord_styled.utils.permissions import Permissions
my_permissions = Permissions([1234567890, 1234567891])
my_permissions.only_allow_roles([1234567892, 1234567893])
my_permissions = my_permissions.permissions
1
2
3
4
5
6
2
3
4
5
6
# utils.py
from discord_styled.utils.permissions import Permissions
my_permissions = Permissions([1234567890, 1234567891]).only_allow_roles([1234567892, 1234567893])
1
2
3
4
2
3
4
Only use the alternative way if you're going to use just one function of the class
Applying a template
Now go to your init.py
(or the file which your command is), import your template and use it:
# init.py
from discord_styled.permissions import permissions
from utils import my_permissions
@slash.slash(...)
@permissions(my_permissions)
async def...
@slash.slash(...)
@permissions(my_permissions)
async def...
@slash.slash(...)
@only_allow_roles(my_permissions)
async def...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# init.py
from discord_styled.permissions import only_allow_roles
@slash.slash(...)
@only_allow_roles([1234567890, 1234567891], [1234567892, 1234567893])
async def...
@slash.slash(...)
@only_allow_roles([1234567890, 1234567891], [1234567892, 1234567893])
async def...
@slash.slash(...)
@only_allow_roles([1234567890, 1234567891], [1234567892, 1234567893])
async def...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Amazing, right? Now you can create your templates and avoid ugly and repetitive code.
Reference
If you want to know all about the Permissions class methods, parameters and types go to the API Reference