Skip to content

Commit

Permalink
fix: subs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Oct 21, 2024
1 parent 1bd11a8 commit a4eab1c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 36 deletions.
36 changes: 20 additions & 16 deletions packages/cooldown/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -33,14 +37,14 @@ export class CooldownManager {
* @returns Whether the user has a cooldown
*/
has(name: string, target: string, tokens = 1): ReturnCache<boolean> {
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);
}

Expand Down Expand Up @@ -89,21 +93,21 @@ export class CooldownManager {
* @returns The remaining cooldown in seconds or true if successful
*/
use(name: string, target: string): ReturnCache<number | true> {
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,
}),
).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;
});
});
Expand Down Expand Up @@ -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,
);
}
Expand Down
66 changes: 46 additions & 20 deletions packages/cooldown/test/manager.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe('CooldownManager', async () => {
description: 'aaa',
cooldown: cooldownData,
},
// @ts-expect-error
{
name: 'testSubNon',
description: 'aaa',
},
],
},
];
Expand All @@ -51,36 +56,57 @@ 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', () => {
const data = cooldownManager.getCommandData('nonExistentCommand');
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);
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit a4eab1c

Please sign in to comment.