简体   繁体   中英

Message Mention and author returning TypeError: Cannot read property 'id' of undefined Discord.js

I have a simple rob command that works on the basis of mentions. I would like to make it so you can use a user's id to rob them as well but it keeps returning a type error of undefined. I have tried changing it around but it keeps returning the same error. Here is the code.

const Discord = require('discord.js');
const db = require('quick.db')
const talkedRecently = new Set();

module.exports = {
    name: 'rob',
    description: 'Steal Moeny',
    async run(client, message, args) {

        if (talkedRecently.has(message.author.id)) {
            return message.channel.send(`Please wait 20 minutes before using this command again`)
        } else {

            let user;

            if (message.mentions.users.first()) {
                user = message.mentions.users.first();
            } else if (args[1]) {
                user = message.guild.members.cache.get(args[1]).user;
            }

            if (user.id == message.author.id) {

                let embed = new Discord.MessageEmbed()
                    .setTitle(`You cannot rob yourself`)
                    .setColor('RED')
                message.channel.send(embed)

            } else {

                talkedRecently.add(message.author.id);
                setTimeout(() => {
                    talkedRecently.delete(message.author.id);
                }, 1200000)

                let robchanceees = ['yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'no', 'no',]
                let suckrate = robchanceees[Math.floor(Math.random() * robchanceees.length)]

                if (suckrate == 'yes' && message.mentions.users.first() && db.get(`eco.bal.${user.id}`) > 0) {
                    let vicbal = db.get(`eco.bal.${user.idid}`)
                    let half = vicbal / 2
                    let stolen = Math.floor(Math.random() * half)
                    message.channel.send(`You successfuly robbed <@${user.id}> of ${stolen}`)
                    db.add(`eco.bal.${message.author,id}`, stolen)
                    db.subtract(`eco.bal.${user.id}`, stolen)
                } else if (suckrate == 'no' && message.mentions.users.first() && db.get(`eco.bal.${user.id}`) > 0) {
                    message.channel.send(`You failed to rob <@${user.id}> and had to pay 100,000 coins to the police :police_officer:`)
                    db.subtract(`eco.bal.${message.author.id}`, 100000)
                }

            }

        }

    },
}; 

If you don't have the members intent enabled in the gateway, the user may or may not be in your cache, so you need to fetch the user using

member = await guild.members.fetch(id)
user = member.user  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM