TypeError:尝试制作不和谐机器人时无法读取未定义的属性“id”

Posted

技术标签:

【中文标题】TypeError:尝试制作不和谐机器人时无法读取未定义的属性“id”【英文标题】:TypeError: Cannot read property 'id' of undefined when trying to make discord bot 【发布时间】:2019-01-27 07:01:09 【问题描述】:

我正在尝试制作一个简单的不和谐机器人,但是每当我运行-setcaps 命令时,我都会得到:

TypeError:无法读取未定义的属性“id”。

我不确定是什么原因造成的。我将不胜感激您能提供的任何帮助。不完全确定要添加什么以提供更多详细信息,我正在使用 node.js 的最新稳定版本并使用 notpad++ 进行编辑

// Call Packages
const Discord = require('discord.js');
const economy = require('discord-eco');

// Define client for Discord
const client = new Discord.Client();
// We have to define a moderator role, the name of a role you need to run certain commands
const modRole = 'Sentinel';

// This will run when a message is recieved...
client.on('message', message => 

    // Variables
    let prefix = '-';
    let msg = message.content.toUpperCase();
    // Lets also add some new variables
    let cont = message.content.slice(prefix.length).split(" "); // This slices off the prefix, then stores everything after that in an array split by spaces.
    let args = cont.slice(1); // This removes the command part of the message, only leaving the words after it seperated by spaces

    // Commands

    // Ping - Let's create a quick command to make sure everything is working!
    if (message.content.toUpperCase() === `$prefixPING`) 
        message.channel.send('Pong!');
    

    // Add / Remove Money For Admins
    if (msg.startsWith(`$prefixSETCAPS`)) 

        // Check if they have the modRole
        if (!message.member.roles.find("name", modRole))  // Run if they dont have role...
            message.channel.send('**You need the role `' + modRole + '` to use this command...**');
            return;
        

        // Check if they defined an amount
        if (!args[0]) 
            message.channel.send(`**You need to define an amount. Usage: $prefixSETCAPS <amount> <user>**`);
            return;
        

        // We should also make sure that args[0] is a number
        if (isNaN(args[0])) 
            message.channel.send(`**The amount has to be a number. Usage: $prefixSETCAPS <amount> <user>**`);
            return; // Remember to return if you are sending an error message! So the rest of the code doesn't run.
        

        // Check if they defined a user
        let defineduser = '';
        if (!args[1])  // If they didn't define anyone, set it to their own.
            defineduser = message.author.id;
         else  // Run this if they did define someone...
            let firstMentioned = message.mentions.users.first();
            defineduser = firstMentioned.id;
        

        // Finally, run this.. REMEMBER IF you are doing the guild-unique method, make sure you add the guild ID to the end,
        economy.updateBalance(defineduser + message.guild.id, parseInt(args[0])).then((i) =>  // AND MAKE SURE YOU ALWAYS PARSE THE NUMBER YOU ARE ADDING AS AN INTEGER
            message.channel.send(`**User defined had $args[0] added/subtraction from their account.**`)
        );

    

    // Balance & Money
    if (msg === `$prefixBALANCE` || msg === `$prefixMONEY`)  // This will run if the message is either ~BALANCE or ~MONEY

        // Additional Tip: If you want to make the values guild-unique, simply add + message.guild.id whenever you request.
        economy.fetchBalance(message.author.id + message.guild.id).then((i) =>  // economy.fetchBalance grabs the userID, finds it, and puts the data with it into i.
            // Lets use an embed for This
            const embed = new Discord.RichEmbed()
                .setDescription(`**$message.guild.name Stash**`)
                .setColor(0xff9900) // You can set any HEX color if you put 0x before it.
                .addField('Stash Owner',message.author.username,true) // The TRUE makes the embed inline. Account Holder is the title, and message.author is the value
                .addField('Stash Contents',i.money,true)


            // Now we need to send the message
            message.channel.send(embed)

        )

    

);

client.login('TOKEN HIDDEN');

【问题讨论】:

能否将整个错误发送给我们,请在我们查看哪个代码行导致此错误的地方 【参考方案1】:

我不确定这是否会导致您的错误,但让我们试一试。我编辑了用户是否提到某人的检查。

// Call Packages
const Discord = require('discord.js');
const economy = require('discord-eco');

// Define client for Discord
const client = new Discord.Client();
// We have to define a moderator role, the name of a role you need to run certain commands
const modRole = 'Sentinel';

// This will run when a message is recieved...
client.on('message', message => 

    // Variables
    let prefix = '-';
    let msg = message.content.toUpperCase();
    // Lets also add some new variables
    let cont = message.content.slice(prefix.length).split(" "); // This slices off the prefix, then stores everything after that in an array split by spaces.
    let args = cont.slice(1); // This removes the command part of the message, only leaving the words after it seperated by spaces

    // Commands

    // Ping - Let's create a quick command to make sure everything is working!
    if (message.content.toUpperCase() === `$prefixPING`) 
        message.channel.send('Pong!');
    

    // Add / Remove Money For Admins
    if (msg.startsWith(`$prefixSETCAPS`)) 

        // Check if they have the modRole
        if (!message.member.roles.find("name", modRole))  // Run if they dont have role...
            message.channel.send('**You need the role `' + modRole.name + '` to use this command...**');
            return;
        

        // Check if they defined an amount
        if (!args[0]) 
            message.channel.send(`**You need to define an amount. Usage: $prefixSETCAPS <amount> <user>**`);
            return;
        

        // We should also make sure that args[0] is a number
        if (isNaN(args[0])) 
            message.channel.send(`**The amount has to be a number. Usage: $prefixSETCAPS <amount> <user>**`);
            return; // Remember to return if you are sending an error message! So the rest of the code doesn't run.
        

        // Check if they defined a user
        let defineduser = '';
        let user = message.mentions.users.first() || msg.author;
        defineduser = user.id

        // Finally, run this.. REMEMBER IF you are doing the guild-unique method, make sure you add the guild ID to the end,
        economy.updateBalance(defineduser + message.guild.id, parseInt(args[0])).then((i) =>  // AND MAKE SURE YOU ALWAYS PARSE THE NUMBER YOU ARE ADDING AS AN INTEGER
            message.channel.send(`**User defined had $args[0] added/subtraction from their account.**`)
        );

    

    // Balance & Money
    if (msg === `$prefixBALANCE` || msg === `$prefixMONEY`)  // This will run if the message is either ~BALANCE or ~MONEY

        // Additional Tip: If you want to make the values guild-unique, simply add + message.guild.id whenever you request.
        economy.fetchBalance(message.author.id + message.guild.id).then((i) =>  // economy.fetchBalance grabs the userID, finds it, and puts the data with it into i.
            // Lets use an embed for This
            const embed = new Discord.RichEmbed()
                .setDescription(`**$message.guild.name Stash**`)
                .setColor(0xff9900) // You can set any HEX color if you put 0x before it.
                .addField('Stash Owner', message.author.username, true) // The TRUE makes the embed inline. Account Holder is the title, and message.author is the value
                .addField('Stash Contents', i.money, true)


            // Now we need to send the message
            message.channel.send(
                embed
            )

        )

    

);

client.login('TOKEN HIDDEN')

【讨论】:

遗憾的是还没有修复,感谢您的尝试:(

以上是关于TypeError:尝试制作不和谐机器人时无法读取未定义的属性“id”的主要内容,如果未能解决你的问题,请参考以下文章

不和谐.js | TypeError:无法读取未定义的属性“0”

如何修复“TypeError:无法读取未定义的属性 'toString'” |不和谐.js

TypeError:每当我在不和谐的情况下向我的机器人输入 PM 时,都无法读取未定义的属性“id”

Discord.js 在尝试将游戏消息转换为不和谐消息时给我留下“TypeError:无法读取未定义的属性 'includes'”

Discord.js TypeError:无法读取未定义发送到特定频道的属性“发送”

TypeError:无法读取未定义的属性“发送”