"""The MIT License (MIT)Copyright (c) 2024-present Developer AnonymousPermission 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."""from__future__importannotationsfromtypingimportAnyfromdiscordimportInteractionfromdiscord.abcimportSnowflakefromdiscord.app_commandsimportCommand,Groupfromdiscord.app_commands.commandsimportcheckfrom.errorsimportMissingSKUfrom.enumsimportBucketTypefrom.modelsimportMaxConcurrencyfrom..modelsimportMaxUsages__all__=("max_usages","has_skus","max_concurrency",)
[docs]defmax_usages(limit:int,bucket:BucketType):"""A decorator that adds a limit of usages to a command. Parameters ---------- limit: :class:`int` The amount of allowed usages before the command is no longer usable. bucket: :class:`BucketType` The bucket in which the usages are restricted by. """returncheck(MaxUsages(limit,bucket))
[docs]defhas_skus(*sku_ids:int|str|Snowflake):"""A decorator that checks if the interaction has all the provided skus. Parameters ---------- sku_ids The SKU IDs to check for. """skus=[sku.idifisinstance(sku,Snowflake)elseint(sku)forskuinsku_ids]defpredicate(interaction:Interaction[Any])->bool:entitlements_sku_ids=[e.sku_idforeininteraction.entitlements]ifany(esinotinskusforesiinentitlements_sku_ids):raiseMissingSKU(list(sku_ids))returnTruereturncheck(predicate)
[docs]defmax_concurrency(number:int,per:BucketType=BucketType.default):"""A decorator that adds a maximum concurrency to a :class:`discord.app_commands.Command` or its subclasses. This enabled you to only allow a certain number of command invocations at the same time, for example if a command takes too long or if only one user can use it at a time. This differs from a cooldown in that there is no set waiting period or token bucket -- only a set number of people can run the command. This is the application command variant, for prefixed commands see :func:`discord.ext.commands.max_concurrency`. Unlike :func:`discord.ext.commands.max_concurrency`, this decorator cannot wait for the commands to finish because of the 3 seconds limit to respond to an interaction. Parameters ---------- number: :class:`int` The maximum number of concurrent invocations. per: :class:`.BucketType` The bucket that this concurrency is based on, e.g. :attr:`BucketType.guild` would allow it to be used up to ``number`` times per guild. """defdecorator(func):asyncdefpredicate(interaction:Interaction[Any])->bool:ifinteraction.commandisNone:obj=getattr(func,"__discord_app_commands_max_concurrency__",None)else:obj=interaction.command.extras.get("__max_concurrency__")ifnotisinstance(obj,MaxConcurrency):returnTrueawaitobj.acquire(interaction)awaitobj.release(interaction)# If it does not error in obj.acquire then it has not reached the# max concurrency yet. So return a True.returnTrueifisinstance(func,(Command,Group)):func.extras["__max_concurrency__"]=MaxConcurrency(number,per=per)else:func.__discord_app_commands_max_concurrency__=MaxConcurrency(number,per=per)returncheck(predicate)(func)returndecorator