MessageEmbed:无法读取未定义的属性“客户端”

时间:2020-06-03 12:44:22

标签: javascript node.js discord.js

我正在编写一个简单的discord.js机器人。以下代码创建了一个嵌入:

const Discord = require('discord.js')
require('dotenv/config')

const bot = new Discord.Client();

const token = process.env.TOKEN;
const owner = process.env.OWNER;

let snipe = '.s';

bot.on('ready', async() => {
  console.log(`Logged in as ${bot.user.tag}!`);
});

bot.on('message', msg => {
  if (msg.content === `${snipe} help`) {
    const help = new Discord.MessageEmbed()
      .setColor('#7289DA')
      .setTitle('snipe help commands')
      .setAuthor('join the support discord here', 'https://i.ibb.co/4mPgxV9/imageedit-4-8430062590.png', 'https://www.discord.com/')
      .addFields({
        name: snipe,
        value: 'snipes aka shows the last deleted message in that channel'
      }, {
        name: `${snipe} help`,
        value: 'shows this help message'
      }, {
        name: `${snipe} [argument]`,
        value: 'changes the command to "argument" (must be server owner at time of bot addition)'
      }, )
      .addFooter('go to example.org to add this bot to your server')
    msg.channel.send(help)
  }
})

bot.login(token);

运行时,出现以下错误:

C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\structures\MessageEmbed.js:13
    Object.defineProperty(this, 'client', { value: message.client });
                                                           ^

TypeError: Cannot read property 'client' of undefined
    at new MessageEmbed (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\structures\MessageEmbed.js:13:60)
    at Client.<anonymous> (C:\Users\redbrain\Documents\chatbots\snipe\test.js:18:16)
    at Client.emit (events.js:310:20)
    at MessageCreateHandler.handle (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:310:20)
    at Receiver._receiver.onmessage (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\ws\lib\websocket.js:137:47)

引用"C:\Users\redbrain\Documents\chatbots\snipe\test.js:18:16"引用第18行中的“新”一词。有人可以帮助我解决此问题吗?

1 个答案:

答案 0 :(得分:4)

那是因为您在使用MessageEmbed时就像在使用discord.js@v12一样,而您仍在使用discord.js@v11。解决此问题的方法如下:

1。升级到discord.js@v12

如果您决定升级到v12,则还必须更新代码的其他部分,因为其中有一些重大更改:单击here可以找到有关此内容的更多信息。
如果您决定升级,那么这部分代码应该没有问题,只要您编辑代码添加一个数组即可,如另一个答案中所建议的那样:请参阅MessageEmbed.addFields()的文档。
外观如下:

const help = new Discord.MessageEmbed()
  .addFields([
    {...},
    {...},
    {...}
  ])

2。继续使用discord.js@v11

如果您选择继续使用v11,则需要将使用的类更改为RichEmbed,因为MessageEmbed仅用于接收的嵌入,而不用于创建嵌入。在此版本中,您只能使用.addField(),因此您可能最终会执行以下操作:

const help = new Discord.RichEmbed()

[ // Put your field objects into an array
  {
    name: 'name', // string
    value: 'content', // string
    isInline: true // ?boolean (default is false)
  },
  {...}
].forEach(({name, value, isInline}) => {
  help.addField(name, value, isInline)
}) // You can use the Array.forEach method to loop through them