Home > Software engineering >  Cannot read properties of null (reading 'id') [Discord Slash Commands]
Cannot read properties of null (reading 'id') [Discord Slash Commands]

Time:02-07

I'm trying to get a little familiar with the slash command handler, but I get an error when trying to grab the member id, this happens with just about everything I try to grab.

Here at the top is directly my command, where I try to send a message with the content "user.id".

this is my command:

const { SlashCommandBuilder } = require("@discordjs/builders");

module.exports = {
    data: new SlashCommandBuilder()
    .setName("avatar")
    .setDescription("SERVUSSS")
    .addUserOption(option => option.setName("member").setDescription("memberdings").setRequired(true)),
    async execute(interaction) {
        const user = interaction.options.getUser('target');
        await interaction.reply(`${user.id}`);
    }
}

this is my deploy file:

require("dotenv").config();
const fs = require("fs");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const commands = [];

const commandFiles = fs.readdirSync("./src/commands").filter(file => file.endsWith(".js"));

commandFiles.forEach(commandFile => {
    const command = require(`./commands/${commandFile}`);
    commands.push(command.data.toJSON());
});

const restClient = new REST({version: "9"}).setToken(process.env.TOKEN);

restClient.put(Routes.applicationGuildCommands(process.env.APPLICATION_ID, process.env.GUILD_ID),
{body: commands }).then(() => console.log("success")).catch(console.error);

my index file:

require("dotenv").config();
const { Client, Collection } = require("discord.js");
const fs = require("fs");

const client = new Client({intents: []});
client.commands = new Collection();

const commandFiles = fs.readdirSync("./src/commands").filter(file => file.endsWith(".js"));

commandFiles.forEach(commandFile => {
    const command = require(`./commands/${commandFile}`);
    client.commands.set(command.data.name, command);
});

client.once("ready", () => {
    console.log("ready!!");
});


client.on("interactionCreate", async (interaction) => {
    if(!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName)

    if(command) {
        try {
            await command.execute(interaction)
        } catch(error) {
            console.log(error)

            if(interaction.deferred || interaction.replied) {
                interaction.editReply("error")
            } else {
                interaction.reply("error")
            }
        }
    }
})


client.login(process.env.TOKEN)

I actually stick to it pretty much all the time https://discordjs.guide/ just do not understand what I'm doing wrong

CodePudding user response:

In your command file, the name for the option is given as member, but when you are using interaction.options.getUser(), you are passing the name as 'target'. So all you have to do is change the line where you are declaring the user to: const user = interaction.options.getUser('member')

CodePudding user response:

Since option name is member then it's going to be

const user = interaction.options.getUser('member');
  •  Tags:  
  • Related