Discord.js "TypeError: Cannot read property 'send' of undefined"

so i want to give more details about what i’m doing to make it easier to understand: I’m making a virtual food delivery bot using Discord.js V12 there is an order command and an order to claim the order and another command to deliver the order, the issue happens when using the deliver command it seems that the issue has to do with other commands as well but i’m not sure when using the “deliver” command this error comes up “TypeError: Cannot read property ‘send’ of undefined”
the line which mainly gives the error is this:

 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. ${args[2]}`);

that line is from the “delivery” command and the full code for it is here:

const { prefix } = require('../config.json');
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> <imageURL>',
    aliases: ['d'],
    execute(message) {
        
        
        const args = message.content.slice(prefix.length).trim().split(/ +/g);
		
		if (message.member.roles.cache.find(r => r.id === '745410836901789749')) {
			if(message.guild.channels.cache.find(r => r.id === '746423099871985755')) {
				fsn.readJSON("./orders.json").then((orderDB) => {
                    let ticketID = args[1];
                    let imageURL = args[2];
					let order = orderDB[ticketID];

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

						return;
                    }
                    
                    if(imageURL === undefined) {
                        message.reply('Correct usage: \`.deliver (order ID) (ImageURL)\`');

                        return;
                    }
                    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. ${args[2]}`);
					// 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(() => {
							message.reply(`Order \`${args[1]}\` Has been sent.`)
							

							console.log(`Order ${args[1]} has been sent.`)
				
			
							
						}).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.`));
		}
    }
}

The command gets information from the “orders.json” file which stores orders like this

{
    "VLC": {
        "orderID": "VLC",
        "userID": "734532125021307001",
        "guildID": "745409671430668389",
        "channelID": "746423099871985755",
        "order": "a",
        "status": "Ready",
        "ticketChannelMessageID": "not set",
        "chef": "734532125021307001"
    }
}

sorry for including so much details but this error is stopping all my bot from doing its main feature
if it would be helpful to include the “order” and “claim” commands’ code let me know and ill edit the post

same error but different issue that didn’t actually help me

i added console.log(order.channelID) and console.log(order.userID) at the start of the code and it actually logged the correct ids for these so why doesn’t it want it to send message to there (saying that they are undefined)?

i just replaced

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. ${args[2]}`);

with

const customerchannel = message.guild.channels.cache.find(c => c.id === order.channelID)
                    customerchannel.send(`<@${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. ${args[2]}`);

and it worked pretty stupid ik

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.