简体   繁体   中英

Discord.js TypeError: Cannot read property 'id' of null

I was making a delivering command which sends an image url to a server however i encounter the error "TypeError: Cannot read property 'id' of null" when using the command the code for the command: (sorry for putting too much code i just don't know where is the issue because there are many lines that contain "ID")

const Discord = require('discord.js');
const fsn = require("fs-nextra");
const client = new Discord.Client();
const colors = require("colors");
module.exports = {
    name: 'deliver',
    description: 'Deliverying an order',
    args: 'true',
    usage: '<order ID>',
    aliases: ['d'],
    execute(message) {
        let employeeRole = message.guild.roles.cache.get("745410836901789749");
        var me = message.guild.members.cache.get(client.user.id);
        const args = message.content.slice(prefix.length).trim().split(/ +/g);

        if(message.member.roles.cache.has(employeeRole.id)) {
            if(message.channel.id == 746423099871985755) {
                fsn.readJSON("./orders.json").then((orderDB) => {
                    let ticketID = args[1];
                    let order = orderDB[ticketID];

                    // If the order doesn't exist.
                    if(order === undefined) {
                        message.reply(`Couldn't find order \`${args[1]}\` Try again.`);

                        return;
                    }
                    
                    // Checks status.
                    if (order.status === "Ready") {
                        // Delete ticket from database.
                        delete orderDB[ticketID];

                        // Writes data to JSON.
                        fsn.writeJSON("./orders.json", orderDB, {
                            replacer: null,
                            spaces: 4
                        }).then(() => {
                            // If bot has create instant invite permission.
                            if(client.guilds.cache.get(order.guildID).me.hasPermission("CREATE_INSTANT_INVITE")) {
                                // Send message to cook.
                                client.channels.cache.get(order.channelID).send(`${client.users.cache.get(order.userID)}, Here is your taco that you ordered. Remember you can do \`.tip [Amount]\` to give us virtual tips, and \`.feedback [Feedback]\` to give us feedback on how we did. ${order.imageURL}`);

                                message.reply(`Order has been sent to ${client.guilds.cache.get(order.guildID).name}`);
                                
                            }else {
                                // Bot delivers it's self.
                                client.channels.cache.get(order.channelID).send(`${client.users.cache.get(order.userID)}, Here is your taco that you ordered. Remember you can do \`.tip [Amount]\` to give us virtual tips, and \`.feedback [Feedback]\` to give us feedback on how we did. ${order.imageURL}`);

                                
                                
                                // Logs in console.
                                console.log(colors.green(`The bot did not have the Create Instant Invite Permissions for ${client.guilds.get(order.guildID).name}, so it delivered the popsicle itself.`));
                            }

                            
                        }).catch((err) => {
                            if (err) {
                                message.reply(`There was an error while writing to the database! Show the following message to a developer: \`\`\`${err}\`\`\``);
                            }
                        });
                    }else if(order.status === "Unclaimed") {
                        message.reply("This order hasn't been claimed yet. Run `.claim [Ticket ID]` to claim it.");
                    }else if(order.status === "Claimed") {
                        if(message.author.id === order.chef) {
                            message.reply("You haven't set an image for this order! yet Use `.setorder [Order ID]` to do it.");
                        }else {
                            message.reply(`This order hasn't been set an image yet, and only the chef of the order, ${client.users.get(order.chef).username} may set this order.`);
                        }
                    }else if(order.status === "Waiting") {
                        message.reply("This order is in the waiting process right now. Wait a little bit, then run `.deliver [Order ID] to deliver.");
                    }
                });
            }else {
                message.reply("Please use this command in the correct channel.");
                console.log(colors.red(`${message.author.username} used the claim command in the wrong channel.`));
            }
        }else {
            message.reply("You do not have access to this command.");
            console.log(colors.red(`${message.author.username} did not have access to the deliver command.`));
        }
    }
}```

You can narrow down your search to only 4 ids as the traceback itself specifies lower case id.ie

message.author.id,
message.channel.id,
employeeRole.id,
client.user.id

Console them all and whichever console throws error is the source of issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM