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

时间:2021-02-17 17:10:35

标签: node.js mongodb discord.js typeerror

我一直在尝试制作一个简单的机器人列表,我想从数据库中获取提到的机器人信息。数据库包含机器人 ID。你可以在下面找到我的代码。

获取机器人信息的代码。

const Discord = require('discord.js');
const Botinfo = require('../models/bot.js');

module.exports = {
        name: 'botinfo',
        description: 'Send all the information about the bot in database',

        async execute(client, message, args) {

                const botmention = message.mentions.users.first();
                const mentionid = message.mentions.users.first().id()
                if (!botmention) {
                        const embed = new Discord.MessageEmbed()
                                .setTitle('Error :)')
                                .setColor('RED')
                                .setDescription('You must mention the bot that want information')
                                .setFooter(`By ${message.author.tag}`)
                                .setTimestamp()
                        message.channel.send(embed)
                        return;
                }
                const bot = await Botinfo.findOne({ botid: mentionid })
                if (!bot) {
                        const embed1 = new Discord.MessageEmbed()
                                .setTitle('Error :)')
                                .setColor('RED')
                                .setDescription('We can\'t find that bot in our database, Sorry about that')
                                .setFooter(`By ${message.author.tag}`)
                                .setTimestamp()

                        message.channel.send(embed1)
                        return;
                }
                const embed2 = new Discord.MessageEmbed()
                        .setTitle('Bot Information You asked')
                        .setColor('GREEN')
                        .setDescription('These information have directly got from our database')
                        .addField('Botname:', `<@${id}>`)
                        .addField('Bot ID:', `${id}`)
                        .addField('Bot Prefix:', bot.botprefix)
                        .addField('Bot Image URL:', bot.botimage)
                        .addField('Bot Status:', bot.state)
                        .addField('Certification Status:', bot.certification)
                        .addField('Bot Owner:', `<@${bot.botowner}>`)
                        .setFooter(`Requested By ${message.author.tag}`)
                        .setTimestamp()

                message.channel.send(embed2)
        }
}

当我执行这个命令时。我收到此错误。

(node:15072) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
    at Object.execute (C:\Users\user\Desktop\BotWorld\commands\botinfo.js:11:65)
    at Client.<anonymous> (C:\Users\user\Desktop\BotWorld\index.js:40:46)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (C:\Users\user\Desktop\BotWorld\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\user\Desktop\BotWorld\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)    at WebSocketManager.handlePacket (C:\Users\user\Desktop\BotWorld\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\user\Desktop\BotWorld\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\user\Desktop\BotWorld\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\user\Desktop\BotWorld\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:314:20)
    at Receiver.receiverOnMessage (C:\Users\user\Desktop\BotWorld\node_modules\ws\lib\websocket.js:825:20)
    at Receiver.emit (events.js:314:20)
    at Receiver.dataMessage (C:\Users\user\Desktop\BotWorld\node_modules\ws\lib\receiver.js:437:14)
    at Receiver.getData (C:\Users\user\Desktop\BotWorld\node_modules\ws\lib\receiver.js:367:17)
    at Receiver.startLoop (C:\Users\user\Desktop\BotWorld\node_modules\ws\lib\receiver.js:143:22)
    at Receiver._write (C:\Users\user\Desktop\BotWorld\node_modules\ws\lib\receiver.js:78:10)
(node:15072) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15072) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate 
the Node.js process with a non-zero exit code.

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

第 11 行:.id 而不是 .id(),id 是一个属性,而不是一个方法

const mentionid = message.mentions.users.first().id