xyCe
xyCe

Reputation: 35

Create Embedded message with data of csv - nodeJS

So today I got my script to extract data from a csv into an array and reduce + sort it. Now I wanna use that data to embed into a discord message. However the code for the embed message is not able to get the data as it is in a const that is out of its scope. Where would I need to start creating the discord embed or how do I get the data into the global scope.

const csv = require('csv-parser'); // npm install csv-parser
const fs = require('fs'); // npm install fs
const Discord = require('discord.js');

let data = []; // empty array in which the downloaded data will be pushed in

    fs.createReadStream('./FFA/csv/rankme.csv') // read csv file
    .pipe(csv({ delimiter: ',', from_line: 2 })) // data starts at line 2 ; line 1 will be headers
    .on('data', (row) => {
        const keyLookup = ['name', 'score', 'kills']; // constant to filter out the properties that are needed
        const newData = Object.keys(row)
        .filter(key => keyLookup.includes(key))
        .reduce((obj, key) => {
        obj[key] = row[key];  
        return obj; 
        },  {});   
        data.push(newData); // push data into the data array that we created at the beinning
    })    
    .on("close", () => {    
    let sorted = data.sort((a,b) => b.score - a.score).slice(0,5); // sorts the objects based on their points and only keeps the 5 highest
    console.log(sorted);    
    });



    const embedTop = new Discord.MessageEmbed();
    embedTop.setColor('#0099ff');
    embedTop.setTitle('FFA Deathmatch TOP 5');
    embedTop.setAuthor('BIG NUTS Bot','https://cdn.discordapp.com/app-icons/766003096517345300/f3a15990dd7f7ec50d8b9fea65047efc.png');
    embedTop.setDescription('FFA Leaderboard');
    embedTop.setThumbnail('https://cdn.discordapp.com/app-icons/766003096517345300/f3a15990dd7f7ec50d8b9fea65047efc.png');
    embedTop.addField('1. Place: ', sorted[0].name, ' - Score : ', sorted[0].score, ' - Kills : ', sorted[0].kills);
    embedTop.addField('2. Place: ', sorted[1].name, ' - Score : ', sorted[1].score, ' - Kills : ', sorted[1].kills);
    embedTop.addField('3. Place: ', sorted[2].name, ' - Score : ', sorted[2].score, ' - Kills : ', sorted[2].kills);
    embedTop.addField('4. Place: ', sorted[3].name, ' - Score : ', sorted[3].score, ' - Kills : ', sorted[3].kills);
    embedTop.addField('5. Place: ', sorted[4].name, ' - Score : ', sorted[4].score, ' - Kills : ', sorted[4].kills);
    embedTop.setFooter('Last update');
    embedTop.setTimestamp();
        
    message.channel.send(embedTop);


Upvotes: 0

Views: 577

Answers (2)

xyCe
xyCe

Reputation: 35

I found the issue . The readstream is never closed. I changed it with adding :

.var readStream = fs.createReadStream('./FFA/csv/rankme.csv') 
(....)
.on('end', function () {
    readStream.destroy();
}) 

now I get the console.log(sorted); in my console and an error that the channel is not defines. But I will be able to fix that. Thanks for your help

Upvotes: 1

Technoh
Technoh

Reputation: 1600

You need to place all the code that creates MessageEmbed inside the callback for your on close event. Something like this:

.on("close", () => {    
let sorted = data.sort((a,b) => b.score - a.score).slice(0,5); // sorts the objects based on their points and only keeps the 5 highest
console.log(sorted);

const embedTop = new Discord.MessageEmbed();
embedTop.setColor('#0099ff');
embedTop.setTitle('FFA Deathmatch TOP 5');
embedTop.setAuthor('BIG NUTS Bot','https://cdn.discordapp.com/app-icons/766003096517345300/f3a15990dd7f7ec50d8b9fea65047efc.png');
embedTop.setDescription('FFA Leaderboard');
embedTop.setThumbnail('https://cdn.discordapp.com/app-icons/766003096517345300/f3a15990dd7f7ec50d8b9fea65047efc.png');
embedTop.addField('1. Place: ', sorted[0].name, ' - Score : ', sorted[0].score, ' - Kills : ', sorted[0].kills);
embedTop.addField('2. Place: ', sorted[1].name, ' - Score : ', sorted[1].score, ' - Kills : ', sorted[1].kills);
embedTop.addField('3. Place: ', sorted[2].name, ' - Score : ', sorted[2].score, ' - Kills : ', sorted[2].kills);
embedTop.addField('4. Place: ', sorted[3].name, ' - Score : ', sorted[3].score, ' - Kills : ', sorted[3].kills);
embedTop.addField('5. Place: ', sorted[4].name, ' - Score : ', sorted[4].score, ' - Kills : ', sorted[4].kills);
embedTop.setFooter('Last update');
embedTop.setTimestamp();
    
message.channel.send(embedTop);
});

Upvotes: 0

Related Questions