nomad
nomad

Reputation: 63

Why isn't the "if (member)" called?

Currently creating a check to see if the id inputed were of a user, but it seems like "member" doesn't exist. Code in question:

client.on("message", (msg) => {
    if (msg.author.bot) return;
    if (msg.content.startsWith(PREFIX)) {
        const [CMD_NAME, ...args] = msg.content
            .trim()
            .substring(PREFIX.length)
            .split(/\s+/);
        if (CMD_NAME == "kick") {
            if (args.length === 0) return msg.channel.send("Invalid user.");
            const member = msg.guild.members.cache.get(args[0]);
            if (member) {
                member
                    .kick()
                    .then((member) => msg.channel.send(`${member} was kicked.`))
                    .catch((err) => msg.channel.send(`I lack permissions to kick ${member.user}`));
            } else {
                msg.channel.send("User not found.");
            }
        }    
    }
});
       

Thanks for the help.

Upvotes: 0

Views: 75

Answers (1)

TheBlueOne
TheBlueOne

Reputation: 496

First of all I would recommend to use fetch (https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=fetch) instead of get, because the user maybe isn't cached. Also fetch needs a Snowflake(userid) to get the user, but you probably don't enter a userid, but rather a mention, which is in format '<@userid>' or '!@userid' so it needs to be parsed before you can call the function with the snowflake.

But in your case I would suggest to just use the built in features for mentions.

const member = msg.mentions.users.first();

Upvotes: 1

Related Questions