-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from ajsaraujo/feature/botless-channels
Added botless channels support and wrote tests for CommandParserHandler.
- Loading branch information
Showing
4 changed files
with
153 additions
and
1 deletion.
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
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,11 @@ | ||
import config from "../config.json"; | ||
|
||
function getKeyValue(key: string, obj: Record<string, any>) { | ||
return obj[key]; | ||
} | ||
|
||
function getConfigValue(key: string) { | ||
return getKeyValue(key, config); | ||
} | ||
|
||
export default getConfigValue; |
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,131 @@ | ||
import { expect } from "chai"; | ||
import { Constants } from "discord.js"; | ||
import { Message } from "discord.js"; | ||
import { SinonSandbox, createSandbox } from "sinon"; | ||
|
||
import MockCommand from "../../MockCommand"; | ||
import MockDiscord from "../../MockDiscord"; | ||
import CommandFactory from "../../../src/factories/CommandFactory"; | ||
import CommandParserHandler from "../../../src/event/handlers/CommandParserHandler"; | ||
import * as getConfigValue from "../../../src/utils/getConfigValue"; | ||
import { COMMAND_PREFIX } from "../../../src/config.json"; | ||
|
||
describe("CommandParserHandler", () => { | ||
describe("constructor()", () => { | ||
let sandbox: SinonSandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox(); | ||
}); | ||
|
||
it("creates a handler for MESSAGE_CREATE", () => { | ||
const handler = new CommandParserHandler(); | ||
|
||
expect(handler.getEvent()).to.equal(Constants.Events.MESSAGE_CREATE); | ||
}); | ||
|
||
it("should call loadCommands()", () => { | ||
const factoryMock = sandbox.stub(CommandFactory.prototype, "loadCommands"); | ||
|
||
const handler = new CommandParserHandler(); | ||
|
||
expect(factoryMock.calledOnce).to.be.true; | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
}); | ||
|
||
describe("handle", () => { | ||
let sandbox: SinonSandbox; | ||
let handler: CommandParserHandler; | ||
let discordMock: MockDiscord; | ||
let command: MockCommand; | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox(); | ||
handler = new CommandParserHandler(); | ||
discordMock = new MockDiscord(); | ||
command = new MockCommand(); | ||
}); | ||
|
||
it("should not run command if it doesn't start with command prefix", async () => { | ||
sandbox.stub(CommandFactory.prototype, "commandExists").returns(true); | ||
|
||
const runCommandMock = sandbox.stub(command, "run"); | ||
const message = discordMock.getMessage(); | ||
|
||
message.content = command.getName(); | ||
|
||
await handler.handle(message); | ||
|
||
expect(runCommandMock.called).to.be.false; | ||
}); | ||
|
||
it("should not run command if message was sent on a botless channel", async () => { | ||
sandbox.stub(CommandFactory.prototype, "commandExists").returns(true); | ||
sandbox.stub(getConfigValue, "default").returns({ MOCK_CHANNEL: "mock-channel-lol"}); | ||
|
||
const runCommandMock = sandbox.stub(command, "run"); | ||
const message = discordMock.getMessage(); | ||
|
||
message.content = COMMAND_PREFIX + command.getName(); | ||
message.channel.id = "mock-channel-lol"; | ||
|
||
await handler.handle(message); | ||
|
||
expect(runCommandMock.called).to.be.false; | ||
}); | ||
|
||
it("should not run a nonexistent command", async () => { | ||
sandbox.stub(CommandFactory.prototype, "commandExists").returns(false); | ||
|
||
const runCommandMock = sandbox.stub(command, "run"); | ||
const message = discordMock.getMessage(); | ||
|
||
message.content = COMMAND_PREFIX + command.getName(); | ||
message.channel.id = "fake-bot-enabled-channel-id-123"; | ||
|
||
await handler.handle(message); | ||
|
||
expect(runCommandMock.called).to.be.false; | ||
}); | ||
|
||
it("should run command", async () => { | ||
sandbox.stub(CommandFactory.prototype, "commandExists").returns(true); | ||
sandbox.stub(CommandFactory.prototype, "getCommand").returns(command); | ||
|
||
const runCommandMock = sandbox.stub(command, "run"); | ||
const message = discordMock.getMessage(); | ||
|
||
message.content = COMMAND_PREFIX + command.getName(); | ||
message.channel.id = "fake-bot-enabled-channel-id-123"; | ||
|
||
await handler.handle(message); | ||
|
||
expect(runCommandMock.called).to.be.true; | ||
}); | ||
|
||
it("should delete messages that trigger a self destructing command", async () => { | ||
sandbox.stub(CommandFactory.prototype, "commandExists").returns(true); | ||
sandbox.stub(CommandFactory.prototype, "getCommand").returns(command); | ||
sandbox.stub(MockCommand.prototype, "isSelfDestructing").returns(true); | ||
|
||
const deleteMessageMock = sandbox.stub(Message.prototype, "delete"); | ||
const message = discordMock.getMessage(); | ||
|
||
message.content = COMMAND_PREFIX + command.getName(); | ||
message.channel.id = "fake-bot-enabled-channel-id-123"; | ||
|
||
await handler.handle(message); | ||
|
||
expect(deleteMessageMock.called).to.be.true; | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.reset(); | ||
sandbox.restore(); | ||
}); | ||
}); | ||
}); |