【问题标题】:I cannot DM users with discord.js bot我无法使用 discord.js 机器人 DM 用户
【发布时间】:2021-05-04 00:04:33
【问题描述】:

我一直在尝试在 discord.js 中构建一个邮件机器人
这是到目前为止的代码
`

client.on('message', message=> {
  var input = message.toString().replace("+send ","").split(",");
  const mail = new Discord.MessageEmbed();
    mail.setColor('#ff0044')
    mail.setTitle(input[1])
    mail.setAuthor(message.author.username)
    mail.setDescription(input[2])
    mail.setThumbnail(message.author.avatarURL)
    mail.setFooter('Send messages with "+send id,subject,content"');
  client.users.cache.get(input[0]).send(mail);
});

`

发送命令时,它发送邮件但返回此错误

client.users.cache.get(input[0]).send(mail); TypeError: Cannot read property 'send' of undefined at Client.<anonymous> (/home/runner/VladMail/index.js:18:35) at Client.emit (events.js:315:20) at Client.EventEmitter.emit (domain.js:483:12) at MessageCreateAction.handle (/home/runner/VladMail/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14) at Object.module.exports [as MESSAGE_CREATE] (/home/runner/VladMail/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (/home/runner/VladMail/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31) at WebSocketShard.onPacket (/home/runner/VladMail/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22) at WebSocketShard.onMessage (/home/runner/VladMail/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10) at WebSocket.onMessage (/home/runner/VladMail/node_modules/ws/lib/event-target.js:132:16)


为什么会返回此错误,我该如何解决?

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    看起来您的 client.users.cache.get(input[0]) 返回未定义。

    当您尝试获取它时,请检查它返回的内容以确保它不是未定义的。

    类似这样的:

    let user = client.users.cache.get(input[0]);
    console.log(user);
    if(user){
      user.send(mail);
    }
    

    【讨论】:

    • 我明白了。我昨天刚加入 stackoverflow,所以我是新手
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2021-05-11
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多