javascript - 消息嵌入 : Cannot read property 'client' of undefined

标签 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 行中引用了单词“new”。谁能帮我解决这个问题问题?

最佳答案

那是因为您在使用 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

关于javascript - 消息嵌入 : Cannot read property 'client' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62173144/

相关文章:

javascript - 强制 Service Worker 仅在特定页面打开时返回缓存的响应

javascript - 按数字对多维数组 alpha 进行排序

javascript - 将字符串更新为 mongodb 中的 Date 对象

javascript - 使用 Google API 的 YouTube 搜索 Node.js

javascript - 如何在本地项目中使用jsdoc

javascript - 为什么在javascript中出现未处理的 'error'事件

javascript - 替换 JavaScript 中的方法

javascript - React hook 在 Event Listener 中保存的状态错误?

即使在 try 语句中,MySQL 查询也会使我的程序崩溃

javascript - Discord.js 为 ReactionRole 添加一次 react 收集器