【问题标题】:richembed fields may not be empty error -- How would I fix this?richembed 字段可能不是空错误——我将如何解决这个问题?
【发布时间】:2019-04-11 08:47:03
【问题描述】:

我在控制台上不断收到错误消息,提示 RichEmbed 字段可能不为空。当我为每个字段定义值时......这是代码:

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

这是错误:

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

我将不胜感激!提前谢谢!

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    首先,确保提供了args[1]。然后,假设args中的第一个字符串是命令,将suggestion的声明改为...

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

    编辑:还将建议字段的行更改为...

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

    这将防止任何错误导致 suggestion 对于嵌入字段而言过长。

    【讨论】:

      【解决方案2】:

      您不能在 RichEmbed 中添加整个对象作为值。您正在尝试输入 channel Object 作为 RichEmbed 值,这是不允许的。

      我想你想将channel.name 添加到这个 RichEmbed 字段中。我更改了代码,所以它显示了频道的名称。

      这是更正后的代码:

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

      【讨论】:

      • `.addField('Suggestion', suggestion)
      • 那么suggestion是一个空字符串
      • 只是一个建议,但如果你加入discord.js 公会并在那里提出一些问题,也许会更好。我只是基于你提出的最后三个问题。在为您正在使用的库设计的社区中可能更容易获得快速反馈。
      • 我猜你应该这样做:suggestion = args.join(' '); 因为.slice(22) 是删除提及的“廉价而愚蠢”的方式。
      • Lmao 如此真实。现在我改变了使用消息的方式#mentions 谢谢:D
      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2013-11-23
      • 2021-07-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多