首页 > 解决方案 > 将消息分成 2 个嵌入

问题描述

我有命令显示服务器的所有角色。我知道消息可以发送的字符数量有上限,所以我做了这样的设置,这样如果超过限制,它就不会显示角色。但我试图让它拆分消息并发送包含服务器角色的 2 个嵌入

const Discord = require('discord.js');
const chalk = require('chalk');
console.log(chalk.white("Roles Loaded"));
module.exports = {
name: 'roles',
description: 'List the roles of the server',
serveronly: 'Yes',
aliases: ['title', 'role'],
usage:'.role',
category: 'Info',
Info:'Role',
cooldown: 1,
execute(client, message) {
    let rolemap = message.guild.roles.cache
    .sort((a, b) => b.position - a.position)
    .map(r => r)
    .join(",");
    if (rolemap.length > 1024) rolemap = "To many roles to display";
    if (!rolemap) rolemap = "No roles";
    const embed = new Discord.MessageEmbed()
    .setTitle('Sever roles')
    .setThumbnail(message.guild.iconURL({ dynamic: true }))
    .addField("Role List" , rolemap)
    .setColor(0x00ffff)
    .setFooter(`Requested By:${message.author.username}`, message.author.displayAvatarURL({ dynamic: true }));

    message.channel.send(embed );

}
}

在此处输入图像描述 在此处输入图像描述

标签: node.jsdiscorddiscord.js

解决方案


尝试使用 for 循环,如下所示:

let rolemap = message.guild.roles.cache
    .sort((a, b) => b.position - a.position)
    .map(r => r)
    .join(",");

for(let i = 0; i < rolemap.length; i += 1024) {
    const toSend = rolemap.substring(i, Math.min(rolemap.length, i + 2000));
          const roleEmbed = new Discord.MessageEmbed()
            .setTitle(`Roles`)
            .setDescription(toSend)
          message.channel.send(roleEmbed);
}

它使用子字符串将角色拆分为多个嵌入


推荐阅读