|
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 Callable |
27 | | - |
28 | | -from ..permissions import Permissions |
29 | | -from .core import ApplicationCommand |
30 | | - |
31 | | -__all__ = ( |
32 | | - "default_permissions", |
33 | | - "guild_only", |
34 | | -) |
35 | | - |
36 | | - |
37 | | -def default_permissions(**perms: bool) -> Callable: |
38 | | - """A decorator that limits the usage of a slash command to members with certain |
39 | | - permissions. |
40 | | -
|
41 | | - The permissions passed in must be exactly like the properties shown under |
42 | | - :class:`.discord.Permissions`. |
43 | | -
|
44 | | - .. note:: |
45 | | - These permissions can be updated by server administrators per-guild. As such, these are only "defaults", as the |
46 | | - name suggests. If you want to make sure that a user **always** has the specified permissions regardless, you |
47 | | - should use an internal check such as :func:`~.ext.commands.has_permissions`. |
48 | | -
|
49 | | - Parameters |
50 | | - ---------- |
51 | | - **perms: Dict[:class:`str`, :class:`bool`] |
52 | | - An argument list of permissions to check for. |
53 | | -
|
54 | | - Example |
55 | | - ------- |
56 | | -
|
57 | | - .. code-block:: python3 |
58 | | -
|
59 | | - from discord import default_permissions |
60 | | -
|
61 | | - @bot.slash_command() |
62 | | - @default_permissions(manage_messages=True) |
63 | | - async def test(ctx): |
64 | | - await ctx.respond('You can manage messages.') |
65 | | - """ |
66 | | - |
67 | | - invalid = set(perms) - set(Permissions.VALID_FLAGS) |
68 | | - if invalid: |
69 | | - raise TypeError(f"Invalid permission(s): {', '.join(invalid)}") |
70 | | - |
71 | | - def inner(command: Callable): |
72 | | - if isinstance(command, ApplicationCommand): |
73 | | - if command.parent is not None: |
74 | | - raise RuntimeError( |
75 | | - "Permission restrictions can only be set on top-level commands" |
76 | | - ) |
77 | | - command.default_member_permissions = Permissions(**perms) |
78 | | - else: |
79 | | - command.__default_member_permissions__ = Permissions(**perms) |
80 | | - return command |
81 | | - |
82 | | - return inner |
83 | | - |
84 | | - |
85 | | -def guild_only() -> Callable: |
86 | | - """A decorator that limits the usage of a slash command to guild contexts. |
87 | | - The command won't be able to be used in private message channels. |
88 | | -
|
89 | | - Example |
90 | | - ------- |
91 | | -
|
92 | | - .. code-block:: python3 |
93 | | -
|
94 | | - from discord import guild_only |
95 | | -
|
96 | | - @bot.slash_command() |
97 | | - @guild_only() |
98 | | - async def test(ctx): |
99 | | - await ctx.respond("You're in a guild.") |
100 | | - """ |
101 | | - |
102 | | - def inner(command: Callable): |
103 | | - if isinstance(command, ApplicationCommand): |
104 | | - command.guild_only = True |
105 | | - else: |
106 | | - command.__guild_only__ = True |
107 | | - |
108 | | - return command |
109 | | - |
110 | | - return inner |
| 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 Callable |
| 27 | + |
| 28 | +from ..permissions import Permissions |
| 29 | +from .core import ApplicationCommand |
| 30 | + |
| 31 | +__all__ = ( |
| 32 | + "default_permissions", |
| 33 | + "guild_only", |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +def default_permissions(**perms: bool) -> Callable: |
| 38 | + """A decorator that limits the usage of a slash command to members with certain |
| 39 | + permissions. |
| 40 | +
|
| 41 | + The permissions passed in must be exactly like the properties shown under |
| 42 | + :class:`.discord.Permissions`. |
| 43 | +
|
| 44 | + .. note:: |
| 45 | + These permissions can be updated by server administrators per-guild. As such, these are only "defaults", as the |
| 46 | + name suggests. If you want to make sure that a user **always** has the specified permissions regardless, you |
| 47 | + should use an internal check such as :func:`~.ext.commands.has_permissions`. |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + **perms: Dict[:class:`str`, :class:`bool`] |
| 52 | + An argument list of permissions to check for. |
| 53 | +
|
| 54 | + Example |
| 55 | + ------- |
| 56 | +
|
| 57 | + .. code-block:: python3 |
| 58 | +
|
| 59 | + from discord import default_permissions |
| 60 | +
|
| 61 | + @bot.slash_command() |
| 62 | + @default_permissions(manage_messages=True) |
| 63 | + async def test(ctx): |
| 64 | + await ctx.respond('You can manage messages.') |
| 65 | + """ |
| 66 | + |
| 67 | + invalid = set(perms) - set(Permissions.VALID_FLAGS) |
| 68 | + if invalid: |
| 69 | + raise TypeError(f"Invalid permission(s): {', '.join(invalid)}") |
| 70 | + |
| 71 | + def inner(command: Callable): |
| 72 | + if isinstance(command, ApplicationCommand): |
| 73 | + if command.parent is not None: |
| 74 | + raise RuntimeError( |
| 75 | + "Permission restrictions can only be set on top-level commands" |
| 76 | + ) |
| 77 | + command.default_member_permissions = Permissions(**perms) |
| 78 | + else: |
| 79 | + command.__default_member_permissions__ = Permissions(**perms) |
| 80 | + return command |
| 81 | + |
| 82 | + return inner |
| 83 | + |
| 84 | + |
| 85 | +def guild_only() -> Callable: |
| 86 | + """A decorator that limits the usage of a slash command to guild contexts. |
| 87 | + The command won't be able to be used in private message channels. |
| 88 | +
|
| 89 | + Example |
| 90 | + ------- |
| 91 | +
|
| 92 | + .. code-block:: python3 |
| 93 | +
|
| 94 | + from discord import guild_only |
| 95 | +
|
| 96 | + @bot.slash_command() |
| 97 | + @guild_only() |
| 98 | + async def test(ctx): |
| 99 | + await ctx.respond("You're in a guild.") |
| 100 | + """ |
| 101 | + |
| 102 | + def inner(command: Callable): |
| 103 | + if isinstance(command, ApplicationCommand): |
| 104 | + command.guild_only = True |
| 105 | + else: |
| 106 | + command.__guild_only__ = True |
| 107 | + |
| 108 | + return command |
| 109 | + |
| 110 | + return inner |
0 commit comments