简体   繁体   中英

DiscordAPIError: Cannot send an empty message

First time asking a question. Have gone through similar questions but does not seem to match my inquiry.

Trying to create a calendar with discord bot and getting the favorable "Cannot send an empty message." The bot is able to read the commands and using fs.readFileSync on the specific file returns favorable results.

However, I am trying to use fs.readdir to get the list of files first and then iterate through them with a for loop. The console.log shows favorable results but the bot shows DiscordAPIError: Cannot send an empty message.

readFS.js file

var fs = require("fs");

function readPath(){

    fs.readdir('./calendar/', function(err, list){
        if(err) throw err;

        for(let i=0; i<list.length; i++){
            return readFile('./calendar/'+list[i]);
            //return list[i];
        }
    })
}

function readFile(file, err){
    if(err) return err;

    console.log(JSON.parse(fs.readFileSync(file)))
    return JSON.stringify(JSON.parse(fs.readFileSync(file)));

}

process.on('unhandledRejection', (reason, promise) => {
    console.log('Unhandled Rejection at:', reason.stack || reason)
})

module.exports = {
    readFile,
    readPath
}

I would like to stay away from promises if possible as they have been kind of hard for me to completely understand. The discord bot is able to discern my arguments with no issue.

bot.js file
else if(arguments.length > 0 && arguments[0] == "view" && arguments[1] == 'list'){
        receivedMessage.channel.send("Retreiving Calendar!")
        receivedMessage.channel.send(read.readPath())
        //console.log(read.readPath())

    }

Actual Error in console:

Command received: calendar
Arguments: view,list
{ Title: 'Test Event',
  Description: 'Event for testing the output of JSON',
  StartDate: 'Today',
  StartTime: '3 hours from now',
  LockedTo: 'Guild Only' }
Unhandled Rejection at: DiscordAPIError: Cannot send an empty message
    at item.request.gen.end (C:\Users\afrederick\Documents\GitHub\xtreamexe\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15)
    at then (C:\Users\afrederick\Documents\GitHub\xtreamexe\node_modules\snekfetch\src\index.js:215:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Hopefully I put enough information.

read.readPath() basically returns void or undefined .

Why? Because you are returning the readFile() in the callback function itself. It's not going outside the fs.readdir function. So the callback function inside the readdir returns the value not fs.readdir . Hopefully it makes sense.

First of all, use promises, callbacks are messy and outdated.

But if you want a simple callback solution you have to add another callback to readPath and call it from there :-)

function readPath(cb){

    fs.readdir('./calendar/', function(err, list){
        if(err) throw err;

        for(let i=0; i<list.length; i++){
            return cb(null, readFile('./calendar/'+list[i]));
            //return list[i];
        }
    })
}

bot.js file

else if(arguments.length > 0 && arguments[0] == "view" && arguments[1] == 'list'){
        read.readPath(function(err, data){
            receivedMessage.channel.send("Retreiving Calendar!")
            receivedMessage.channel.send(data)
        })
    }

But again, use Promises with conjunction with async/await , will be easier to understand as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM