Discord.js Per Server Prefix Error "TypeError: Cannot read property 'ID' of null"

Hi i was making a per server prefix thing for my discord.js v12 bot using quick.db everything was going well but when using a command it throws this error

C:\Users\Aoshey\Desktop\Taco Bot V2\tacobot.js:50
        let prefix = db.get(`prefix_${message.guild.ID}`);
                                                    ^

TypeError: Cannot read property 'ID' of null

i don’t quite understand since i’m new to coding so sorry if it was something stupid
the main bot file’s code:

client.on('message', message => {
	let prefix = db.get(`prefix_${message.guild.ID}`);
	if(prefix === null) prefix = default_prefix;
	if (!message.content.startsWith(prefix) || message.author.bot) return;
	

	const args = message.content.slice(prefix.length).trim().split(/ +/);
	const commandName = args.shift().toLowerCase();

	const command = client.commands.get(commandName)
		|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

	if (!command) return;

	if (command.guildOnly && message.channel.type === 'dm') {
		return message.reply('I can\'t execute that command inside DMs!');
	}

	if (command.args && !args.length) {
		let reply = `${message.author}, wrong usage`;

		if (command.usage) {
			reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
		}

		return message.channel.send(reply);
	}



	try {
		command.execute(message, args);
	} catch (error) {
		console.error(error);
		message.reply('there was an error trying to execute that command! Please tell Aro#1221 about this.');
	}
});

EDIT: i noticed that the error only occurs when its used on only 1 command meanwhile other commands work well
the code for the command which causes the error i think?

const fsn = require("fs-nextra");
const colors = require("colors");



module.exports = {
    name: 'deleteorder',
    description: 'deleting/denying an order.',
    args: 'true',
    usage: '<order id> <reason>',
    aliases: ['od', 'delorder', 'do'],
    execute(message, args) {
        

        if (message.member.roles.cache.find(r => r.id === '745410836901789749')) {
            let ticketID = args[0];
            let reason = args[1];

            fsn.readJSON("./orders.json").then((orderDB) => {
                const order = orderDB[ticketID];

                if(order === undefined) {
                    message.reply(`Couldn't find order \`${args[0]}\` Try again.`);

                    return;
                }

                if(reason === "") {
                    reason = "None Provided";
                }

                delete orderDB[ticketID];

                fsn.writeJSON("./orders.json", orderDB, {
                    replacer: null,
                    spaces: 4
                });

                message.reply(`You deleted order \`${args[0]}\` with reason \`${args[1]}\``)
                // Sends a message to the customer.
                let customer = message.guild.members.cache.find(m => m.id === order.userID)
                //message.users.cache.get(order.userID).send(`Sorry, but your order was cancelled by **${message.author.username}** due to the following reason: \`${reason}\`.`);
                customer.send(`Sorry, but your order was cancelled by **${message.author.username}** due to the following reason: \`${reason}\`.`)

                // Logs in console.
                console.log(colors.red(`${message.author.username} deleted order ${order.orderID}.`));


            });

        } else {
            message.reply("You aren't an employee.");
            console.log(colors.red(`${message.author.username} did not have access to the delorder command.`));
        }
    }
}

Are you using this command in dms? message.guild will only exist if it was sent in a server. I’ve got no idea why other commands are working though.

no i’m not i was using it in a guild

I have same exact problem please help

Did you capitalize ID also? It is supposed to be lowercase.

Meanwhile you surely didn’t get “exactly” the same error under the same circumstances right?

The error tells you exactly which file and line and what the error is

ID should be lower case but that is not the error it’s saying it can get ID from null which means message.guild = null is this command being used in dms? and have you passed / user the message variable properly?

1 Like

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