Home > OS >  CMD sending multiple embeds instead of one
CMD sending multiple embeds instead of one

Time:12-25

I made a leaderboard command for my discord bot (v13) but it sends multiple embeds with each user's info individually instead of one whole message. I'm not sure how else to structure this so any help is appreciated.

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

CodePudding user response:

The reason is the way you send the embed

Inside your for loop you create a new ebed every time and add the fields, where you need to create the embed outisde the for loop, add the field in the for loop and then send the embed

It would look something like:

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] });
  • Related