Skip to content

Commit

Permalink
fix: forcefully set the context (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeadTriXz authored Jul 19, 2024
1 parent 3252ca2 commit 7421f64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/core/src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import {
import type { ApplicationCommandInteraction } from "../index.js";
import type { Module } from "../modules/index.js";

/**
* The default interaction contexts for a command.
*/
const DEFAULT_INTERACTION_CONTEXTS = [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel
];

/**
* Options for a {@link BaseCommand}.
*/
Expand Down Expand Up @@ -86,9 +95,9 @@ export abstract class BaseCommand<M extends Module = Module> {
client: M["client"];

/**
* Array of interaction context(s) where the command can be used.
* Array of interaction context(s) where the command can be used. Defaults to all contexts.
*/
contexts?: InteractionContextType[];
contexts: InteractionContextType[];

/**
* The period during which the user cannot execute the same command (in seconds).
Expand Down Expand Up @@ -150,23 +159,17 @@ export abstract class BaseCommand<M extends Module = Module> {
constructor(public module: M, options: ApplicationCommandOptions) {
this.appPermissions = options.appPermissions;
this.client = module.client;
this.contexts = options.contexts;
this.contexts = options.contexts ?? DEFAULT_INTERACTION_CONTEXTS;
this.cooldown = options.cooldown ?? 3;
this.defaultMemberPermissions = options.defaultMemberPermissions;
this.guildOnly = options.guildOnly ?? false;

if (options.guildOnly !== undefined && options.contexts === undefined) {
console.warn("The 'guildOnly' option is deprecated. Please use the 'contexts' option instead.");

if (options.guildOnly) {
this.contexts = [InteractionContextType.Guild];
} else {
this.contexts = [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel
];
}
this.contexts = options.guildOnly
? [InteractionContextType.Guild]
: DEFAULT_INTERACTION_CONTEXTS;
}

if (options.contexts !== undefined) {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/tests/commands/BaseCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe("BaseCommand", () => {
name: "test"
});

expect(command.contexts).toEqual([
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel
]);
expect(command.cooldown).toBe(3);
expect(command.ownerOnly).toBe(false);
});
Expand Down Expand Up @@ -121,6 +126,11 @@ describe("BaseCommand", () => {
const payload = command.toJSON();

expect(payload).toEqual({
contexts: [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel
],
name: "test",
type: ApplicationCommandType.ChatInput
});
Expand Down
5 changes: 5 additions & 0 deletions packages/core/tests/commands/SlashCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ describe("SlashCommand", () => {
const payload = command.toJSON();

expect(payload).toEqual({
contexts: [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel
],
description: "Mock command for testing purposes",
name: "test",
options: [],
Expand Down

0 comments on commit 7421f64

Please sign in to comment.