pavy
pavy

Reputation: 11

TypeError: Cannot read properties of undefined (reading 'member')

Can someone help me figure out this error? I really dont know what to do. I tried my best to get rid of this error, but nothing worked. I will be very grateful if you try to help me. This code is supposed to give you basic information about user that you pinged.enter image description here

const Command = require("../Structures/Command.js");

/**
 * @param {Client} client
 * @param {Message} message
 * @param {String[]} args 
 */

module.exports = new Command({
    name: "userinfo",
    description: 'info',

    async run(client, message, args) {
        const user = message.mentions.member.first() || message.member.user.username;
        const member = message.guild.members.cache.get(user.id);

        const embed = new MessageEmbed()
            .setColor('DARK_RED')
            .setAuthor({ name: user.username, iconURL: user.avatarURL({ dynamic: true }) })
            .setDescription("Kdo to je")
            .setThumbnail(user.displayAvatarURL({ dynamic: true }))
            .addFields(
            { name: "Username", value: user.username, inline: true }, 
            { name: "Tag", value: user.discriminator, inline: true }, 
            { name: "Bot", value: user.bot, inline: true }, 
            { name: "Nickname", value: member.nickname || "None", inline: true },
            { name: "Joined:", value: new Date(member.joinedTimestamp()).toDateString(), inline: true },
            { name: "Discord user since:", value: new Date(user.createdTimestamp()).toDateString(), inline: true },
            { name: "Roles count:", value: member.roles.cache - 1, inline: true },

            )

        return message.channel.send({ embeds: [embed] });
    }

})

Im also sharing my simple command handler, because its used in this code.

const Client = require("./Client.js");

/**
 * @param {Discord.Message} message 
 * @param {string[]} args 
 * @param {Client} client 
 */
function RunFunction(message, args, client) {}


class Command {

    /**
     * @typedef {{name: string, description: string, run: RunFunction}} CommandOptions
     * @param {CommandOptions} options 
     */
    constructor(options) {
        this.name = options.name;
        this.description = options.description;
        this.permission = options.permission;
        this.run = options.run;
    }
}

module.exports = Command;

Upvotes: 1

Views: 1832

Answers (1)

libik
libik

Reputation: 23029

One of the following does not contain object:

message.mentions.member or message.member or message.guild.members

Therefore when you try to access i.e. message.mentions.member.first() it fails, because you try to call something like undefined.first()

You should log (or debug) the whole message parameter and see what is actually inside.

Upvotes: 1

Related Questions