Skip to content

Commit

Permalink
Fix DI not resolving injectables for event handlers (#299)
Browse files Browse the repository at this point in the history
* Add tsyringe to handle DI

* Fix DI not resolving injectables for event handlers
  • Loading branch information
LamboCreeper authored Aug 12, 2024
1 parent 3ad6fc8 commit e4c6df8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import DirectoryUtils from "./utils/DirectoryUtils";
import DiscordUtils from "./utils/DiscordUtils";
import getConfigValue from "./utils/getConfigValue";
import Schedule from "./decorators/Schedule";
import {container} from "tsyringe";
import {setupCache} from "axios-cache-interceptor";
import { container } from "tsyringe";
import { setupCache } from "axios-cache-interceptor";
import EventHandler from "./abstracts/EventHandler";

if (process.env.NODE_ENV !== getConfigValue<string>("PRODUCTION_ENV")) {
env({
Expand Down Expand Up @@ -64,7 +65,8 @@ class App {

handlerFiles.forEach(handler => {
const { default: Handler } = handler;
const handlerInstance = new Handler();

const handlerInstance = container.resolve<EventHandler>(Handler);

this.client.on(handlerInstance.getEvent(), handlerInstance.handle);
});
Expand Down
4 changes: 2 additions & 2 deletions src/event/handlers/DiscordMessageLinkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DiscordMessageLinkHandler extends EventHandler {
super(Events.MessageCreate);
}

async handle(message: Message): Promise<void> {
handle = async (message: Message) => {
const messageRegex = /https:\/\/(?:ptb\.)?discord(?:app)?\.com\/channels\/\d+\/\d+\/\d+/gm;
const matches = message.content.matchAll(messageRegex);

Expand All @@ -28,7 +28,7 @@ class DiscordMessageLinkHandler extends EventHandler {
}
}
}
}
};
}

export default DiscordMessageLinkHandler;
8 changes: 6 additions & 2 deletions test/appTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SinonSandbox, createSandbox, SinonStub } from "sinon";
import { Client, ChannelManager } from "discord.js";
import { BaseMocks } from "@lambocreeper/mock-discord.js";
import axios from "axios";
import { container } from "tsyringe";
import App from "../src/app";
import DirectoryUtils from "../src/utils/DirectoryUtils";
import { AUTHENTICATION_MESSAGE_CHANNEL, AUTHENTICATION_MESSAGE_ID, PRODUCTION_ENV } from "../src/config.json";
Expand Down Expand Up @@ -80,11 +81,14 @@ describe("App", () => {
sandbox.stub(DirectoryUtils, "getFilesInDirectory").callsFake(async () => [require("./MockHandler")]);
const onStub = sandbox.stub(Client.prototype, "on");

await new App().init();

const mockHandler = new MockHandler();

const resolveStub = sandbox.stub(container, "resolve").returns(mockHandler);

await new App().init();

expect(onStub.calledWith(mockHandler.getEvent())).to.be.true;
expect(resolveStub.calledWith(MockHandler)).to.be.true;
});

it("should fetch auth channel and messages in production environment", async () => {
Expand Down

0 comments on commit e4c6df8

Please sign in to comment.