首页 > 解决方案 > TypeError:无法读取 Discord.js 中未定义的属性“id”

问题描述

module.exports = {
    name: "dm",
    description: "DM a user in the guild",
    category: "fun",
    run: async (bot, message, args) => {
      if (!member.message.author.id === ("356440200253276163"))
        return message.channel.send("**:x: You cannot use this command as it is only for the owner of bot. :x:**");
      let user =
        message.mentions.members.first() ||
        message.guild.members.cache.get(args[0]);
      if (!user)
        return message.channel.send(
         `**:x: You did not mention a user. :x:**`
        );
      if (!args.slice(1).join(" "))
        return message.channel.send("**:x: You did not specify your message. :x:**");
      user.user
        .send(args.slice(1).join(" "))
        .catch(() => message.channel.send("**:regional_indicator_x: That user could not be DMed! :regional_indicator_x:**"))
        .then(() => message.channel.send(`**:white_check_mark: Sent a message to ${user.user.tag} :white_check_mark:**`));
    },
  };
(node:4) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined

2020-08-08T06:14:42.414100+00:00 app[worker.1]:     at Object.run (/app/commands/dm.js:33:34)

2020-08-08T06:14:42.414101+00:00 app[worker.1]:     at Client.<anonymous> (/app/bot.js:39:21)

2020-08-08T06:14:42.414101+00:00 app[worker.1]:     at Client.emit (events.js:327:22)

2020-08-08T06:14:42.414102+00:00 app[worker.1]:     at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)

2020-08-08T06:14:42.414103+00:00 app[worker.1]:     at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)

2020-08-08T06:14:42.414104+00:00 app[worker.1]:     at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)

2020-08-08T06:14:42.414104+00:00 app[worker.1]:     at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)

2020-08-08T06:14:42.414104+00:00 app[worker.1]:     at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)

2020-08-08T06:14:42.414105+00:00 app[worker.1]:     at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:125:16)

2020-08-08T06:14:42.414106+00:00 app[worker.1]:     at WebSocket.emit (events.js:315:20)

2020-08-08T06:14:42.414304+00:00 app[worker.1]: (node:4) 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)

2020-08-08T06:14:42.414353+00:00 app[worker.1]: (node:4) [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.

所以我试图让 DM 命令只能由我,机器人的所有者使用,因为我不希望人们滥用它并让我的机器人因为他们所做的事情而被禁止,但它给出了这个错误。我检查了 discord.js.org 但我找不到它发生的原因或如何解决它。我检查了权限部分。请帮忙。谢谢你。

标签: javascriptnode.jsdiscorddiscord.js

解决方案


而不是if (!message.member.author.id === "356440200253276163")

使用if (!message.author.id === "356440200253276163").

message.member.author.id不是一个对象,这就是它返回 undefined 的原因。


推荐阅读