【问题标题】:Discord.js v12 Emoji List CommandDiscord.js v12 表情符号列表命令
【发布时间】:2021-01-28 12:42:45
【问题描述】:

我正在尝试在 discord.js v12 中创建表情符号列表命令。但是,如果我在具有许多表情符号的服务器中运行该命令,则会收到 Invalid Form Body 错误,因为嵌入描述不能超过 2048 个字符。因此,我试图拆分消息。这是我的代码:

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

即使在此之后,我也会收到相同的 Invalid Body Form 错误。这是我得到的错误:

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

你能帮帮我吗?提前致谢!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    问题在于emojis 是一个数组,而不是字符串。 .map 函数返回一个数组(在您的情况下为字符串)。修复应该很简单,只需在.map 函数的末尾添加一个.join 以使其成为字符串。检查下面的代码:

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

    【讨论】:

    • 如果这是可能的。因此,以这种方式发送列表,但是在列表的末尾,列表的一部分被切断,其余部分在另一个嵌入中继续。机器人是否有可能在第一个列表中发送完整的表情符号而不切断任何表情符号并继续下一个嵌入
    • 看看Discord JS implements the split property是怎么做到的。这会尝试在换行符 (\n) 上干净地拆分消息。您可以尝试用逗号分隔消息,因为这是代码中表情符号之间的分隔符
    猜你喜欢
    • 2021-01-21
    • 2021-01-11
    • 2021-01-01
    • 2021-02-18
    • 1970-01-01
    • 2021-01-18
    • 2021-04-14
    • 2021-04-19
    • 2021-04-18
    相关资源
    最近更新 更多