David Bell
David Bell

Reputation: 23

DiscordAPIError: Cannot send an empty message. Cannot send Data from API to Discord Channel. Discord.js

I have a problem. I am trying to get the bitcoin price using coingecko api. If i console it, it works, but if i am trying to send it to channel it crashes and i get this error:

DiscordAPIError: Cannot send an empty message

This is my code:

const Discord = require("discord.js");
const client = new Discord.Client();
const coingecko = require("coingecko-api");
const fetch = require("node-fetch");
const axios = require('axios');
let api = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=USD";

module.exports ={
    name: 'bitcoin',
    description: 'Get Bitcoin Price',
    execute(msg,args){
        axios.get(api)
        .then((res) => {
            console.log('RES:', res.data.bitcoin);
            msg.reply(res.data.bitcoin);
        })

    }
}

Please help me. I have this problem since two days ago and i coudn't solve it. And maybe if you can help me to get out of that {} in the price ({ usd: 32664 }), it will be great. 😊

Upvotes: 1

Views: 84

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23141

You cannot send an object, and res.data.bitcoin is an object ({ usd: XXXXX }). Either send the value of res.data.bitcoin.usd or stringify it first:

module.exports ={
    name: 'bitcoin',
    description: 'Get Bitcoin Price',
    execute(msg,args){
        axios.get(api)
        .then((res) => {
            console.log('RES:', res.data.bitcoin);
            msg.reply(res.data.bitcoin.usd);
            // OR
            msg.reply(JSON.stringify(res.data.bitcoin))
        })
    }
}

Upvotes: 1

Related Questions