Nguyen Minh An
Nguyen Minh An

Reputation: 21

Discord JS how to read and send data from web (JSON)

I'm currently working on a Discord BOT, which allow member can check for deaths/infecteds/recovereds COVID-19. Data from https://code.junookyo.xyz/api/ncov-moh/data.json.

I'm new to NodeJS, how can I read data from it and send it to user (pic 1)

Can someone help me please ? Thanks!

Image

Upvotes: 0

Views: 1986

Answers (2)

Nguyen Minh An
Nguyen Minh An

Reputation: 21

Haha thanks guy, my friend just help me. This is the code, you can use it if you have the same problem with me :P

if(cmd === `${prefix}covid`) {
    return request("https://code.junookyo.xyz/api/ncov-moh/data.json", (err, response, body) => {
        if (err) throw(err);
        var data = JSON.parse(body);

        let vietnam = new Discord.MessageEmbed()
        .setColor('#32a860')
        .setTitle('Thống kê tại Việt Nam')
        .addField('☢️Nhiễm:', data.data.vietnam.cases)
        .addField('🍀Hồi phục:', data.data.vietnam.recovered)
        .addField('☠️Tử vong:', data.data.vietnam.deaths)

        message.channel.send(vietnam);

        let thegioi = new Discord.MessageEmbed()
        .setColor('#32a860')
        .setTitle('Thống kê tại Thế giới')
        .addField('☢️Nhiễm:', data.data.global.cases)
        .addField('🍀Hồi phục:', data.data.global.recovered)
        .addField('☠️Tử vong:', data.data.global.deaths)

        message.channel.send(thegioi);  
        message.channel.send("Nhớ giữ sức khỏe nhé bạn <3")     
    });                 
}

Upvotes: 1

user13429955
user13429955

Reputation:

Do you already know how to receive the data? If not you can use node-fetch or axios

example: (needs to be in async function), or use .then

const fetch = require("node-fetch");
const res = await fetch("https://code.junookyo.xyz/api/ncov-moh/data.json");
const data = await res.json();
const { vietnam, global } = data.data;
//now you don't have to do data.data.global.deaths, just global.deaths

Upvotes: 1

Related Questions