Skip to content

Commit

Permalink
Merge pull request #44 from ajsaraujo/feature/botless-channels
Browse files Browse the repository at this point in the history
Added botless channels support and wrote tests for CommandParserHandler.
  • Loading branch information
LamboCreeper authored Jun 18, 2020
2 parents f066e1d + 74fa6e8 commit dc56920
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"commands_directory": "commands",
"handlers_directory": "event/handlers",
"LOG_CHANNEL_ID": "405068878151024640",
"BOTLESS_CHANNELS": {
"HIRING_OR_LOOKING": "328146607486795777"
},
"EMBED_COLOURS": {
"SUCCESS": "#35BC31",
"ERROR": "#BC3131",
Expand Down
9 changes: 8 additions & 1 deletion src/event/handlers/CommandParserHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Constants, Message } from "discord.js";
import { COMMAND_PREFIX } from "../../config.json";
import EventHandler from "../../abstracts/EventHandler";
import CommandFactory from "../../factories/CommandFactory";
import getConfigValue from "../../utils/getConfigValue";
import { COMMAND_PREFIX } from "../../config.json";

class CommandParserHandler extends EventHandler {
private readonly commandFactory: CommandFactory;
Expand All @@ -15,6 +16,12 @@ class CommandParserHandler extends EventHandler {

handle = async (message: Message): Promise<void> => {
if (message.content.startsWith(COMMAND_PREFIX)) {
const BOTLESS_CHANNELS = getConfigValue("BOTLESS_CHANNELS");

if (Object.values(BOTLESS_CHANNELS).includes(message.channel.id)) {
return;
}

const args = message.content.replace("?", "").split(" ");
const trigger = args.shift() || args[0];

Expand Down
11 changes: 11 additions & 0 deletions src/utils/getConfigValue.ts
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;
131 changes: 131 additions & 0 deletions test/event/handlers/CommandParserHandlerTest.ts
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();
});
});
});

0 comments on commit dc56920

Please sign in to comment.