简体   繁体   English

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

[英]Emoji List Command Discord.js v12

I created an emoji list command here is my code of the command:我创建了一个表情符号列表命令,这是我的命令代码:

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

However I get this error in case the characters in the embed exceeds 2048 letters:但是,如果嵌入的字符超过 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.

Is there any way the bot can still show the emojis and there names.有什么方法可以让机器人仍然显示表情符号和名称。 By using discord-menu or like that.通过使用不和谐菜单或类似的。 I was unable to understand how to do that.我无法理解如何做到这一点。 Can you help me out?你能帮我吗? Thanks in Advance提前致谢

As you can probably tell by the error message, your embed description is too long.正如您可能从错误消息中得知的那样,您的嵌入描述太长了。 You can split your message into several parts by using string.split() and send each shortened string as a separate message.您可以使用string.split()将消息拆分为几个部分,并将每个缩短的字符串作为单独的消息发送。 Here's a rudimentary example.这是一个基本的例子。

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

Do take note that emojis is now a string rather than an Array .请注意emojis现在是一个string而不是一个Array

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

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