0
votes

Hello.

I coded a simple MessageEmbed function (with Discord.JS) and every time that I call it, the new embed that is sent in the channel has his field who adds up with the precedent (e.g.: if the embed should have 2 fields, the next time that the command will be called it will have 2*2 the required fields. If you call it again, 3*2, 4*2, etc.).

When I restart the bot it reset. I tried to reset the embed value but it didn't affect the problem. Could you help me please ?

Here is my JS command :

module.exports = {
    name: 'drive',
    execute(client, message, args, embed) {
        message.channel.send(embed
            .setColor('#0099ff')
            .setTitle('abcdedfg')
            .setDescription('abcdedfg \n\u200B')
            .setThumbnail('abcdedfg')
            .addFields(
                { name: 'abcdedfg :', value: 'link' },
                { name: 'abcdedfg :', value: 'link \n\u200B' },
            )
            .setFooter('abcdedfg'))
            .catch(console.error);
    }
}

And here is my main if needed :

const fs = require('fs');
const { Client, Collection, MessageEmbed } = require('discord.js');
const { TOKEN, PREFIX } = require('./config/config');

const client = new Client();
client.commands = new Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const embed = new MessageEmbed();

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
} 

client.on('message', message => {
    if (!message.content.startsWith(PREFIX) || message.author.bot) return;

    const args = message.content.slice(PREFIX.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (!client.commands.has(command)) return;
    client.commands.get(command).execute(client, message, args, embed);
});

client.login(TOKEN); 
2
Its quite simple. don't add the fields.mw509
@mw509 Okay, and when should I add them ?Jpn287
I have posted an answer. please try and let me knowmw509
@mw509 Oh yes I see my problem... It so dumb... Sorry and thank you ^^Jpn287
Its fine. It happens.mw509

2 Answers

1
votes

I found the answer to block the fields from being added every times to the the fields array here on StackOverFlow.

So the code answer is addind a embed.fields = []; at the end :

module.exports = {
    name: 'drive',
    execute(client, message, args, embed) {
        message.channel.send(embed
            .setColor('#0099ff')
            .setTitle('abcdedfg')
            .setDescription('abcdedfg \n\u200B')
            .setThumbnail('abcdedfg')
            .addFields(
                { name: 'abcdedfg :', value: 'link' },
                { name: 'abcdedfg :', value: 'link \n\u200B' },
            )
            .setFooter('abcdedfg'))
            .catch(console.error);
            embed.fields = [];
    }
}
0
votes

Its quite simple. don't add fields. adding fields is what adds new fields so if you do not want them do not add them. else, clear the old ones before adding new ones.

My recommendation, use this;

module.exports = {
    name: 'drive',
    execute(client, message, args, embed) {
        message.channel.send(embed
            .setColor('#0099ff')
            .setTitle('abcdedfg')
            .setDescription('abcdedfg \n\u200B')
            .setThumbnail('abcdedfg')
            .setFooter('abcdedfg'))
            .catch(console.error);
    }
}

Try this and let's see.

As long as the first fields exist, you don't ever have to add.