@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
1 lines • 6.05 kB
Source Map (JSON)
{"version":3,"file":"Cooldown.cjs","names":["AllFlowsPrecondition","TimestampStyles","Identifiers","BucketScope","RateLimitManager","container"],"sources":["../../../src/preconditions/Cooldown.ts"],"sourcesContent":["import { container } from '@sapphire/pieces';\nimport { RateLimitManager } from '@sapphire/ratelimits';\nimport {\n\tTimestampStyles,\n\ttime,\n\ttype ChatInputCommandInteraction,\n\ttype CommandInteraction,\n\ttype ContextMenuCommandInteraction,\n\ttype Message,\n\ttype Snowflake\n} from 'discord.js';\nimport { Identifiers } from '../lib/errors/Identifiers';\nimport type { Command } from '../lib/structures/Command';\nimport { AllFlowsPrecondition } from '../lib/structures/Precondition';\nimport { BucketScope } from '../lib/types/Enums';\n\nexport interface CooldownPreconditionContext extends AllFlowsPrecondition.Context {\n\tscope?: BucketScope;\n\tdelay: number;\n\tlimit?: number;\n\tfilteredUsers?: Snowflake[];\n}\n\nexport class CorePrecondition extends AllFlowsPrecondition {\n\tpublic buckets: WeakMap<Command, RateLimitManager<string>> = new WeakMap<Command, RateLimitManager<string>>();\n\n\tpublic messageRun(message: Message, command: Command, context: CooldownPreconditionContext): AllFlowsPrecondition.Result {\n\t\tconst cooldownId = this.getIdFromMessage(message, context);\n\n\t\treturn this.sharedRun(message.author.id, command, context, cooldownId, 'message');\n\t}\n\n\tpublic chatInputRun(\n\t\tinteraction: ChatInputCommandInteraction,\n\t\tcommand: Command,\n\t\tcontext: CooldownPreconditionContext\n\t): AllFlowsPrecondition.Result {\n\t\tconst cooldownId = this.getIdFromInteraction(interaction, context);\n\n\t\treturn this.sharedRun(interaction.user.id, command, context, cooldownId, 'chat input');\n\t}\n\n\tpublic contextMenuRun(\n\t\tinteraction: ContextMenuCommandInteraction,\n\t\tcommand: Command,\n\t\tcontext: CooldownPreconditionContext\n\t): AllFlowsPrecondition.Result {\n\t\tconst cooldownId = this.getIdFromInteraction(interaction, context);\n\n\t\treturn this.sharedRun(interaction.user.id, command, context, cooldownId, 'context menu');\n\t}\n\n\tprivate sharedRun(\n\t\tauthorId: string,\n\t\tcommand: Command,\n\t\tcontext: CooldownPreconditionContext,\n\t\tcooldownId: string,\n\t\tcommandType: string\n\t): AllFlowsPrecondition.Result {\n\t\t// If the command it is testing for is not this one, return ok:\n\t\tif (context.external) return this.ok();\n\n\t\t// If there is no delay (undefined, null, 0), return ok:\n\t\tif (!context.delay) return this.ok();\n\n\t\t// If the user has provided any filtered users and the authorId is in that array, return ok:\n\t\tif (context.filteredUsers?.includes(authorId)) return this.ok();\n\n\t\tconst rateLimit = this.getManager(command, context).acquire(cooldownId);\n\n\t\tif (rateLimit.limited) {\n\t\t\tconst remaining = rateLimit.remainingTime;\n\t\t\tconst nextAvailable = time(Math.floor(rateLimit.expires / 1000), TimestampStyles.RelativeTime);\n\n\t\t\treturn this.error({\n\t\t\t\tidentifier: Identifiers.PreconditionCooldown,\n\t\t\t\tmessage: `There is a cooldown in effect for this ${commandType} command. It'll be available ${nextAvailable}.`,\n\t\t\t\tcontext: { remaining }\n\t\t\t});\n\t\t}\n\n\t\trateLimit.consume();\n\t\treturn this.ok();\n\t}\n\n\tprivate getIdFromMessage(message: Message, context: CooldownPreconditionContext) {\n\t\tswitch (context.scope) {\n\t\t\tcase BucketScope.Global:\n\t\t\t\treturn 'global';\n\t\t\tcase BucketScope.Channel:\n\t\t\t\treturn message.channelId;\n\t\t\tcase BucketScope.Guild:\n\t\t\t\treturn message.guildId ?? message.channelId;\n\t\t\tdefault:\n\t\t\t\treturn message.author.id;\n\t\t}\n\t}\n\n\tprivate getIdFromInteraction(interaction: CommandInteraction, context: CooldownPreconditionContext) {\n\t\tswitch (context.scope) {\n\t\t\tcase BucketScope.Global:\n\t\t\t\treturn 'global';\n\t\t\tcase BucketScope.Channel:\n\t\t\t\treturn interaction.channelId;\n\t\t\tcase BucketScope.Guild:\n\t\t\t\treturn interaction.guildId ?? interaction.channelId;\n\t\t\tdefault:\n\t\t\t\treturn interaction.user.id;\n\t\t}\n\t}\n\n\tprivate getManager(command: Command, context: CooldownPreconditionContext) {\n\t\tlet manager = this.buckets.get(command);\n\t\tif (!manager) {\n\t\t\tmanager = new RateLimitManager(context.delay, context.limit);\n\t\t\tthis.buckets.set(command, manager);\n\t\t}\n\t\treturn manager;\n\t}\n}\n\nvoid container.stores.loadPiece({\n\tname: 'Cooldown',\n\tpiece: CorePrecondition,\n\tstore: 'preconditions'\n});\n"],"mappings":";;;;;;;;;AAuBA,IAAa,mBAAb,cAAsCA,yDAAqB;;;OACnD,0BAAsD,IAAI,SAA4C;;CAE7G,AAAO,WAAW,SAAkB,SAAkB,SAAmE;EACxH,MAAM,aAAa,KAAK,iBAAiB,SAAS,QAAQ;AAE1D,SAAO,KAAK,UAAU,QAAQ,OAAO,IAAI,SAAS,SAAS,YAAY,UAAU;;CAGlF,AAAO,aACN,aACA,SACA,SAC8B;EAC9B,MAAM,aAAa,KAAK,qBAAqB,aAAa,QAAQ;AAElE,SAAO,KAAK,UAAU,YAAY,KAAK,IAAI,SAAS,SAAS,YAAY,aAAa;;CAGvF,AAAO,eACN,aACA,SACA,SAC8B;EAC9B,MAAM,aAAa,KAAK,qBAAqB,aAAa,QAAQ;AAElE,SAAO,KAAK,UAAU,YAAY,KAAK,IAAI,SAAS,SAAS,YAAY,eAAe;;CAGzF,AAAQ,UACP,UACA,SACA,SACA,YACA,aAC8B;AAE9B,MAAI,QAAQ,SAAU,QAAO,KAAK,IAAI;AAGtC,MAAI,CAAC,QAAQ,MAAO,QAAO,KAAK,IAAI;AAGpC,MAAI,QAAQ,eAAe,SAAS,SAAS,CAAE,QAAO,KAAK,IAAI;EAE/D,MAAM,YAAY,KAAK,WAAW,SAAS,QAAQ,CAAC,QAAQ,WAAW;AAEvE,MAAI,UAAU,SAAS;GACtB,MAAM,YAAY,UAAU;GAC5B,MAAM,qCAAqB,KAAK,MAAM,UAAU,UAAU,IAAK,EAAEC,2BAAgB,aAAa;AAE9F,UAAO,K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