Reputation: 1
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
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