diff --git a/plugins/cooldown.ts b/plugins/cooldown.ts index 38ffd2e..04943d6 100644 --- a/plugins/cooldown.ts +++ b/plugins/cooldown.ts @@ -2,17 +2,17 @@ /** * @plugin * Allows you to set cooldowns (or "ratelimits") for commands, limits user/channel/guild actions. + * An extra function cooldown2 is exported if you want your cooldown to be local to the command. * @author @trueharuu [<@504698587221852172>] * @version 1.0.0 * @example * ```ts - * import { cooldown } from "../plugins/cooldown"; + * import { cooldown, cooldown2 } from "../plugins/cooldown"; * import { commandModule } from "@sern/handler"; + * //IF you want this cooldown to be local to this command, which fixes an issue with * export default commandModule({ * plugins: [cooldown.add( [ ['channel', '1/4'] ] )], // limit to 1 action every 4 seconds per channel - * execute: (ctx) => { - * //your code here - * } + * execute: (ctx) => { //your code here } * }) * ``` * @end @@ -60,7 +60,6 @@ export class ExpiryMap extends Map { } } -export const map = new ExpiryMap(); function parseCooldown( location: CooldownLocation, @@ -107,17 +106,16 @@ export interface RecievedCooldown { } type CooldownResponse = (cooldown: RecievedCooldown) => any; -function add( - items: Array< - | [CooldownLocation | keyof typeof CooldownLocation, CooldownString] - | Cooldown - >, - message?: CooldownResponse, -) { +function __add(map : ExpiryMap, + items: Array<| [CooldownLocation + | keyof typeof CooldownLocation, CooldownString] + | Cooldown>, + message?: CooldownResponse) { const raw = items.map((c) => { if (!Array.isArray(c)) return c; return parseCooldown(c[0] as CooldownLocation, c[1]); }) as Array; + return CommandControlPlugin(async (context, args) => { for (const { location, actions, seconds } of raw) { const id = getPropertyForLocation(context, location); @@ -149,15 +147,33 @@ function add( type Location = (value: CooldownString) => ReturnType; -const locations: Record = { +const locationsFn = (map: ExpiryMap)=> ({ [CooldownLocation.channel]: (value) => - add([[CooldownLocation.channel, value]]), - [CooldownLocation.user]: (value) => add([[CooldownLocation.user, value]]), - [CooldownLocation.guild]: (value) => add([[CooldownLocation.guild, value]]), + __add([[CooldownLocation.channel, value]]), + [CooldownLocation.user]: (value) => __add([[CooldownLocation.user, value]]), + [CooldownLocation.guild]: (value) => __add([[CooldownLocation.guild, value]]), +} as Record); + + + +export const cooldown2 = () => { + const cooldownMap = new Map(); + return { + add:(items: Array<| [CooldownLocation + | keyof typeof CooldownLocation, CooldownString] + | Cooldown>, + message?: CooldownResponse) => __add(cooldownMap, items, message), + locations: locationsFn(cooldownMap), + } }; +const map = new ExpiryMap(); export const cooldown = { - add, - locations, - map, -}; + map, + locations: locationsFn(map), + add: (items: Array<| [CooldownLocation + | keyof typeof CooldownLocation, CooldownString] + | Cooldown>, + message?: CooldownResponse) => __add(map, items, message) +} +