Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的程序在管理这个JSON文件时会崩溃?_Javascript_Node.js_Json_Discord.js - Fatal编程技术网

Javascript 为什么我的程序在管理这个JSON文件时会崩溃?

Javascript 为什么我的程序在管理这个JSON文件时会崩溃?,javascript,node.js,json,discord.js,Javascript,Node.js,Json,Discord.js,每当有人发送!setmainchannel,我的程序崩溃。我将Node.js与discord.js包一起使用。这是我唯一的问题。我不知道也不知道到底出了什么问题,如果有人能帮我,我将不胜感激 bot.js: let Discord=require(“Discord.js”); 让client=newdiscord.client(); 设fs=require('fs'); let help=require(“./helpembed.json”); var jsonRead=函数(文件路径){ fs

每当有人发送
!setmainchannel
,我的程序崩溃。我将Node.js与discord.js包一起使用。这是我唯一的问题。我不知道也不知道到底出了什么问题,如果有人能帮我,我将不胜感激

bot.js

let Discord=require(“Discord.js”);
让client=newdiscord.client();
设fs=require('fs');
let help=require(“./helpembed.json”);
var jsonRead=函数(文件路径){
fs.readFile(文件路径'utf-8',(err,returnJson)=>{
如果(错误){
控制台日志(err);
}
否则{
让ret=JSON.parse(returnJson);
返回ret;
}
})
}
var jsonWrite=函数(文件路径,objIn){
writeFile(filePath,JSON.stringify(objIn),err=>{
如果(错误){
控制台日志(err);
}
})
}
client.on(“message”,msg=>{
让时间=新日期();
让cstTimeStamp=`${time.getMonth()+1}/${time.getDate()}/${time.getFullYear()}${time.getHours()+1}:${time.getMinutes()}:${time.getSeconds()}`
如果(msg.content==“!setmainchannel”){
让mainChannel=msg.indications.channels.first();
如果(!主通道){
console.log(`${cstTimeStamp}@${msg.author.tag}请求设置主通道,但未提供通道\n`);
msg.channel.send(“没有可设置主频道的频道”);
}
否则{
让currentSettings=jsonRead(“./main channel.json”);
currentSettings.channel=主通道;
jsonWrite(“./main channel.json”,currentSettings);
console.log(`${cstTimeStamp}@${msg.author.tag}将主通道设置为${currentSettings.channel}\n`);
msg.channel.send(`将主通道设置为${currentSettings.channel}`);
}
}
})
client.once('ready',()=>{
log('Bot处于联机状态\n');
});
client.login(“出于安全原因隐藏令牌”);
main channel.json

{
“通道”:空
}

首先,您的bot什么也不做,因为它只在(msg.content==“!setmainchannel”)的情况下才会做出反应。如果提供一个参数,如通道(
!setmainchannel#ch name
),它将不会运行,因为消息的内容不完全是前缀和命令

你需要先解决这个问题。通过删除前缀并切掉参数,您可以获取命令本身并检查是否使用了这个确切的命令。(同样,您不应该检查整个
消息内容

其次,您不必等待
readFile
中的回调运行。这意味着,当您尝试更新
currentSettings.channel
时,您的
jsonRead()
函数刚刚开始读取文件,并且
currentSettings
未定义。您将无法更新其
频道
属性

我已经更新了
jsonWrite
jsonRead
函数,以返回承诺并使其更易于使用。检查以下工作代码:

const Discord=require(“Discord.js”);
常数fs=要求('fs');
const path=require('path');
const client=new Discord.client();
常量前缀='!';
client.on('message',async(msg)=>{
//创建一个args变量,将前缀切掉并拆分为数组
const args=msg.content.slice(prefix.length.split(+/);
//通过获取数组中的第一个元素来创建命令变量
//并将其从args中删除
const命令=args.shift().toLowerCase();
const cstTimeStamp=new Date().toLocaleString();
如果(命令=='setmainchannel'){
const mainChannel=msg.indications.channels.first();
const filePath=path.resolve(_dirname,'./main channel.json');
如果(!主通道){
console.log(`${cstTimeStamp}${msg.author.tag}请求设置主通道,但未提供通道\n`);
返回msg.channel.send(“没有可设置主频道的频道”);
}
试一试{
const currentSettings=await jsonRead(文件路径);
currentSettings.channel=主通道;
jsonWrite(文件路径、当前设置);
console.log(`${cstTimeStamp}${msg.author.tag}将主通道设置为${currentSettings.channel}\n`);
msg.channel.send(`将主通道设置为${currentSettings.channel}`);
}捕捉(错误){
控制台日志(err);
}
}
});
client.once('ready',()=>{
log('Bot处于联机状态\n');
});
client.login(“出于安全原因隐藏令牌”);
函数jsonRead(文件路径){
返回新承诺((解决、拒绝)=>{
fs.readFile(文件路径,'utf-8',(错误,内容)=>{
如果(错误){
拒绝(错误);
}否则{
试一试{
const parsedContent=JSON.parse(content);
解析(解析内容);
}捕捉(错误){
拒绝(错误);
}
}
});
});
}
函数jsonWrite(文件路径、数据){
返回新承诺((解决、拒绝)=>{
fs.writeFile(文件路径,JSON.stringify(数据),(错误)=>{
如果(错误){
拒绝(错误);
}
决心(正确);
});
});
}

好吧,我明白了。我让逻辑只对
作出反应!setmainchannel
,而我应该在if(msg.content.startsWith(“!setmainchannel”)中执行

有错误消息吗?@supersormer没有错误消息发生,但我现在意识到该程序什么也不做,并且继续接受任何其他命令。不,这不是唯一的错误。仅更改该命令不会解决问题,它仍然会导致类型错误。有关更多详细信息,请参阅我的答案。