[英]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()
基本上返回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
結合使用,也會更容易理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.