Skip to content

Commit

Permalink
fix: sub commands
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Oct 21, 2024
1 parent 532261d commit 1bd11a8
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/cooldown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"lint": "biome lint --write ./src",
"format": "biome format --write ./src",
"checkb": "biome check --write --no-errors-on-unmatched ./src",
"test": "vitest run --config ./test/vitest.config.ts ./test/"
"test": "vitest run --config ./test/vitest.config.mts ./test/"
},
"devDependencies": {
"@types/node": "^22.5.5",
Expand Down
18 changes: 9 additions & 9 deletions packages/cooldown/src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyContext } from 'seyfert';
import type { AnyContext, SubCommand } from 'seyfert';
import type { ReturnCache } from 'seyfert/lib/cache';
import type { BaseClient } from 'seyfert/lib/client/base';
import { type MakePartial, fakePromise } from 'seyfert/lib/common';
Expand All @@ -16,7 +16,14 @@ export class CooldownManager {
* @returns The cooldown data for the command
*/
getCommandData(name: string): CooldownProps | undefined {
return this.client.commands?.values.find(x => x.name === name)?.cooldown;
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;
}
return undefined;
}

/**
Expand Down Expand Up @@ -166,13 +173,6 @@ declare module 'seyfert' {
interface ContextMenuCommand {
cooldown?: CooldownProps;
}

interface ComponentCommand {
cooldown?: CooldownProps;
}
interface ModalCommand {
cooldown?: CooldownProps;
}
interface EntryPointCommand {
cooldown?: CooldownProps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ describe('CooldownManager', async () => {
uses: 3,
};
handler.values = [
// @ts-expect-error
{
name: 'testCommand',
description: 'aaaa',
cooldown: cooldownData,
options: [
// @ts-expect-error
{
name: 'testSub',
description: 'aaa',
cooldown: cooldownData,
},
],
},
];

Expand All @@ -41,7 +49,7 @@ describe('CooldownManager', async () => {
cooldownManager = new CooldownManager(client);
});

await test('Data should return cooldown data for a command', () => {
test('Data should return cooldown data for a command', () => {
const data = cooldownManager.getCommandData('testCommand');
assert.deepEqual(data, {
type: CooldownType.User,
Expand All @@ -50,45 +58,63 @@ describe('CooldownManager', async () => {
});
});

await test('Data should return undefined for non-existent command', () => {
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,
});
});

test('Data should return undefined for non-existent command', () => {
const data = cooldownManager.getCommandData('nonExistentCommand');
assert.equal(data, undefined);
});

await test('has should return false for a new cooldown', () => {
test('has should return false for a new cooldown', () => {
const result = cooldownManager.has('testCommand', 'user1');
assert.equal(result, false);
});

await test('has should return true when cooldown is active', () => {
test('has should return true when cooldown is active', () => {
for (let i = 0; i < cooldownData.uses; i++) {
cooldownManager.use('testCommand', 'user1');
}
const result = cooldownManager.has('testCommand', 'user1');
assert.equal(result, true);
});

await test('use should set cooldown when used for the first time', () => {
test('use should set cooldown when used for the first time', () => {
const result = cooldownManager.use('testCommand', 'user2');
assert.equal(result, true);
});

await test('use should return time left when cooldown is active', () => {
test('use should return time left when cooldown is active', () => {
for (let i = 0; i < cooldownData.uses; i++) {
cooldownManager.use('testCommand', 'user3');
}
const result = cooldownManager.use('testCommand', 'user3');
assert.ok(typeof result === 'number');
});

await test('refill should refill the cooldown', () => {
test('refill should refill the cooldown', () => {
cooldownManager.use('testCommand', 'user1');
const result = cooldownManager.refill('testCommand', 'user1');
assert.equal(result, true);
assert.equal(cooldownManager.has('testCommand', 'user1'), false);
});

await test('drip should drip the cooldown over time', async () => {
test('drip should drip the cooldown over time', async () => {
cooldownManager.use('testCommand', 'user1');

// Simulate time passing
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/redis-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "tsc",
"lint": "biome lint --write ./src",
"format": "biome format --write ./src",
"test": "vitest run --config ./test/vitest.config.ts ./test/",
"test": "vitest run --config ./test/vitest.config.mts ./test/",
"checkb": "biome check --write --no-errors-on-unmatched ./src",
"test:adapter": "node --test ./test/adapter.test.js",
"test:cache": "node --test ./test/cache.test.js",
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 1bd11a8

Please sign in to comment.