【发布时间】: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