Javascript 如何为一起运行命令添加检查

Javascript 如何为一起运行命令添加检查,javascript,discord,discord.js,Javascript,Discord,Discord.js,我想在执行accept命令之前添加一个检查以查看是否发送了请求,我做了一些研究,以确定这是否可能,并发现您可以使用fs.writeFileSync和fs.readFileSync()来存储和读取数据 这是主命令,我想用它发送提到的用户id,并告诉accept命令,此人确实收到了请求 (旧-这是固定的,并在下面解释) 这是accept命令,我希望它检查文件,看看他们是否收到了请求,如果没有的话,忽略这个命令。 (旧-这是固定的,并在下面解释) 这就是我得到的错误: [ERR_INVALID_ARG

我想在执行accept命令之前添加一个检查以查看是否发送了请求,我做了一些研究,以确定这是否可能,并发现您可以使用fs.writeFileSync和fs.readFileSync()来存储和读取数据

这是主命令,我想用它发送提到的用户id,并告诉accept命令,此人确实收到了请求 (旧-这是固定的,并在下面解释)

这是accept命令,我希望它检查文件,看看他们是否收到了请求,如果没有的话,忽略这个命令。 (旧-这是固定的,并在下面解释)

这就是我得到的错误:

[ERR_INVALID_ARG_VALUE]: The argument 'true' is invalid encoding. Received 'encoding'
它说错误在第136行,这是这一行:

try {
    sentRequest == fs.readFileSync('./variable.json', variable[user].sentRequest, 'utf8') // This is line 136
} catch(err) {
    console.error(err);
} 
基本上,我能做些什么来解决这个问题,或者有更好的方法来解决这个问题

我不喜欢使用fs.read/write,因为我不想使用单独的文件来保存请求,如果可能的话,我希望所有这些都放在一个文件中

但如果这是唯一可用的东西,那么它就必须这么做

编辑:我实际上开始这样做是因为我在读取文件时遇到了问题,当文件为空或者用户没有请求时,它会崩溃,但是我添加了一个try{},并且修复了这个问题,虽然它仍然抛出错误,但它不会崩溃,这就是我所关心的

现在无论文件中是否有人,它都无法读取该文件,它显示了上述错误。起初,我试图将其存储为bool,但由于遇到类似错误,不得不将其转换为字符串

编辑2:我想出来了。将为搜索此问题的其他人编辑此内容

GetUserFromAntify函数,如果您想使用它(我从discordjs.guide获得):

这将生成一个名为request的新映射

这是我的新请求命令:

if (args[0] === 'request') {
    if (args[1] == null){
        return message.reply(`Hmmm... It doesnt look like you mentioned anyone... \n Try this \`${prefix}interview request @ChocolateReaper\``);
    } else if (args[1] !== null){
        if (args[1] === 'help') {
            return message.channel.send(`To send a request you must mention the user! (example: @ChocolateReaper)`);
        }
                        
        const user = getUserFromMention(args[1]);
        const author = message.author;
            
        // This checks if the command mentioned a user
        if (!user) {
            return message.reply('The user you mentioned either doesnt exist or isnt in the server, please check the spelling and try again.');
        }
        // the user.id HAS to be a string so if you need to use a variable use `` not '' or "" it's next to the 1, you cant just do let keyString = user.id it wont work
        let keyString = `${user.id}`

        // This is just for testing, it's not needed
        request.set(keyString, `${user.id}`);
        console.log('User ID: ' + request.get(keyString));
        console.log(request);
        // This is just sending the mentioned user a message telling them to accept or deny the invitation. 
        user.send(`Hello, ${user}, ${author} has requested an interview to discuss being a Content Creator with ${message.guild.name}, please reply back with an available date and time for an interview! If this date and time works with you please type $accept, if not type $deny`).catch(()=>{ console.log("I wasn't able to send the message, please try again")});
                        
        return message.channel.send(`Request Message was sent successfully to ${user.displayName}!`);
    }
}
这是我新的accept命令(注意:这只能在dm中使用,因此检查它是否在dm中)


如果你觉得这很有帮助,请向上投票,让更多人可以看到它

这个错误从字面上告诉你出了什么问题?您将错误的参数传递给了
readFileSync
,请仔细阅读,然后确保您的参数顺序正确,并且包装正确。
try {
    sentRequest == fs.readFileSync('./variable.json', variable[user].sentRequest, 'utf8') // This is line 136
} catch(err) {
    console.error(err);
} 
function getUserFromMention(mention) {
    // The id is the first and only match found by the RegEx.
    const matches = mention.match(/^<@!?(\d+)>$/);

    // If supplied variable was not a mention, matches will be null instead of an array.
    if (!matches) return;

    // However the first element in the matches array will be the entire mention, not just the ID,
    // so use index 1.
    const id = matches[1];

    return client.users.cache.get(id);
}
let request = new Map()
if (args[0] === 'request') {
    if (args[1] == null){
        return message.reply(`Hmmm... It doesnt look like you mentioned anyone... \n Try this \`${prefix}interview request @ChocolateReaper\``);
    } else if (args[1] !== null){
        if (args[1] === 'help') {
            return message.channel.send(`To send a request you must mention the user! (example: @ChocolateReaper)`);
        }
                        
        const user = getUserFromMention(args[1]);
        const author = message.author;
            
        // This checks if the command mentioned a user
        if (!user) {
            return message.reply('The user you mentioned either doesnt exist or isnt in the server, please check the spelling and try again.');
        }
        // the user.id HAS to be a string so if you need to use a variable use `` not '' or "" it's next to the 1, you cant just do let keyString = user.id it wont work
        let keyString = `${user.id}`

        // This is just for testing, it's not needed
        request.set(keyString, `${user.id}`);
        console.log('User ID: ' + request.get(keyString));
        console.log(request);
        // This is just sending the mentioned user a message telling them to accept or deny the invitation. 
        user.send(`Hello, ${user}, ${author} has requested an interview to discuss being a Content Creator with ${message.guild.name}, please reply back with an available date and time for an interview! If this date and time works with you please type $accept, if not type $deny`).catch(()=>{ console.log("I wasn't able to send the message, please try again")});
                        
        return message.channel.send(`Request Message was sent successfully to ${user.displayName}!`);
    }
}
if (command === 'accept' || message.content.includes("accept") || message.content.includes("Accept")) {
    if (message.author.bot) return;
    if (message.channel.type == "dm") { // If you want the second command to be used in any channel just remove this if
        const user = message.author;

        if (request.has(user.id)) { // this is checking if the message.auther.id matches one in the request map if it does proceed if not then do something else
            
            request.delete(user.id); // this is deleting the authors user.id from the map so that they cant spam the command
            
            console.log(request); // more testing stuff
            
            message.channel.send('Thanks for accepting the invitation! Don\'t be late, we are excited to talk to you!');
            client.channels.cache.get(botChannelID).send(`${message.author} has accepted the request!`); // This just send a message back to a channel and says this person has accepted, I also have a separate command for deny the says they denied
            // You can change botChannelID to your channel ID make sure to put it in 'single quotes' 
        } else {
            message.channel.send('you dumbass you know this wont work, I have the high ground'); // more testing stuff i'd recommend just putting a return; here so the bot will just ignore the command
        }
        
    } else {
        message.channel.send('This command can only be used in DMs'); // again if you dont want this to be dm only then take this out
    }
    return;
}