Skip to content

Instantly share code, notes, and snippets.

@jozsefsallai
Created March 22, 2024 18:13
Show Gist options
  • Save jozsefsallai/234b9ce0e9586876fc9a9437aba9168c to your computer and use it in GitHub Desktop.
Save jozsefsallai/234b9ce0e9586876fc9a9437aba9168c to your computer and use it in GitHub Desktop.
Discord.js slash command builder with user app command support

Just import your own SlashCommandBuilder instead of the one from discord.js. Then you can do this:

const meta = new SlashCommandBuilder()
  .setName('sandwich')
  .setDescription('Makes a sandwich')
  .setIntegrationTypes(AppIntegrationType.GuildInstall, AppIntegrationType.UserInstall);

As per Discord spec, commands will only have the GuildInstall integration type, if none specified.

Optionally, you can also specify interaction contexts:

const meta = new SlashCommandBuilder()
  .setName('sandwich')
  .setDescription('Makes a sandwich')
  .setIntegrationTypes(AppIntegrationType.GuildInstall, AppIntegrationType.UserInstall)
  .setContexts(InteractionContextType.Guild, InteractionContextType.BotDM);

As per Discord spec, commands will have all 3 interaction contexts. In this example, the command can be used in servers and in bot DMs, but not in any other DMs.

// you should use typescript btw, see the typescript version below
const { SlashCommandBuilder: DJSSlashCommandBuilder } = require('discord.js');
const AppIntegrationType = Object.freeze({
// App is installable to servers
GuildInstall: 0,
// App is installable to users
UserInstall: 1,
});
const InteractionContextType = Object.freeze({
// Interaction can be used within servers
Guild: 0,
// Interaction can be used within DMs with the app's bot user
BotDM: 1,
// Interaction can be used within Group DMs and DMs other than the app's bot user
PrivateChannel: 2,
});
class SlashCommandBuilder extends DJSSlashCommandBuilder {
_integrationTypes = [AppIntegrationType.GuildInstall];
_contexts = [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel,
];
get integrationTypes() {
return this._integrationTypes;
}
get contexts() {
return this._contexts;
}
setIntegrationTypes(...types) {
this._integrationTypes = types;
return this;
}
setContexts(...contexts) {
this._contexts = contexts;
return this;
}
toJSON() {
return {
...super.toJSON(),
integration_types: this.integrationTypes,
contexts: this.contexts,
};
}
}
module.exports = {
AppIntegrationType,
InteractionContextType,
SlashCommandBuilder,
};
import {
SlashCommandBuilder as DJSSlashCommandBuilder,
RESTPostAPIChatInputApplicationCommandsJSONBody,
} from 'discord.js';
export enum AppIntegrationType {
// App is installable to servers
GuildInstall = 0,
// App is installable to users
UserInstall = 1,
}
export enum InteractionContextType {
// Interaction can be used within servers
Guild = 0,
// Interaction can be used within DMs with the app's bot user
BotDM = 1,
// Interaction can be used within Group DMs and DMs other than the app's bot user
PrivateChannel = 2,
}
export class SlashCommandBuilder extends DJSSlashCommandBuilder {
private _integrationTypes: AppIntegrationType[] = [
AppIntegrationType.GuildInstall,
];
private _contexts: InteractionContextType[] = [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel,
];
get integrationTypes() {
return this._integrationTypes;
}
get contexts() {
return this._contexts;
}
setIntegrationTypes(...types: AppIntegrationType[]) {
this._integrationTypes = types;
return this;
}
setContexts(...contexts: InteractionContextType[]) {
this._contexts = contexts;
return this;
}
toJSON(): RESTPostAPIChatInputApplicationCommandsJSONBody & {
integration_types: AppIntegrationType[];
contexts: InteractionContextType[];
} {
return {
...super.toJSON(),
integration_types: this.integrationTypes,
contexts: this.contexts,
};
}
}
@Mega-Bits
Copy link

Nice work. But i cant figure out, how do you register those? Is there a diffrent method for this then await client.application.commands.set(commands);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment