EmbedBuilder troubles

I've beeb trying to make an inventory sort of embed for a couple hours now and im completely stuck. i'm sure theres a way to do this but I can't figure it out, .addFields in the embed just doesnt seem to work when theres a for loop and It's just what i need so i'm not too sure what to do here is my code:
async function createCollectionEmbed(cardIds) {
const maxCards = 10; // You can change this value to display more or fewer cards

const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("Player's Collection")
.setDescription(`Displaying ${Math.min(maxCards, cardIds.length)} cards from your collection:`);

for (let i = 0; i < Math.min(maxCards, cardIds.length); i++) {
const cardId = cardIds[i].cardId;
const { cardLevel, cardPrestige, cardEvo, cardName } = await getCardData(cardId);
const cardInfo = `ID: ${cardId}\nLevel: ${cardLevel}\nName: ${cardName}\nPrestige: ${cardPrestige}\nEvo: ${cardEvo}`;

.addFields(`**${i + 1})**`, cardInfo, false);
}

return embed;
}

module.exports = {
createEmbed,
createCollectionEmbed,
};
async function createCollectionEmbed(cardIds) {
const maxCards = 10; // You can change this value to display more or fewer cards

const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("Player's Collection")
.setDescription(`Displaying ${Math.min(maxCards, cardIds.length)} cards from your collection:`);

for (let i = 0; i < Math.min(maxCards, cardIds.length); i++) {
const cardId = cardIds[i].cardId;
const { cardLevel, cardPrestige, cardEvo, cardName } = await getCardData(cardId);
const cardInfo = `ID: ${cardId}\nLevel: ${cardLevel}\nName: ${cardName}\nPrestige: ${cardPrestige}\nEvo: ${cardEvo}`;

.addFields(`**${i + 1})**`, cardInfo, false);
}

return embed;
}

module.exports = {
createEmbed,
createCollectionEmbed,
};
i've also sent an image with how my folder structure looks like
6 Replies
d.js toolkit
d.js toolkit14mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Xu Xiaolan
Xu Xiaolan14mo ago
and just incase its needed, heres my collection.js code
const sqlite = require("sqlite3").verbose();
const { createCollectionEmbed } = require("../utils");
const { isUserRegistered, getPlayerID, getListCardId } = require("../database.js");

module.exports = {
name: "collection",
description: "Displays the user's collection",
async execute(message, args, interaction) {
const userId = message.author.id;

const registered = await isUserRegistered(userId);
if (!registered) {
message.reply("User is not registered, type t.start to register now.");
return;
}

const playerDb = new sqlite.Database("./database/player.db", sqlite.OPEN_READWRITE);

try {
const playerId = await getPlayerID(userId);
const ids = await getListCardId(playerDb, playerId);

const embed = await createCollectionEmbed(ids);
message.reply({embeds: [embed]});
} catch (err) {
console.log(err);
message.reply("An error occurred");
} finally {
playerDb.close();
}
},
};
const sqlite = require("sqlite3").verbose();
const { createCollectionEmbed } = require("../utils");
const { isUserRegistered, getPlayerID, getListCardId } = require("../database.js");

module.exports = {
name: "collection",
description: "Displays the user's collection",
async execute(message, args, interaction) {
const userId = message.author.id;

const registered = await isUserRegistered(userId);
if (!registered) {
message.reply("User is not registered, type t.start to register now.");
return;
}

const playerDb = new sqlite.Database("./database/player.db", sqlite.OPEN_READWRITE);

try {
const playerId = await getPlayerID(userId);
const ids = await getListCardId(playerDb, playerId);

const embed = await createCollectionEmbed(ids);
message.reply({embeds: [embed]});
} catch (err) {
console.log(err);
message.reply("An error occurred");
} finally {
playerDb.close();
}
},
};
help would be really appreciated, i haven't been this stuck on coding this as much as now
treble/luna
treble/luna14mo ago
what version are you on? iirc in v14 .addFields is an array consisting of {name: "", value: ""} objects
Xu Xiaolan
Xu Xiaolan14mo ago
I'm preeetty sure I'm v14, I thought I tried that and it didn't work but ill try again, thought I'm really unsure if I'll even syntax it properly Yeah I tried that and wouldn't work, I've been working on smthn a lil differently which also didn't work but I'll post my code in a bit
async function addCardField(embed, cardIds, index, maxCards) {
if (index >= maxCards || index >= cardIds.length) {
return embed;
}

const cardId = cardIds[index].cardId;
const { cardLevel, cardPrestige, cardEvo, cardName } = await getCardData(cardId);
const cardInfo = `ID: ${cardId}\nLevel: ${cardLevel}\nName: ${cardName}\nPrestige: ${cardPrestige}\nEvo: ${cardEvo}`;

embed.addFields(
{ name: `**${index + 1})**`, value: cardInfo, inline: false }
);

return addCardField(embed, cardIds, index + 1, maxCards);
}


async function createCollectionEmbed(cardIds) {
const maxCards = 3; // You can change this value to display more or fewer cards
const numberOfCards = maxCards;

const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("Player's Collection")
.setDescription(`Displaying ${numberOfCards} cards from your collection:`);

return addCardField(embed, cardIds, 0, maxCards);
}
async function addCardField(embed, cardIds, index, maxCards) {
if (index >= maxCards || index >= cardIds.length) {
return embed;
}

const cardId = cardIds[index].cardId;
const { cardLevel, cardPrestige, cardEvo, cardName } = await getCardData(cardId);
const cardInfo = `ID: ${cardId}\nLevel: ${cardLevel}\nName: ${cardName}\nPrestige: ${cardPrestige}\nEvo: ${cardEvo}`;

embed.addFields(
{ name: `**${index + 1})**`, value: cardInfo, inline: false }
);

return addCardField(embed, cardIds, index + 1, maxCards);
}


async function createCollectionEmbed(cardIds) {
const maxCards = 3; // You can change this value to display more or fewer cards
const numberOfCards = maxCards;

const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("Player's Collection")
.setDescription(`Displaying ${numberOfCards} cards from your collection:`);

return addCardField(embed, cardIds, 0, maxCards);
}
im using these functions like a recursive type of thing and im getting output but im only getting the first card in my database thats assigned to my userid the maxCard's i have just simply doesnt work and im not sure why at all the code i have in collections.js hasnt changed
Xu Xiaolan
Xu Xiaolan14mo ago
Xu Xiaolan
Xu Xiaolan14mo ago
Oh that could be it, cardIds should be a list of the ids a player owns Then if it's that, then getListCardId isn't working I'll need to check that
async function getListCardId(playerDb, playerId) {
return new Promise((resolve, reject) => {
playerDb.all("SELECT cardid AS cardId FROM playerdb WHERE playerid = ? ORDER BY cardid LIMIT 1",
[playerId], (err, ids) => {
if (err)
{
reject(err);
}
else
{
resolve(ids);
}
})
})
}
async function getListCardId(playerDb, playerId) {
return new Promise((resolve, reject) => {
playerDb.all("SELECT cardid AS cardId FROM playerdb WHERE playerid = ? ORDER BY cardid LIMIT 1",
[playerId], (err, ids) => {
if (err)
{
reject(err);
}
else
{
resolve(ids);
}
})
})
}
Ig ill read over this and see if it's doing as it should Oh goddammit I put LIMIT 1 That was indeed the problem, thanks guys I'm so stupid lok Lol