I'm currently working on a command show a user's profile using and embed but every time the command gets used the text gets added again. so if you use the command twice you see the text twice and so on. I've tried to find a solution for about an hour but I can't find anything. I've also tried to rewrite the code multiple times and I currently have this
const { discord, MessageEmbed } = require('discord.js');
const embed = new MessageEmbed();
const users = require('../users.json');
const stand = require('../standsInfo.json');
module.exports = {
name: 'profile',
description: 'show the users profile',
execute(message, args) {
var user = message.author;
var xpNeeded = (users[user.id].level (users[user.id].level 1))*45;
embed.setTitle(`${user.username}'s profile`);
embed.setThumbnail(user.displayAvatarURL());
embed.addField('level', `${users[user.id].level}`);
embed.addField('experience', `${users[user.id].xp}/${xpNeeded}`);
message.channel.send({embeds: [embed] });
}
}
edit: so. I just realized what was wrong I used addField instead of setField
CodePudding user response:
Move const embed = new MessageEmbed();
to inside the execute scope. Otherwise you will keep editing the same embed and sending again, with added fields
const { discord, MessageEmbed } = require('discord.js');
const users = require('../users.json');
const stand = require('../standsInfo.json');
module.exports = {
name: 'profile',
description: 'show the users profile',
execute(message, args) {
var user = message.author;
var xpNeeded = (users[user.id].level (users[user.id].level 1))*45;
const embed = new MessageEmbed();
embed.setTitle(`${user.username}'s profile`);
embed.setThumbnail(user.displayAvatarURL());
embed.addField('level', `${users[user.id].level}`);
embed.addField('experience', `${users[user.id].xp}/${xpNeeded}`);
message.channel.send({embeds: [embed] });
}
}