首页 > 解决方案 > Discord.js - if(something === undefined) 问题

问题描述

我正在为我的不和谐机器人创建索赔订单,所有订单都存储在这样的 json 文件中,{ "mUT": { "orderID": "the order's id", "userID": "A number", "guildID": "another number", "channelID": "another remember", "order": "the order", "status": "Unclaimed", "ticketChannelMessageID": "another another number" }, 但是当使用索赔命令时,它一直说订单不存在,即使我检查了 json 文件并且订单在那里我不太明白这个问题,命令根本不起作用

const colors = require("colors");

exports.id = "claim";

exports.onLoad = api => {
    api.commands.add("claim", (msg) => {
        
        let employeeRole = msg.guild.roles.get("745410836901789749");

        

        if(msg.member.roles.has(employeeRole.id)) {
            if(msg.channel.id == 746423099871985755) {
                // The ticket ID.
                let ticketID = msg.content.substring(7);
                
                fsn.readJSON("./orders.json").then((orderDB) => {
                    const order = orderDB[ticketID];

                    // If the order doesn't exist.
                    if(order === undefined) {
                        msg.reply("Couldn't find that order.");

                        return;
                    }
                    
                    if (order.status === "Unclaimed") {                        
                        // Edits the message in the tickets channel.
                        api.client.channels.get("746423099871985755").fetchMessages({
                            around: order.ticketChannelMessageID,
                            limit: 1
                        }).then(messages => {
                            const fetchedMsg = messages.first();

                            fetchedMsg.edit({embed: {
                                color: 0xFFFFFF,
                                title: api.client.users.get(order.userID).username,
                                fields: [{
                                    name: "Order Description",
                                    value: order,
                                }, {
                                    name: "Order ID",
                                    value: ticketID,
                                }, {
                                    name: "Order Status",
                                    value: "claimed",
                                }],
                                timestamp: new Date(),
                                footer: {
                                    text: "Taco Bot"
                                }
                            }}).then((m) => {
                                m = m.id;
        
                                // Update Status
                                delete orderDB[ticketID];

                                orderDB[ticketID] = {
                                    "orderID": order.orderID,
                                    "userID": order.userID,
                                    "guildID": order.guildID,
                                    "channelID": order.channelID,
                                    "order": order.order,
                                    "status": "Claimed",
                                    "ticketChannelMessageID": m,
                                    "chef": msg.author.id
                                };

                                // Writes Data to JSON.
                                fsn.writeJSON("./orders.json", orderDB, {
                                    replacer: null,
                                    spaces: 4
                                }).then(() => {
                                    // Sends a message to the cook.
                                    msg.channel.send(`You've now claimed \`${ticketID}\`.`);

                                    // Sends a message to the customer.
                                    api.client.users.get(order.userID).send(`Your order has been claimed and will be delivered soon.`);
                                }).catch((err) => {
                                    if (err) {
                                        msg.reply(`There was an error while writing to the database! Show the following message to a developer: \`\`\`${err}\`\`\``);   
                                    }
                                });
                                console.log(colors.green(`${msg.author.username} claimed order ${ticketID}.`));
                            });
                        });
                    }else if(order.status === "Claimed") {
                        if(order.chef !== msg.author.id) {
                            msg.reply(`This order is already claimed by ${api.client.users.get(order.chef).tag}`);
                        }else {
                            msg.reply("You have already claimed this order. Run `.setimage [Order ID]` to set the order's image.");
                        }
                    }else if(order.status === "Waiting") {
                        msg.reply("This order is in the waiting process. Wait a little bit, then run `.deliver [Order ID]` to deliver.");
                    }else if(order.status === "Ready") {
                        msg.reply("This order is already ready to be delivered. Run `.deliver [Order ID]` to deliver.");
                    } 
                });
            }else {
                msg.reply("Please use this command in the orders channel.");
                console.log(colors.red(`${msg.author.username} used the claim command in the wrong channel.`));
            }
        }else {
            msg.reply("You do not have access to this command.");
            console.log(colors.red(`${msg.author.username} did not have access to the claim command.`));
        }
    });
};

标签: javascriptnode.jsdiscord.js

解决方案


推荐阅读