From 25bd75547883588b9bd41ac265ad9f6a1e1fee9f Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 22 Jul 2024 21:24:30 -0500 Subject: [PATCH 1/6] hope dis works --- plugins/cooldown.ts | 56 +++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) 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) +} + From d03fcb29e6a2e641781a8a9927b952361281d626 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 22 Jul 2024 21:26:59 -0500 Subject: [PATCH 2/6] exposemap --- plugins/cooldown.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/cooldown.ts b/plugins/cooldown.ts index 04943d6..b790a35 100644 --- a/plugins/cooldown.ts +++ b/plugins/cooldown.ts @@ -164,6 +164,7 @@ export const cooldown2 = () => { | Cooldown>, message?: CooldownResponse) => __add(cooldownMap, items, message), locations: locationsFn(cooldownMap), + map: cooldownMap } }; From 21ec6bed853c0687f11efb9b4b334778fd8d8c66 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 22 Jul 2024 21:31:26 -0500 Subject: [PATCH 3/6] update doc --- plugins/cooldown.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/cooldown.ts b/plugins/cooldown.ts index b790a35..36ac348 100644 --- a/plugins/cooldown.ts +++ b/plugins/cooldown.ts @@ -9,9 +9,11 @@ * ```ts * 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 + * //IF you want this cooldown to be local to this command: + * const localCooldown = cooldown2() * export default commandModule({ - * plugins: [cooldown.add( [ ['channel', '1/4'] ] )], // limit to 1 action every 4 seconds per channel + * plugins: [cooldown.add( [ ['channel', '1/4'] ] ), // limit to 1 action every 4 seconds per channel + * localCooldown.add([["user", "1/10"]]) ], // limit to 1 action every 10 seconds * execute: (ctx) => { //your code here } * }) * ``` From da5c424509795b0a864b14e6c3533092b374f72f Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 22 Jul 2024 21:31:37 -0500 Subject: [PATCH 4/6] doc --- plugins/cooldown.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/cooldown.ts b/plugins/cooldown.ts index 36ac348..b703ee0 100644 --- a/plugins/cooldown.ts +++ b/plugins/cooldown.ts @@ -13,7 +13,7 @@ * const localCooldown = cooldown2() * export default commandModule({ * plugins: [cooldown.add( [ ['channel', '1/4'] ] ), // limit to 1 action every 4 seconds per channel - * localCooldown.add([["user", "1/10"]]) ], // limit to 1 action every 10 seconds + * localCooldown.add([["user", "1/10"]]) ], // limit to 1 action every 10 seconds, local to command * execute: (ctx) => { //your code here } * }) * ``` From 678bd7e34bf7e8530d240bea34aaf14f3f868012 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 22 Jul 2024 21:34:14 -0500 Subject: [PATCH 5/6] yea --- plugins/cooldown.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/cooldown.ts b/plugins/cooldown.ts index b703ee0..abadf0d 100644 --- a/plugins/cooldown.ts +++ b/plugins/cooldown.ts @@ -12,8 +12,8 @@ * //IF you want this cooldown to be local to this command: * const localCooldown = cooldown2() * export default commandModule({ - * plugins: [cooldown.add( [ ['channel', '1/4'] ] ), // limit to 1 action every 4 seconds per channel - * localCooldown.add([["user", "1/10"]]) ], // limit to 1 action every 10 seconds, local to command + * plugins: [cooldown.add([['channel', '1/4']]), // limit to 1 action every 4 seconds per channel + * localCooldown.add([["user", "1/10"]])], // limit to 1 action every 10 seconds, local to command * execute: (ctx) => { //your code here } * }) * ``` From 9bb403bc292367d630c018b5e12a1fb0e25ef9db Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 22 Jul 2024 21:40:08 -0500 Subject: [PATCH 6/6] more fixes --- plugins/cooldown.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/cooldown.ts b/plugins/cooldown.ts index abadf0d..6263fed 100644 --- a/plugins/cooldown.ts +++ b/plugins/cooldown.ts @@ -1,4 +1,3 @@ -// @ts-nocheck /** * @plugin * Allows you to set cooldowns (or "ratelimits") for commands, limits user/channel/guild actions. @@ -147,19 +146,18 @@ function __add(map : ExpiryMap, }); } -type Location = (value: CooldownString) => ReturnType; +type Location = (value: CooldownString) => ReturnType; 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]]), + [CooldownLocation.channel]: (value) => __add(map, [[CooldownLocation.channel, value]]), + [CooldownLocation.user]: (value) => __add(map, [[CooldownLocation.user, value]]), + [CooldownLocation.guild]: (value) => __add(map, [[CooldownLocation.guild, value]]), } as Record); export const cooldown2 = () => { - const cooldownMap = new Map(); + const cooldownMap = new ExpiryMap(); return { add:(items: Array<| [CooldownLocation | keyof typeof CooldownLocation, CooldownString]