Static
Static

Reputation: 786

richembed fields may not be empty error -- How would I fix this?

I keep getting an error on my console saying that RichEmbed fields may not be empty. When I have defined values for each field... Here's the code:

if (cmd === `${prefix}suggest`) {
  // USAGE:
  // /suggest this is the suggestion

  const suggestion = args.join(' ').slice(22);

  const suggestEmbed = new Discord.RichEmbed()
      .setDescription('~~-------~~**__NEW SUGGESTION!__**~~-------~~')
      .setColor('#ff0000')
      .addField('Suggestion By', `${message.author} (${message.author.id})`)
      .addField('Channel', message.channel)
      .addField('Time', message.createdAt)
      .addField('Suggestion', suggestion)
      .setTimestamp()
      .setFooter('Use /invite to invite me to your server!');

  const suggestchannel = message.guild.channels.find(`name`, 'suggestions');
  if (!suggestchannel) return message.channel.send("Couldn't find suggestions channel. Please **create one for this command to work!**");


  message.delete().catch(O_o => {});
  suggestchannel.send({ embed: suggestEmbed });
}

And here's the error:

(node:616) UnhandledPromiseRejectionWarning: RangeError: RichEmbed field values may not be empty.
    at RichEmbed.addField

I would appreciate some help! Thank you in advance!

Upvotes: 0

Views: 1063

Answers (2)

slothiful
slothiful

Reputation: 5623

First, make sure that args[1] is provided. Then, assuming the first string in args is the command, change the declaration of suggestion to...

const suggestion = args.slice(1).join(' ');

Edit: Also change the line for the suggestion field to...

.addField('Suggestion', suggestion.length <= 1024 ? suggestion : suggestion.slice(0, 1020) + '...')

That will prevent any errors resulting in suggestion being too long for an embed field.

Upvotes: 1

Gilles Heinesch
Gilles Heinesch

Reputation: 2980

You can't add a whole Object in a RichEmbed as a value. You're trying to enter the channel Object as RichEmbed value which is not allowed.

I suppose you want to add the channel.name to this RichEmbed field. I changed the code so it shows the name of the channel.

Here is the corrected code:

if (cmd === `${prefix}suggest`) {
    // USAGE:
    // /suggest this is the suggestion

    const suggestion = args.join(' ').slice(22);

    const suggestEmbed = new Discord.RichEmbed()
        .setDescription('~~-------~~**__NEW SUGGESTION!__**~~-------~~')
        .setColor('#ff0000')
        .addField('Suggestion By', `${message.author} (${message.author.id})`)
        .addField('Channel', message.channel.name)
        .addField('Time', message.createdAt)
        .addField('Suggestion', suggestion)
        .setTimestamp()
        .setFooter('Use /invite to invite me to your server!');

    const suggestchannel = message.guild.channels.find(`name`, 'suggestions');
    if (!suggestchannel) return message.channel.send("Couldn't find suggestions channel. Please **create one for this command to work!**");


    message.delete().catch(O_o => {});
    suggestchannel.send({ embed: suggestEmbed });
  }

Upvotes: 0

Related Questions