简体   繁体   English

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

[英]Discord.js v12 Emoji List Command

I'm trying to make an emoji list command in discord.js v12.我正在尝试在 discord.js v12 中创建一个表情符号列表命令。 However, if I run the command in a server with many emojis I get an Invalid Form Body error as the embed description cannot have more than 2048 characters.但是,如果我在带有许多表情符号的服务器中运行该命令,我会收到一个 Invalid Form Body 错误,因为嵌入的描述不能超过 2048 个字符。 Hence I tried to split the message.因此,我试图拆分消息。 Here is my code:这是我的代码:

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

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

 async run(client, message, args) {
  const charactersPerMessage = 2000;
  const emojis = message.guild.emojis.cache.map((e) => {
   return `${e} **-** \`:${e.name}:\``;
  });
  const numberOfMessages = Math.ceil(emojis.length / charactersPerMessage);
  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)
    )
   );
  }
 },
};

Even after this, I get the same Invalid Body Form error.即使在此之后,我也会收到相同的 Invalid Body Form 错误。 Here is the error I get:这是我得到的错误:

(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.

Can you help me out?你能帮我吗? Thanks in advance!提前致谢!

The problem is the fact that emojis is an array, not a string.问题是emojis是一个数组,而不是一个字符串。 The .map function returns an array (of strings in your case). .map函数返回一个数组(在您的情况下是字符串)。 The fix should be quite simple, just add a .join at the end of the .map function to make it a string.修复应该很简单,只需在.map函数的末尾添加一个.join使其成为字符串。 Check the code below:检查下面的代码:

const emojis = message.guild.emojis.cache
 .map((e) => `${e} **-** \`:${e.name}:\``)
 .join(', ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM