From a4eab1cbd07656b01160f7d1ff7b6f613986600e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Mon, 21 Oct 2024 18:13:04 -0400 Subject: [PATCH] fix: subs improvements --- packages/cooldown/src/manager.ts | 36 ++++++++------ packages/cooldown/test/manager.test.mts | 66 +++++++++++++++++-------- 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/packages/cooldown/src/manager.ts b/packages/cooldown/src/manager.ts index 3536f21..5e12a62 100644 --- a/packages/cooldown/src/manager.ts +++ b/packages/cooldown/src/manager.ts @@ -15,13 +15,17 @@ export class CooldownManager { * @param name - The name of the command * @returns The cooldown data for the command */ - getCommandData(name: string): CooldownProps | undefined { + getCommandData(name: string): [name: string, data: CooldownProps | undefined] | undefined { if (!this.client.commands?.values?.length) return; for (const command of this.client.commands.values) { if (!('cooldown' in command)) continue; - if (command.name === name) return command.cooldown; - if ('options' in command) - return command.options?.find((x): x is SubCommand => 'cooldown' in x && x.name === name)?.cooldown; + if (command.name === name) return [command.name, command.cooldown]; + if ('options' in command) { + const option = command.options?.find((x): x is SubCommand => x.name === name); + if (option) { + return [option.name, option.cooldown ?? command.cooldown]; + } + } } return undefined; } @@ -33,14 +37,14 @@ export class CooldownManager { * @returns Whether the user has a cooldown */ has(name: string, target: string, tokens = 1): ReturnCache { - const data = this.getCommandData(name); - if (!data) return false; + const [resolve, data] = this.getCommandData(name) ?? []; + if (!(data && resolve)) return false; - return fakePromise(this.resource.get(`${name}:${data.type}:${target}`)).then(cooldown => { + return fakePromise(this.resource.get(`${resolve}:${data.type}:${target}`)).then(cooldown => { if (tokens > data.uses) return true; if (!cooldown) { return fakePromise( - this.set(name, target, { type: data.type, interval: data.interval, remaining: data.uses }), + this.set(resolve, target, { type: data.type, interval: data.interval, remaining: data.uses }), ).then(() => false); } @@ -89,13 +93,13 @@ export class CooldownManager { * @returns The remaining cooldown in seconds or true if successful */ use(name: string, target: string): ReturnCache { - const data = this.getCommandData(name); - if (!data) return true; + const [resolve, data] = this.getCommandData(name) ?? []; + if (!(data && resolve)) return true; - return fakePromise(this.resource.get(`${name}:${data.type}:${target}`)).then(cooldown => { + return fakePromise(this.resource.get(`${resolve}:${data.type}:${target}`)).then(cooldown => { if (!cooldown) { return fakePromise( - this.set(name, target, { + this.set(resolve, target, { type: data.type, interval: data.interval, remaining: data.uses - 1, @@ -103,7 +107,7 @@ export class CooldownManager { ).then(() => true); } - return fakePromise(this.drip(name, target, data, cooldown)).then(drip => { + return fakePromise(this.drip(resolve, target, data, cooldown)).then(drip => { return typeof drip === 'number' ? data.interval - drip : true; }); }); @@ -145,10 +149,10 @@ export class CooldownManager { * @returns Whether the cooldown was refilled */ refill(name: string, target: string) { - const data = this.getCommandData(name); - if (!data) return false; + const [resolve, data] = this.getCommandData(name) ?? []; + if (!(data && resolve)) return false; - return fakePromise(this.resource.patch(`${name}:${data.type}:${target}`, { remaining: data.uses })).then( + return fakePromise(this.resource.patch(`${resolve}:${data.type}:${target}`, { remaining: data.uses })).then( () => true, ); } diff --git a/packages/cooldown/test/manager.test.mts b/packages/cooldown/test/manager.test.mts index 3baf6fe..7aac6e2 100644 --- a/packages/cooldown/test/manager.test.mts +++ b/packages/cooldown/test/manager.test.mts @@ -39,6 +39,11 @@ describe('CooldownManager', async () => { description: 'aaa', cooldown: cooldownData, }, + // @ts-expect-error + { + name: 'testSubNon', + description: 'aaa', + }, ], }, ]; @@ -51,29 +56,26 @@ describe('CooldownManager', async () => { test('Data should return cooldown data for a command', () => { const data = cooldownManager.getCommandData('testCommand'); - assert.deepEqual(data, { - type: CooldownType.User, - interval: 1000, - uses: 3, - }); + assert.deepEqual(data, [ + 'testCommand', + { + type: CooldownType.User, + interval: 1000, + uses: 3, + }, + ]); }); test('Data should return cooldown data for a subcommand', () => { const data = cooldownManager.getCommandData('testSub'); - assert.deepEqual(data, { - type: CooldownType.User, - interval: 1000, - uses: 3, - }); - }); - - test('Data should return cooldown data for a command', () => { - const data = cooldownManager.getCommandData('testCommand'); - assert.deepEqual(data, { - type: CooldownType.User, - interval: 1000, - uses: 3, - }); + assert.deepEqual(data, [ + 'testSub', + { + type: CooldownType.User, + interval: 1000, + uses: 3, + }, + ]); }); test('Data should return undefined for non-existent command', () => { @@ -81,6 +83,30 @@ describe('CooldownManager', async () => { assert.equal(data, undefined); }); + test('Data should return cooldown data parent for a non-existent subcommand', () => { + const data = cooldownManager.getCommandData('testSub'); + assert.deepEqual(data, [ + 'testSub', + { + type: CooldownType.User, + interval: 1000, + uses: 3, + }, + ]); + }); + + test('Data should return cooldown data parent for a subcommand without cooldown data ', () => { + const data = cooldownManager.getCommandData('testSubNon'); + assert.deepEqual(data, [ + 'testSubNon', + { + type: CooldownType.User, + interval: 1000, + uses: 3, + }, + ]); + }); + test('has should return false for a new cooldown', () => { const result = cooldownManager.has('testCommand', 'user1'); assert.equal(result, false); @@ -122,7 +148,7 @@ describe('CooldownManager', async () => { const data = cooldownManager.resource.get('testCommand:user:user1'); const props = cooldownManager.getCommandData('testCommand'); - await cooldownManager.drip('testCommand', 'user1', props!, data!); + await cooldownManager.drip('testCommand', 'user1', props![1]!, data!); const getter = cooldownManager.resource.get('testCommand:user:user1'); assert.ok(getter?.remaining === 2); });