Yootti
Yootti

Reputation: 1

"Cannot read property 'username' of undefined" when trying to build an embed

I'm creating a discord bot and in this piece of code is showing the error below, instead of sending the embed.

case 'embed':
  const embed = new Discord.RichEmbed()
    .addField('Player Name:', message.author.username);
  message.channel.sendEmbed(embed);
  break;

Cannot read property 'username' of undefined" error in the console

Upvotes: 0

Views: 135

Answers (1)

Hibiscus
Hibiscus

Reputation: 736

There are a few things that could be improved on this. First of all, RichEmbed is deprecated. You can use MessageEmbed to send an embed. Second, for v12, it does require you to state the field variables. Third, the sendEmbed function is deprecated, using .send(embed) is the updated way. With these points in mind, here is the code

const MessageEmbed = require('discord.js') // This allows you to not have to put 'Discord.' before MessageEmbed

case 'embed':
  const embed = new MessageEmbed()
    .addField(name: 'Player Name:', value: `${message.author.tag}`, inline: true); // ${var} allows the embed to see the username and put it as plain text
  message.channel.send(embed);
  break;

Upvotes: 1

Related Questions