Skip to content

Commit

Permalink
feat: basic interactions service
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Jul 9, 2024
1 parent f2655d9 commit af3f0fc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
33 changes: 33 additions & 0 deletions services/interactions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@chatsift/automoderator-interactions",
"main": "./dist/index.js",
"private": true,
"version": "1.0.0",
"type": "module",
"scripts": {
"lint": "eslint src",
"build": "tsc"
},
"engines": {
"node": ">=16.9.0"
},
"devDependencies": {
"@types/node": "^20.14.10",
"pino": "^9.2.0",
"typescript": "^5.4.5"
},
"dependencies": {
"@automoderator/core": "workspace:^",
"@chatsift/readdir": "^0.5.0",
"@discordjs/brokers": "^0.3.0",
"@discordjs/core": "^1.2.0",
"@discordjs/rest": "^2.3.0",
"@discordjs/ws": "^1.1.1",
"coral-command": "^0.4.1",
"inversify": "^6.0.2",
"ioredis": "5.3.2",
"reflect-metadata": "^0.2.2",
"tslib": "^2.6.3",
"undici": "^6.19.2"
}
}
52 changes: 52 additions & 0 deletions services/interactions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'reflect-metadata';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
type ICommandHandler,
CoralCommandHandler,
globalContainer,
DependencyManager,
setupCrashLogs,
encode,
decode,
type DiscordGatewayEventsMap,
USEFUL_HANDLERS_PATH,
type HandlerModuleConstructor,
type HandlerModule,
} from '@automoderator/core';
import { readdirRecurseManyAsync } from '@chatsift/readdir';
import { PubSubRedisBroker } from '@discordjs/brokers';
import { GatewayDispatchEvents } from '@discordjs/core';
import type { InteractionHandler as CoralInteractionHandler } from 'coral-command';

const dependencyManager = globalContainer.get(DependencyManager);
const logger = dependencyManager.registerLogger('interactions');
const redis = dependencyManager.registerRedis();
dependencyManager.registerApi();

setupCrashLogs();

const commandHandler = globalContainer.get<ICommandHandler<CoralInteractionHandler>>(CoralCommandHandler);

export const serviceHandlersPath = join(dirname(fileURLToPath(import.meta.url)), 'handlers');

for (const path of await readdirRecurseManyAsync([serviceHandlersPath, USEFUL_HANDLERS_PATH])) {
const moduleConstructor: HandlerModuleConstructor<CoralInteractionHandler> = await import(path);
const module = globalContainer.get<HandlerModule<CoralInteractionHandler>>(moduleConstructor);

module.register(commandHandler);
logger.info(`Loaded handler/module ${module.constructor.name}`);
}

const broker = new PubSubRedisBroker<DiscordGatewayEventsMap>({
redisClient: redis,
encode,
decode,
});

broker.on(GatewayDispatchEvents.InteractionCreate, async ({ data: interaction, ack }) => {
await commandHandler.handle(interaction);
await ack();
});

await broker.subscribe('interactions', [GatewayDispatchEvents.InteractionCreate]);
18 changes: 18 additions & 0 deletions services/interactions/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"allowJs": true
},
"include": [
"**/*.ts",
"**/*.tsx",
"**/*.js",
"**/*.cjs",
"**/*.jsx",
"**/*.test.ts",
"**/*.test.js",
"**/*.spec.ts",
"**/*.spec.js"
],
"exclude": []
}
8 changes: 8 additions & 0 deletions services/interactions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src/**/*.ts"],
"exclude": ["./**/__tests__"]
}

0 comments on commit af3f0fc

Please sign in to comment.