How to access interaction.options.getSubcommand()

Hi, I have a command in which I also have two subcommands. However when I try to access the subcommand via the getSubcommand() the function is not available for me
6 Replies
d.js toolkit
d.js toolkit17mo 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!
darby
darbyOP17mo ago
import {
SlashCommandBuilder,
CommandInteraction,
EmbedBuilder,
} from 'discord.js';
import {Discord} from '../../types/discord';

async function doHandleWhitelistCommandExecution(
interaction: CommandInteraction
): Promise<void> {
const options = interaction.options;
const action = interaction.commandName;
const profile = options.get('profile')?.value;

await interaction.reply(`Profile: ${profile} - Action: ${action}`);
}

const whitelistCommandHandler: Discord.Command = {
data: new SlashCommandBuilder()
.setName('whitelist')
.setDescription(
'Allows you to either add a user to the whitelist or to remove them'
)
.addSubcommand(subcommand =>
subcommand
.setName('add')
.setDescription('Add a player to the whitelist')
.addStringOption(option =>
option
.setName('profile')
.setDescription('Represents a link to a steam profile')
.setRequired(true)
)
)
.addSubcommand(subcommand =>
subcommand
.setName('remove')
.setDescription('Remove a player from the whitelist')
.addStringOption(option =>
option
.setName('profile')
.setDescription('Represents a link to a steam profile')
.setRequired(true)
)
),
onExecution: doHandleWhitelistCommandExecution,
};

export default whitelistCommandHandler;
import {
SlashCommandBuilder,
CommandInteraction,
EmbedBuilder,
} from 'discord.js';
import {Discord} from '../../types/discord';

async function doHandleWhitelistCommandExecution(
interaction: CommandInteraction
): Promise<void> {
const options = interaction.options;
const action = interaction.commandName;
const profile = options.get('profile')?.value;

await interaction.reply(`Profile: ${profile} - Action: ${action}`);
}

const whitelistCommandHandler: Discord.Command = {
data: new SlashCommandBuilder()
.setName('whitelist')
.setDescription(
'Allows you to either add a user to the whitelist or to remove them'
)
.addSubcommand(subcommand =>
subcommand
.setName('add')
.setDescription('Add a player to the whitelist')
.addStringOption(option =>
option
.setName('profile')
.setDescription('Represents a link to a steam profile')
.setRequired(true)
)
)
.addSubcommand(subcommand =>
subcommand
.setName('remove')
.setDescription('Remove a player from the whitelist')
.addStringOption(option =>
option
.setName('profile')
.setDescription('Represents a link to a steam profile')
.setRequired(true)
)
),
onExecution: doHandleWhitelistCommandExecution,
};

export default whitelistCommandHandler;
darby
darbyOP17mo ago
According to discord.js guide on resolving a subcommand I should have the function getSubcommand inside the options property
darby
darbyOP17mo ago
Idk if this has anything to do with the type of the command
export interface Command<T extends any[] = any[], R = void> {
data: SlashCommandSubcommandsOnlyBuilder & {
toJSON: () => any;
};
onExecution: (...args: T) => Promise<R> | R;
}
export interface Command<T extends any[] = any[], R = void> {
data: SlashCommandSubcommandsOnlyBuilder & {
toJSON: () => any;
};
onExecution: (...args: T) => Promise<R> | R;
}
treble/luna
treble/luna17mo ago
Type it as ChatInputCommandInteraction
darby
darbyOP17mo ago
thanks a lot !
Want results from more Discord servers?
Add your server