Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

javascript - 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.`));
        }
    }
}```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...