表情符号列表命令Discord.js v12

时间:2020-10-06 13:18:06

标签: javascript node.js discord discord.js

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

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.

机器人可以通过任何方式显示表情符号和名称。通过使用不和谐菜单或类似的菜单。我不明白该怎么做。你能帮我吗?在此先感谢

1 个答案:

答案 0 :(得分:0)

您可能会从错误消息中看出,您的嵌入说明过长。您可以使用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