Multiple Embeds Being Sent
Hi!
I'm currently working on sending embeds but I'm having an issue where multiple embeds are being sent each time the command is executed.
I'm using v20.17.0 (node -v)
This should be the code:
const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js');
const fs = require('fs');
const path = require('path');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.once('ready', () => {
console.log(
Logged in as ${client.user.tag});
});
function loadEmbeds(filename) {
try {
const rawData = fs.readFileSync(path.join(__dirname, 'embeds', filename), 'utf-8');
const jsonData = JSON.parse(rawData);
return jsonData.embeds.map(embedData => {
const embed = new EmbedBuilder()
.setTitle(embedData.title || null)
.setDescription(embedData.description || null)
.setColor(embedData.color || null)
.setFooter(embedData.footer ? { text: embedData.footer.text } : null)
.setTimestamp(embedData.timestamp ? new Date(embedData.timestamp) : null)
.setImage(embedData.image ? embedData.image.url : null);
if (embedData.fields) {
embed.addFields(embedData.fields);
}
return embed;
});
} catch (error) {
console.error(
Error loading embed file: ${filename}, error);
return null;
}
}
client.on('messageCreate', message => {
if (!message.content.startsWith('!') || message.author.bot) return;
const commandName = message.content.slice(1).toLowerCase();
const embedFilename =
${commandName}.json;
const embeds = loadEmbeds(embedFilename);
if (embeds) {
message.channel.send({ embeds: embeds }).catch(console.error);
} else {
message.channel.send("This command does not exist or no embed is configured for it.");
}
});
client.login('AAAA');
2 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!just to clarify, I assume you mean that it's sending the same embed multiple times?
is it sending multiple messages per command, or multiple per message?
have you confirmed that you don't have multiple instances of your bot running?
just making absolutely sure, is there a reason why you say "this should be the code" rather than something more definite?