"""The MIT License (MIT)Copyright (c) 2015-2021 RapptzCopyright (c) 2021-present Pycord DevelopmentPermission is hereby granted, free of charge, to any person obtaining acopy of this software and associated documentation files (the "Software"),to deal in the Software without restriction, including without limitationthe rights to use, copy, modify, merge, publish, distribute, sublicense,and/or sell copies of the Software, and to permit persons to whom theSoftware is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISINGFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHERDEALINGS IN THE SOFTWARE."""fromtypingimportCallablefrom..enumsimportInteractionContextTypefrom..permissionsimportPermissionsfrom.coreimportApplicationCommand__all__=("default_permissions","guild_only","is_nsfw")
[docs]defdefault_permissions(**perms:bool)->Callable:"""A decorator that limits the usage of an application command to members with certain permissions. The permissions passed in must be exactly like the properties shown under :class:`.discord.Permissions`. .. note:: These permissions can be updated by server administrators per-guild. As such, these are only "defaults", as the name suggests. If you want to make sure that a user **always** has the specified permissions regardless, you should use an internal check such as :func:`~.ext.commands.has_permissions`. Parameters ---------- **perms: Dict[:class:`str`, :class:`bool`] An argument list of permissions to check for. Example ------- .. code-block:: python3 from discord import default_permissions @bot.slash_command() @default_permissions(manage_messages=True) async def test(ctx): await ctx.respond('You can manage messages.') """invalid=set(perms)-set(Permissions.VALID_FLAGS)ifinvalid:raiseTypeError(f"Invalid permission(s): {', '.join(invalid)}")definner(command:Callable):ifisinstance(command,ApplicationCommand):ifcommand.parentisnotNone:raiseRuntimeError("Permission restrictions can only be set on top-level commands")command.default_member_permissions=Permissions(**perms)else:command.__default_member_permissions__=Permissions(**perms)returncommandreturninner
[docs]defguild_only()->Callable:"""A decorator that limits the usage of an application command to guild contexts. The command won't be able to be used in private message channels. Example ------- .. code-block:: python3 from discord import guild_only @bot.slash_command() @guild_only() async def test(ctx): await ctx.respond("You're in a guild.") """definner(command:Callable):ifisinstance(command,ApplicationCommand):command.contexts={InteractionContextType.guild}else:command.__contexts__={InteractionContextType.guild}returncommandreturninner
[docs]defis_nsfw()->Callable:"""A decorator that limits the usage of an application command to 18+ channels and users. In guilds, the command will only be able to be used in channels marked as NSFW. In DMs, users must have opted into age-restricted commands via privacy settings. Note that apps intending to be listed in the App Directory cannot have NSFW commands. Example ------- .. code-block:: python3 from discord import is_nsfw @bot.slash_command() @is_nsfw() async def test(ctx): await ctx.respond("This command is age restricted.") """definner(command:Callable):ifisinstance(command,ApplicationCommand):command.nsfw=Trueelse:command.__nsfw__=Truereturncommandreturninner