-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): new way to register commands
Use SpectrumState.commands.registerCommands() method to quickly register commands either via a glob or a static array BREAKING CHANGE: You cannot instantiate SpectrumCommands on your own, but need to use the SpectrumState.commands instance instead.
- Loading branch information
Showing
9 changed files
with
264 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Container } from "typedi"; | ||
import { SpectrumLobby } from "./../src/"; | ||
import { SpectrumChannel } from "./../src/"; | ||
import { SpectrumBroadcaster } from "./../src/"; | ||
import { SpectrumCommunity, SpectrumCommands } from "../src/"; | ||
import { TestInstance } from "./_.instance"; | ||
import { TestShared } from "./_.shared"; | ||
|
||
import {} from "jasmine"; | ||
import { SpectrumCommand } from "../src/Spectrum/components/api/decorators/spectrum-command.decorator"; | ||
|
||
describe("Spectrum Commands", () => { | ||
TestShared.commonSetUp(); | ||
|
||
let commands: SpectrumCommands; | ||
beforeEach(() => { | ||
commands = TestInstance.bot.getState().commands; | ||
}); | ||
|
||
describe("Service", () => { | ||
it("Should expose the command service", async () => { | ||
expect(commands).toBeTruthy(); | ||
expect(commands instanceof SpectrumCommands).toBe(true); | ||
}); | ||
|
||
it("Should register commands through glob", async () => { | ||
await commands.registerCommands({ commands: ["spec/mock/commands/*.ts"] }); | ||
|
||
const registered = commands.getCommandList(); | ||
|
||
expect(registered.find(command => command.shortCode === "testCommand1")).toBeTruthy(); | ||
expect(registered.find(command => command.shortCode === "testCommand2")).toBeTruthy(); | ||
}); | ||
|
||
it("Should register commands through array", async () => { | ||
const test3 = class { | ||
callback = () => {}; | ||
}; | ||
SpectrumCommand("test3")(test3); | ||
|
||
const test4 = class { | ||
callback = () => {}; | ||
}; | ||
SpectrumCommand("test4")(test4); | ||
await commands.registerCommands({ commands: [test3, test4] }); | ||
|
||
const registered = commands.getCommandList(); | ||
|
||
expect(registered.find(command => command.shortCode === "test3")).toBeTruthy(); | ||
expect(registered.find(command => command.shortCode === "test4")).toBeTruthy(); | ||
}); | ||
|
||
it("Should not register commands that are not decorated", async () => { | ||
const test = class { | ||
callback = () => {}; | ||
}; | ||
|
||
await commands.registerCommands({ commands: [test] }); | ||
|
||
const registered = commands.getCommandList(); | ||
|
||
expect(registered.find(command => command.shortCode === "test5")).toBeUndefined(); | ||
}); | ||
|
||
it("Should call the callback provided on", done => { | ||
TestShared.testLobbyDependentSetUp(); | ||
TestInstance.lobby = TestInstance.bot | ||
.getState() | ||
.getCommunityByName(TestShared.config._testCommunity) | ||
.getLobbyByName(TestShared.config._testLobby); | ||
commands.setPrefix("[UNIT]"); | ||
|
||
const testCallback = class { | ||
callback = () => { | ||
TestInstance.lobby.unSubscribe(); | ||
done(); | ||
}; | ||
}; | ||
SpectrumCommand("test callback")(testCallback); | ||
|
||
commands.getCommandList(); | ||
|
||
TestInstance.lobby.subscribe(); | ||
TestInstance.lobby.sendPlainTextMessage("[UNIT] test callback"); | ||
}); | ||
}); | ||
|
||
describe("Decorator", () => { | ||
it("Should decorate the class", () => { | ||
const test = class { | ||
callback = () => {}; | ||
}; | ||
SpectrumCommand("test")(test); | ||
expect(Reflect.getMetadata("spectrum-command", test)).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SpectrumCommand } from "../../../src/Spectrum/components/api/decorators/spectrum-command.decorator"; | ||
|
||
@SpectrumCommand("testCommand1") | ||
export class TestCommand1 { | ||
public callback() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SpectrumCommand } from "../../../src/Spectrum/components/api/decorators/spectrum-command.decorator"; | ||
|
||
@SpectrumCommand("testCommand2") | ||
export class TestCommand2 { | ||
public callback() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Spectrum/components/api/decorators/spectrum-command.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @module Spectrum | ||
*/ /** */ | ||
import { Container } from "typedi"; | ||
import { aSpectrumCommand } from "../../../interfaces/api/command.interface"; | ||
import { SpectrumCommands } from "../../../services/commands.service"; | ||
|
||
/** | ||
* Decorate a class as being a command a bot can listen to | ||
* | ||
* **example:** | ||
*```typescript | ||
* @SpectrumCommand("my-command") | ||
* export class MyCommand { | ||
* public async callback(textMessage, lobby) { | ||
* // This will be called automatically when someone says "my-command" | ||
* } | ||
*} | ||
* ``` | ||
* | ||
* Do **not** forget to register your command via SpectrumCommands.registerCommands() | ||
*/ | ||
export function SpectrumCommand(opts: SpectrumCommandOpts): SpectrumCommandHandlerClass; | ||
export function SpectrumCommand( | ||
shortCode: SpectrumCommandOpts["shortCode"] | ||
): SpectrumCommandHandlerClass; | ||
export function SpectrumCommand( | ||
opts: SpectrumCommandOpts | SpectrumCommandOpts["shortCode"] | ||
): SpectrumCommandHandlerClass { | ||
let options: SpectrumCommandOpts; | ||
if (typeof opts === "string" || opts instanceof RegExp) { | ||
options = { shortCode: opts }; | ||
} else { | ||
options = { ...opts }; | ||
} | ||
|
||
return ( | ||
Class: new (...any: any[]) => { | ||
callback: aSpectrumCommand["callback"]; | ||
} | ||
) => { | ||
Reflect.defineMetadata("spectrum-command", true, Class); | ||
const instance = new Class(); | ||
Container.get(SpectrumCommands).registerCommand( | ||
options.name || "", | ||
options.shortCode, | ||
instance.callback.bind(instance), | ||
options.manual || "" | ||
); | ||
}; | ||
} | ||
|
||
export type SpectrumCommandHandlerClass = (target: SpectrumCommandMakable) => void; | ||
|
||
export type SpectrumCommandMakable = new (...any: any[]) => { | ||
callback: aSpectrumCommand["callback"]; | ||
}; | ||
|
||
/** | ||
* Options for constructing a spectrum bot command | ||
*/ | ||
export interface SpectrumCommandOpts { | ||
/** | ||
* the code that will trigger this command, excluding the prefix. | ||
* In case of a regex with capture groups, the resulting matches will be provided to the callback function | ||
* | ||
* **Example:** | ||
* *(prefix set to !bot)* | ||
* | ||
* shortCode: `testCommand` | ||
* Will match `!bot testCommand` | ||
**/ | ||
shortCode: string | RegExp; | ||
/** pretty name to be given internally to this command */ | ||
name?: string; | ||
/** manual entry for this command */ | ||
manual?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,9 @@ export class SpectrumLobby { | |
}; | ||
} | ||
|
||
/** | ||
* not implemented yet | ||
*/ | ||
public unSubscribe() {} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.