【问题标题】:DiscordAPIError: Cannot send an empty messageDiscordAPIError:无法发送空消息
【发布时间】:2020-04-07 14:02:50
【问题描述】:

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

尝试使用不和谐机器人创建日历并获得有利的“无法发送空消息”。该机器人能够读取命令并在特定文件上使用 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
}

如果可能的话,我想远离承诺,因为它们对我来说有点难以完全理解。 Discord 机器人能够毫无问题地辨别我的论点。

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)

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

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    read.readPath() 基本上返回void 或undefined。

    为什么?因为您在回调函数本身中返回 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)
            })
        }
    

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

    【讨论】:

    • 老实说。我也从未在回调方面取得过成功,因为当以您的方式使用时,我通常会得到“TypeError:回调不是函数”。我很欣赏快速响应,并且实际上看到我的主要函数调用没有得到响应有很大帮助。我用我的 console.log 检查了除那里之外的所有地方。
    • 我认为您应该阅读有关 node.js 中的异步函数的信息,如果您知道自己在做什么,就没有办法抛出像 cb in not a function 这样的错误。也许参考 javascript.info 资源非常好。但老实说,如果您使用的是节点,那么您必须使用异步的东西,因为这就是节点的用途。它就像一只不想飞的鸟。
    • 我的意思是,我之前已经尝试过很多次来理解回调,但每次我尝试使用它们时,它们都对我不起作用。无论如何,我都不是开发人员,并试图尽可能多地理解这一点,但其中一些东西在我脑海中飘过。尝试查看 youtube 视频,甚至购买了 Zenva 和 Udemy 的一些课程。所以我只是希望我不必使用回调或承诺。
    猜你喜欢
    • 2022-01-01
    • 2021-09-15
    • 2021-11-02
    • 2021-12-01
    • 2021-12-28
    • 2022-01-23
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多