'Interaction undefined discord.js slash commands
Good evening, I am attempting to migrate my text-based commands to slash commands. I have run in to a problem when trying to get a slash command option, getting the error TypeError: Cannot read properties of undefined (reading 'options'). Now I know the interaction variable is being passed because my ping slash command manages to reply fine.
Here is the code for the command:
execute(interaction) { // inside here command stuff
if (interaction.options.getString('type') === 'add') {
const data = JSON.parse(fs.readFileSync('././config.json'));
if (data.blocked.includes(interaction.options.getMentionable('user').id())) return (interaction.reply('That person is already blocked.'));
data.blocked.push(interaction.options.getMentionable('user').id);
fs.writeFileSync('././config.json', JSON.stringify(data));
interaction.reply(`Successfully blocked ${interaction.options.getMentionable('user').tag}.`);
}
},
And the command configuration:
data: new SlashCommandBuilder()
.setName('block')
.setDescription('Blocks or unblocks a user from using the bot.')
.addStringOption(option =>
option.setName('type')
.setDescription('Whether to block or unblock the user.')
.setRequired(true)
.addChoice('add', 'add')
.addChoice('remove', 'add'))
.addMentionableOption(option =>
option.setName('user')
.setDescription('The user to block or unblock.')),
And the interactionCreate event:
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
}
catch (error) {
console.error(`Error executing ${command.name}:\n${error}`);
await interaction.reply('There was an error trying to execute that command!', true);
}
});
Am I trying to get an option incorrectly? Does the getMentionable() not return a user object? What can I do to fix this error? Maybe someone less sleep-deprived can help. Thank you for any help you can provide!
Solution 1:[1]
Try these out.
data: new SlashCommandBuilder()
.setName('block')
.setDescription('Blocks or unblocks a user from using the bot.')
.addStringOption(option =>
option
.setName('type')
.setDescription('Whether to block or unblock the user.')
.setRequired(true)
.addChoice('add', 'add')
.addChoice('remove', 'remove'))
.addUserOption(option => // addUserOption rather than addMentionableOption
option
.setName('user')
.setDescription('The user to block or unblock.')),
//changed all getMentionable to getUser
async execute (interaction) { // inside here command stuff
if (interaction.options.getString('type') === 'add') {
const data = JSON.parse(fs.readFileSync('././config.json'));
if (data.blocked.includes(interaction.options.getUser('user').id)) {
return interaction.reply('That person is already blocked.')
} else {
data.blocked.push(interaction.options.getUser('user').id);
fs.writeFileSync('././config.json', JSON.stringify(data));
interaction.reply(`Successfully blocked ${interaction.options.getUser('user').tag}.`);
}
}
}
MENTIONABLE sets the option to require a user, role or snowflake as value
USER sets the option to require a user or snowflake as value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gh0st |
