redbrain
redbrain

Reputation: 45

MessageEmbed: Cannot read property 'client' of undefined

I am writing a simple discord.js bot. The following code creates an embed:

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);

When it runs, I get the following error:

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)

The reference "C:\Users\redbrain\Documents\chatbots\snipe\test.js:18:16" references the word 'new' in line 18. Can anyone help me fix this issue?

Upvotes: 3

Views: 5681

Answers (1)

Federico Grandi
Federico Grandi

Reputation: 6816

That's because you're using MessageEmbed as if you were using discord.js@v12, while you're still on discord.js@v11. Here's how you can solve this:

1. Upgrading to discord.js@v12

If you decide to upgrade to v12 you'll have to also update other parts of your code, since there are some breaking changes: click here to find more about that.
If you decide to upgrade then this part of your code should work with no issues, provided that you edit your code adding an Array, as suggested in another answer: see the docs for MessageEmbed.addFields() for that.
Here's how it would look:

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

2. Keep using discord.js@v11

If you choose to keep using v11 then you'll need to change the class that you're using to RichEmbed, since MessageEmbed is only used for received embeds, not for creating one. In this version you can only use .addField(), so you might end up doing something like this:

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

Upvotes: 4

Related Questions