using SlashCommandBuilder in Constructor

Hey im updating my old discord bot to v14 and used the slashcommand builder in a constructor after updaing it does not work anymore, can someone help me with that?
constructor() {
super(
new SlashCommandBuilder()
.setName("ban")
.setDescription("bans the given user")
.addUserOption((option) => option.setName("user").setDescription("user you want to ban").setRequired(true))
.addStringOption((option) => option.setName("reason").setDescription("reason for the ban")),
"mod",
["BAN_MEMBERS"],
false
);
}
constructor() {
super(
new SlashCommandBuilder()
.setName("ban")
.setDescription("bans the given user")
.addUserOption((option) => option.setName("user").setDescription("user you want to ban").setRequired(true))
.addStringOption((option) => option.setName("reason").setDescription("reason for the ban")),
"mod",
["BAN_MEMBERS"],
false
);
}
import { ButtonInteraction, CommandInteraction, Interaction, InteractionResponse, PermissionsString } from "discord.js";
import DiscordClient from "../../client/client";
import { SlashCommandBuilder, SlashCommandSubcommandsOnlyBuilder } from "@discordjs/builders";
export default abstract class BaseCommand {
constructor(private data: Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup"> | SlashCommandSubcommandsOnlyBuilder, private category: string, private permissions: Array<PermissionsString>, private global: boolean) {}

getData(): Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup"> | SlashCommandSubcommandsOnlyBuilder {
return this.data;
}
getName(): string {
return this.data.name;
}
getDescription(): string {
return this.data.description;
}
getCategory(): string {
return this.category;
}
getPermissions(): Array<PermissionsString> {
return this.permissions;
}
isGlobal(): boolean {
return this.global;
}

abstract run(client: DiscordClient, interaction: Interaction | ButtonInteraction | CommandInteraction): Promise<InteractionResponse<boolean> | void>;
}
import { ButtonInteraction, CommandInteraction, Interaction, InteractionResponse, PermissionsString } from "discord.js";
import DiscordClient from "../../client/client";
import { SlashCommandBuilder, SlashCommandSubcommandsOnlyBuilder } from "@discordjs/builders";
export default abstract class BaseCommand {
constructor(private data: Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup"> | SlashCommandSubcommandsOnlyBuilder, private category: string, private permissions: Array<PermissionsString>, private global: boolean) {}

getData(): Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup"> | SlashCommandSubcommandsOnlyBuilder {
return this.data;
}
getName(): string {
return this.data.name;
}
getDescription(): string {
return this.data.description;
}
getCategory(): string {
return this.category;
}
getPermissions(): Array<PermissionsString> {
return this.permissions;
}
isGlobal(): boolean {
return this.global;
}

abstract run(client: DiscordClient, interaction: Interaction | ButtonInteraction | CommandInteraction): Promise<InteractionResponse<boolean> | void>;
}
14 Replies
d.js toolkit
d.js toolkit•4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Amgelo
Amgelo•4mo ago
does not work anymore
elaborate as a side note:
d.js docs
d.js docs•4mo ago
discord.js includes multiple sub-packages, installing these separately can mess with internal code:
npm uninstall discord-api-types @discordjs/rest @discordjs/builders
yarn remove discord-api-types @discordjs/rest @discordjs/builders
pnpm remove discord-api-types @discordjs/rest @discordjs/builders
npm uninstall discord-api-types @discordjs/rest @discordjs/builders
yarn remove discord-api-types @discordjs/rest @discordjs/builders
pnpm remove discord-api-types @discordjs/rest @discordjs/builders
Winnmi
WinnmiOP•4mo ago
you cannt use the slashcommand builder in a constructor? do u know why?
Amgelo
Amgelo•4mo ago
define "can't use"
Winnmi
WinnmiOP•4mo ago
mb
Amgelo
Amgelo•4mo ago
it throws an error when running? when building? it doesn't work as expected, if so, how?
Winnmi
WinnmiOP•4mo ago
it shows an error in the ide [{ "resource": "src/commands/ban.ts", "owner": "typescript", "code": "2345", "severity": 8, "message": "Argument of type 'SlashCommandOptionsOnlyBuilder' is not assignable to parameter of type 'SlashCommandSubcommandsOnlyBuilder | Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">'.\n Type 'SlashCommandOptionsOnlyBuilder' is not assignable to type 'Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">'.\n The types returned by 'setName(...)' are incompatible between these types.\n Type 'SlashCommandOptionsOnlyBuilder' is missing the following properties from type 'SlashCommandBuilder': addSubcommandGroup, addSubcommand", "source": "ts", "startLineNumber": 11, "startColumn": 7, "endLineNumber": 15, "endColumn": 100 }]
Unknown User
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
Winnmi
WinnmiOP•4mo ago
thats possible
Winnmi
WinnmiOP•4mo ago
No description
Winnmi
WinnmiOP•4mo ago
I used a "BaseCommand" to create my commands and used the SlashCommandBuilder in the Constructor. now it thows and error in vscode but i cannto get rid of the error
Amgelo
Amgelo•4mo ago
the error isn't about using it in the constructor, as the message clearly says the problem's about the builder's type not matching your BaseCommand's type you can try using SlashCommandOptionsOnlyBuilder | SlashCommandSubcommandsOnlyBuilder instead, or using a Pick type with the properties that you need like toJSON
Winnmi
WinnmiOP•4mo ago
Thank you it worked 🙂 Sorry for my stupidity

Did you find this page helpful?