繁体   English   中英

DiscordAPIError:无法发送空消息

[英]DiscordAPIError: Cannot send an empty message

第一次提问。 经历过类似的问题,但似乎与我的询问不符。

尝试使用 discord bot 创建日历并获得有利的“无法发送空消息”。 bot 能够读取命令并在特定文件上使用 fs.readFileSync 返回有利的结果。

但是,我尝试使用 fs.readdir 首先获取文件列表,然后使用 for 循环遍历它们。 console.log 显示有利的结果,但机器人显示 DiscordAPIError:无法发送空消息。

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
}

如果可能的话,我想远离承诺,因为它们让我很难完全理解。 不和谐机器人能够毫无问题地辨别我的论点。

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())

    }

控制台中的实际错误:

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)

希望我提供了足够的信息。

read.readPath()基本上返回voidundefined

为什么? 因为您正在回调函数本身中返回readFile() 它不会超出fs.readdir函数。 所以readdir的回调函数返回的不是fs.readdir的值。 希望这是有道理的。

首先,使用promise,回调乱七八糟,过时了。

但是如果你想要一个简单的回调解决方案,你必须向readPath添加另一个回调并从那里调用它:-)

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 文件

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)
        })
    }

但同样,将Promisesasync/await结合使用,也会更容易理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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