'Node.js- Dynamically creating array or map for discord js embedMessage
I am working on a bot for discord right now using the discord JS module.
However I am running into a stopping point right now where I do not seem to be able to do the logic that I want to do.
I have a table in a database there look like the following
Table: recruits
Column 1: id
Column 2: class
Column 3: spec
Column 4: status
Column 5: class_icon
Column 6: spec_icon
One class can have multiple spec's.
I want to find all classes and find all associated specs to that class and from there build up the "fields" section of the embded msg so that it ends up looking something like the following:
{
name: '<:dk:963405789702283274> CLASS 1:',
value: ' \
Spec1 of class 1 - Status \n \
Spec2 of class 1 - Status \n \
Spec3 of class 1 - Status',
inline: true
},
{
name: 'CLASS 2:',
value: '\
Spec1 of class 2 - Status \n \
Spec2 of class 2 - Status \n \
Spec3 of class 2 - Status',
inline: true
},
When I do my first findAll in the database I get all the entries for the classes/specs, and I then try to loop through that.
The following code is where i am stuck now, I am not sure where to continue or how to even build the structure like that..:
const recruits = await config.findAll({ attributes: ['class', 'spec', 'status', 'class_icon', 'spec_icon'], group: ['class'] });
const recruitEmbed = {
color: '15c9c9',
title: 'Recruitment status',
fields: [],
timestamp: new Date()
}
for(const recruit of recruits)
{
//Here I get all the classes
const specs = await config.findAll({ attributes: ['spec', 'spec_icon'], where: { class: recruit.class }});
console.log(recruit.class)
for(const spec of specs )
{
// Here I get all the spec for that class
console.log(spec.spec)
}
}
//recruitEmbed.fields.push({ name: recruit.class_icon + recruit.class, value: spec.spec_icon + spec.spec + " - " + recruit.status, inline: true });
client.channels.cache.get(rooms.recruitment).send({embeds: [recruitEmbed]});
I hope it some what makes sense and that somebody can give a tip on where to continue.
Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
