Skip to content

Commit

Permalink
feat: default useful commands
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Jun 27, 2024
1 parent 7d72826 commit 4673153
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 20 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"turbo": "^2.0.4",
"typescript": "^5.4.5",
"typescript-eslint": "^7.13.1"
},
"resolutions": {
"discord-api-types": "0.37.84"
}
}
10 changes: 2 additions & 8 deletions packages/core/src/command-framework/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,13 @@ export class CommandHandler extends ICommandHandler {
}

public async handle(interaction: APIInteraction): Promise<void> {
if (!interaction.guild_id) {
const incident = await this.database.createIncident(new Error('Interaction not in guild'));
return this.reportIncident(interaction, incident);
}

switch (interaction.type) {
case InteractionType.Ping: {
this.logger.warn('Received a ping interaction. We should not receive these, as we run WS.');
this.logger.warn('Received a ping interaction. This CommandHandler is designed for Gateway.');
break;
}

case InteractionType.ApplicationCommand: {
// @ts-expect-error - discord api types version miss match
const options = new InteractionOptionResolver(interaction);

const { root, subcommand } = this.resolveCommandIdentifier(interaction, options);
Expand Down Expand Up @@ -106,7 +100,6 @@ export class CommandHandler extends ICommandHandler {
}

case InteractionType.ApplicationCommandAutocomplete: {
// @ts-expect-error - discord api types version miss match
const options = new InteractionOptionResolver(interaction);

const { root, subcommand } = this.resolveCommandIdentifier(interaction, options);
Expand Down Expand Up @@ -204,6 +197,7 @@ export class CommandHandler extends ICommandHandler {
return { root: identifier };
}

// TODO: Generalize

Check warning on line 200 in packages/core/src/command-framework/CommandHandler.ts

View workflow job for this annotation

GitHub Actions / Quality Check

Unexpected 'todo' comment: 'TODO: Generalize'
private async reportIncident(interaction: APIInteraction, incident: Selectable<Incident>): Promise<void> {
await this.api.interactions.reply(interaction.id, interaction.token, {
content: `An error occurred while processing your request. Please report this incident to the developers. (Incident ID: ${incident.id})`,
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/command-framework/ICommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type {
APIApplicationCommandAutocompleteInteraction,
APIApplicationCommandInteraction,
Expand Down Expand Up @@ -54,6 +56,14 @@ export type AutocompleteHandler = (
*/
export type ModalHandler = (interaction: APIModalSubmitInteraction, args: string[]) => Promise<void>;

export interface HandlerModule {
register(handler: ICommandHandler): void;
}

export type HandlerModuleConstructor = new (...args: unknown[]) => HandlerModule;

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

export interface RegisterOptions {
applicationCommands?: [ApplicationCommandIdentifier, ApplicationCommandHandler][];
autocomplete?: [AutocompleteIdentifier, AutocompleteHandler][];
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/command-framework/handlers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# command-framework/handlers

This folder contains some useful bot-agnostic handlers that we can use in any bot in the monorepo.
45 changes: 45 additions & 0 deletions packages/core/src/command-framework/handlers/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { API, InteractionContextType, type APIInteraction } from '@discordjs/core';
import { injectable } from 'inversify';
import type { Env } from '../../util/Env.js';
import type { ApplicationCommandHandler, HandlerModule, ICommandHandler } from '../ICommandHandler.js';

@injectable()
export default class DevHandler implements HandlerModule {
public constructor(
private readonly api: API,
private readonly handler: ICommandHandler,
private readonly env: Env,
) {}

public register() {
this.handler.register({
interactions: [
{
name: 'deploy',
description: 'Deploy commands',
options: [],
contexts: [InteractionContextType.BotDM],
},
],
applicationCommands: [['deploy:none:none', this.handleDeploy]],
});
}

private readonly handleDeploy: ApplicationCommandHandler = async (interaction: APIInteraction) => {
if (!interaction.user) {
throw new Error('Command /deploy was ran in non-dm.');
}

if (!this.env.admins.has(interaction.user.id)) {
await this.api.interactions.reply(interaction.id, interaction.token, {
content: 'You are not authorized to use this command',
});

return;
}

await this.handler.deployCommands();

await this.api.interactions.reply(interaction.id, interaction.token, { content: 'Successfully deployed commands' });
};
}
5 changes: 4 additions & 1 deletion packages/core/src/util/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class Env {

public readonly parseableAuth: string = process.env.PARSEABLE_AUTH!;

// TODO: Consider logging validation with zod?

Check warning on line 33 in packages/core/src/util/Env.ts

View workflow job for this annotation

GitHub Actions / Quality Check

Unexpected 'todo' comment: 'TODO: Consider logging validation with...'
/**
* The name of the service, used as a prefix for log streams. Do not use any spaces/special chars.
*
Expand All @@ -38,6 +39,9 @@ export class Env {
*/
public readonly service: string = process.env.SERVICE_NAME!;

// TODO: Consider validation with zod?

Check warning on line 42 in packages/core/src/util/Env.ts

View workflow job for this annotation

GitHub Actions / Quality Check

Unexpected 'todo' comment: 'TODO: Consider validation with zod?'
public readonly admins: Set<string> = new Set(process.env.ADMINS?.split(',') ?? []);

private readonly REQUIRED_KEYS = [
'DISCORD_TOKEN',
'DISCORD_CLIENT_ID',
Expand All @@ -58,7 +62,6 @@ export class Env {
'SERVICE_NAME',
] as const;

// TODO: Consider logging validation with zod?
public constructor() {
const missingKeys = this.REQUIRED_KEYS.filter((key) => !(key in process.env));
if (missingKeys.length) {
Expand Down
15 changes: 4 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3104,17 +3104,10 @@ __metadata:
languageName: node
linkType: hard

"discord-api-types@npm:0.37.83":
version: 0.37.83
resolution: "discord-api-types@npm:0.37.83"
checksum: ab2a31188352d9c742f09a114a95322e7f7de90199cb9f5571f7f5ac25765e7abc9b83c15c14d513ffc5e1d63d9e3ea5ff088fa8a1c5d9c1e1f395b27027cef0
languageName: node
linkType: hard

"discord-api-types@npm:^0.37.84":
version: 0.37.90
resolution: "discord-api-types@npm:0.37.90"
checksum: afc9d3e95b796469a8d3cad18840a1f22e2a7536bd246191c340600b93313d2256b05e409f764d4ad756de7e1e7c3c3a57063920fb3f99fc12a0d2078ccd6e4f
"discord-api-types@npm:0.37.84":
version: 0.37.84
resolution: "discord-api-types@npm:0.37.84"
checksum: e557ef582c20a2d8425b66b915f15caef138cff4257104f08fb97d78595694b7756dee617abc94a750f438292910c4e7a9a2933494a3e0de4a0d40b1d276aec0
languageName: node
linkType: hard

Expand Down

0 comments on commit 4673153

Please sign in to comment.