首页 > 解决方案 > 试图在 discord.js 嵌入机器人的列中显示玩家名称

问题描述

嘿伙计们,我想弄清楚如何嵌入一个基本上显示团队名册的机器人,但我不知道如何在列中制作团队球员的名字。我尝试将其放入 .addfield 中,但没有奏效。这是我正在尝试做的一个例子1

    case 'roster':
        const roster = new Discord.MessageEmbed()
        .setTitle('FinalSpark')
        .setDescription('Rank: 3 | Region: EU | League: CCL')
        .setURL('https://club.mpcleague.com')
        .setFooter('bot made by alex :D')
        .addField('Roster')
        message.channel.send(roster);
        break;

标签: botsdiscorddiscord.jsembed

解决方案


看起来嵌入使用了两个字段,第二个字段的标题为空白

您应该这样做的方法是首先将您的花名册列表放入一个数组中,然后将数组分成两半:

//your roster array
const roster = [];
//round up so the first row always either has equal or more items
//use Math.floor() if you want the opposite
const midIndex = Math.ceil(roster.length / 2);

const firstRow = roster.slice(0, midIndex);
const secondRow = roster.slice(midIndex);

const embed = new Discord.MessageEmbed()
    .setTitle('FinalSpark')
    .setDescription('Rank: 3 | Region: EU | League: CCL')
    .setURL('https://club.mpcleague.com')
    .addField("Roster", firstRow.join("\n"), true);

if (secondRow.length) { 
    // \u200b is what makes the invisble effect, you can have \u200b
    // for both the key and value which would essentially make a blank line
    embed.addField("\u200b", secondRow.join("\n"), true);
}

message.channel.send(embed);

当然,那将在案例陈述中。


推荐阅读