【问题标题】:Emoji List Command Discord.js v12表情符号列表命令 Discord.js v12
【发布时间】:2021-01-21 09:10:51
【问题描述】:

我创建了一个表情符号列表命令,这里是我的命令代码:

const { MessageEmbed } = require('discord.js');

module.exports = {
    name: "emojis",
    description: "Gets a guild\'s emojis",

    async run (client, message, args) {
 const emojis = [];
    message.guild.emojis.cache.forEach(e => emojis.push(`${e} **-** \`:${e.name}:\``));
 const embed = new MessageEmbed()  
    .setTitle(`Emoji List`)
    .setDescription(emojis.join('\n'))
    message.channel.send(embed)
  }
};

但是,如果嵌入中的字符超过 2048 个字母,我会收到此错误:

(node:211) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.description: Must be 2048 or fewer in length.
    at RequestHandler.execute (/home/runner/Utki-the-bot/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:211) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:211) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

机器人有什么办法仍然可以显示表情符号和名称。通过使用不和谐菜单或类似的方式。我无法理解如何做到这一点。你能帮我吗?提前致谢

【问题讨论】:

  • 描述中不接受超过 2048 个字符,这还不够明显吗?
  • 有没有其他方法可以使用任何方法显示所有内容?我问的是@TasosBu
  • 拆分成多条消息

标签: javascript node.js discord discord.js


【解决方案1】:

您可能从错误消息中可以看出,您的嵌入描述太长了。您可以使用string.split() 将您的消息分成几个部分,并将每个缩短的字符串作为单独的消息发送。这是一个基本示例。

const charactersPerMessage = 2000;
  // we're going to go with 2000 instead of 2048 for breathing room
const emojis = message.guild.emojis.cache.map(e=> { return `${e} **-** \`:${e.name}:\`` }); // does virtually the same thing as forEach()
const numberOfMessages = Math.ceil(emojis.length/charactersPerMessage); // calculate how many messages we need

const embed = new MessageEmbed()
                  .setTitle(`Emoji List`);

for(i=0;i<numberOfMessages;i++) {
  message.channel.send(
    embed.setDescription(emojis.slice(i*charactersPerMessage, (i+1)*charactersPerMessage))
  );
}

请注意 emojis 现在是 string 而不是 Array

【讨论】:

  • 你能举例说明如何做到这一点吗?我对编码有点陌生,所以我不知道该怎么做。
  • 我添加了一个示例。如果它给您带来任何问题或您需要澄清,请告诉我。
  • 运行代码时出现此错误:const emojis = message.guild.emojis.cache.map(e=&gt; return `${e} **-** \`:${e.name}:\``); SyntaxError: Unexpected token 'return'
  • 抱歉,return 语句应该用大括号括起来。 .map(e=&gt; { return ... })
  • 我仍然不断收到同样的错误@김현진。正如我在问题中提到的那样
猜你喜欢
  • 2021-01-28
  • 2021-01-11
  • 2021-01-01
  • 2021-02-18
  • 1970-01-01
  • 2021-01-18
  • 2021-04-14
  • 2021-04-19
  • 2021-04-18
相关资源
最近更新 更多