【问题标题】:CMD sending multiple embeds instead of oneCMD 发送多个嵌入而不是一个
【发布时间】:2022-01-24 20:48:58
【问题描述】:

我为我的 discord 机器人 (v13) 创建了排行榜命令,但它单独发送了多个嵌入每个用户的信息,而不是一个完整的消息。我不确定如何构造它,因此感谢您的帮助。

const profileModel = require("../models/profileSchema");
module.exports = {
    name: "leaderboard",
    description: "Checks the leaderboard",
    aliases: ['lb'],
    async execute(client, message, args, cmd, Discord, profileData){

        const lbData = await profileModel.find({}).sort({
            reputation: -1,
        }).limit(5);

        for (let counter = 0; counter < lbData.length; ++counter) {
            const { userID, health = 0 } = lbData[counter]

            const Embed = new Discord.MessageEmbed()
                .setColor('#fffffa')
                .setTitle('Challenge Leaderboard')
                .addFields(
                    { name: '\u200b', value: `**${counter + 1}.** <@${userID}> - ${reputation} reputation\n` })
            message.channel.send({ embeds: [Embed] });
        }
    }
}

【问题讨论】:

  • 您在循环的每次迭代中调用send(),因此会出现这种行为。您需要在循环之前初始化嵌入,在每次迭代时连接到它,然后在循环完成后发送它

标签: node.js discord discord.js bots


【解决方案1】:

原因是你发送嵌入的方式

在您的 for 循环中,您每次都创建一个新的 ebed 并添加字段,您需要在 for 循环之外创建嵌入,在 for 循环中添加字段,然后发送嵌入

它看起来像:

const Embed = new Discord.MessageEmbed()
    .setColor('#fffffa')
    .setTitle('Challenge Leaderboard')

for (let counter = 0; counter < lbData.length; ++counter) {
    const { userID, health = 0 } = lbData[counter]
    Embed.addField({ name: '\u200b', value: `**${counter + 1}.** <@${userID}> - ${reputation} reputation\n` })            
}

message.channel.send({ embeds: [Embed] });

【讨论】:

  • 它是 Embed.addFields 带 s 但它有效,我现在明白了,谢谢。
猜你喜欢
  • 2018-07-14
  • 2021-07-24
  • 2019-04-03
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多