Skip to content

Commit 761340e

Browse files
committed
fix: validate metadata.guilds for message commands
1 parent 45b9788 commit 761340e

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

packages/commandkit/src/app/handlers/AppCommandHandler.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SlashCommandBuilder,
1111
} from 'discord.js';
1212
import type { CommandKit } from '../../commandkit';
13+
import { getConfig } from '../../config/config';
1314
import { AsyncFunction, GenericFunction } from '../../context/async-context';
1415
import { Logger } from '../../logger/Logger';
1516
import type {
@@ -24,12 +25,14 @@ import { toFileURL } from '../../utils/resolve-file-url';
2425
import { rewriteCommandDeclaration } from '../../utils/types-package';
2526
import { AppCommandRunner } from '../commands/AppCommandRunner';
2627
import { Context } from '../commands/Context';
28+
import { isInteractionSource } from '../commands/helpers';
2729
import { MessageCommandParser } from '../commands/MessageCommandParser';
30+
import {
31+
beforeExecute as permissions_beforeExecute,
32+
middlewareId as permissions_middlewareId,
33+
} from '../middlewares/permissions';
2834
import { CommandRegistrar } from '../register/CommandRegistrar';
2935
import { Command, Middleware } from '../router';
30-
import { getConfig } from '../../config/config';
31-
import { beforeExecute, middlewareId } from '../middlewares/permissions';
32-
import { isInteractionSource } from '../commands/helpers';
3336

3437
const KNOWN_NON_HANDLER_KEYS = [
3538
'command',
@@ -547,6 +550,19 @@ export class AppCommandHandler {
547550
return null;
548551
}
549552

553+
if (source instanceof Message) {
554+
if (!source.guildId) {
555+
return null; // command is being called in a dm
556+
}
557+
558+
if (
559+
loadedCommand.metadata?.guilds &&
560+
!loadedCommand.metadata.guilds.includes(source.guildId)
561+
) {
562+
return null; // command is being called in a guild that is not in the metadata
563+
}
564+
}
565+
550566
// Collect all applicable middleware
551567
const middlewares: LoadedMiddleware[] = [];
552568

@@ -562,12 +578,12 @@ export class AppCommandHandler {
562578
middlewares.push({
563579
data: {
564580
// @ts-ignore
565-
beforeExecute,
581+
beforeExecute: permissions_beforeExecute,
566582
},
567583
middleware: {
568584
command: null,
569585
global: true,
570-
id: middlewareId,
586+
id: permissions_middlewareId,
571587
name: 'permissions',
572588
parentPath: '',
573589
path: '',

0 commit comments

Comments
 (0)