TypeError: Cannot read property 'get' of undefined

Code :

let prefix = client.guildSettings.get(message.guild.id,"prefix")

Error :

TypeError: Cannot read property ‘get’ of undefined

Can someone help?

I am using enmap…

Please help :frowning:

:angry:

please

print("please read and solve")

Please be patient and calm down. Nobody wants to help you when you are being rude.

5 Likes

Sorry :frowning:

Can you help though? :slight_smile:

I don’t have the answer (as I don’t really make Discord Bots) , I’ll ask somebody else for you though.

https://discordjs.guide/additional-info/changes-in-v12.html#clientuserguildsettings

So what should I do instead?

I’m not sure. Can I ask what the function is supposed to do? Why do you need it?

2 Likes

I need it to define the custom prefix. I’m using Enmap for it.

Hi! Have you defined client.guildSettings yourself? Is it anywhere else in your code. If so, can you send the code?

1 Like

Nope, i didn’t define it.

my code is here :

const Discord = require('discord.js')
const fs = require('fs');
const client = new Discord.Client()
const { token } = require('./config.json');
const enmap = require('enmap')

const defaultsettings = require("./util/defaultsettings")

const config = require("./config.json")




// create a new Discord client
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
	const command = require(`./commands/${file}`);
	client.commands.set(command.name, command);
}


client.once('ready', () => {
	console.log('Ready!');
	client.user.setActivity('USERS TYPE -help', { type: 'WATCHING' });
});
client.on("guildCreate",(guild)=>{
    createserver(guild.id, defaultsettings)
})

client.on("guildDelete",(guild)=>{
    client.guildSettings.delete(guild.id)
})

client.on('message', message => {
	let prefix = client.guildSettings.get(message.guild.id,prefix)
	if (!message.content.startsWith(prefix) || message.author.client) return;

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

	
try {
	if (command.guildOnly && message.channel.type === 'dm') {
		return message.reply('I can\'t execute that command inside DMs!');
	}
	command.execute(message, args);
} catch (error) {
	console.log(error);
	message.reply('there was an error trying to execute that command!');
}
});
process.on('unhandledRejection', error => {
	console.error('Unhandled promise rejection:', error);
});
// login to Discord with your app's token
client.login(token);

Aha! I get you now!
Looks like you need to use this instead:

let prefix = client.settings.get(message.guild.id,"prefix")

Hope this helps and Happy Glitching :slight_smile: :glitch:
Eddie

2 Likes

It still says a error. The same eror

Can I see the /util/defaultsettings file?

:frowning:

I don’t know…

const defaultsettings = {
    prefix: "!",
    deletemessage: true,
    owners: []
}

module.exports = defaultsettings

there gave you the code

:slight_smile:

Hi @DaRealMiniToon
Have you got code like this somewhere?

client.settings = new enmap({
  name: "settings",
  fetchAll: false,
  autoFetch: true,
  cloneLevel: 'deep'
});

Nope, but where should I add it?

I

I would put it after you define enmap
(This line: const enmap = require('enmap'))

2 Likes

Thanks! That fixed old one. New error! :frowning:

let prefix = client.settings.get(message.guild.id,prefix)

ReferenceError: Cannot access ‘prefix’ before initialization
@EddiesTech

Could you also help with this one? <3

In enmap the path needs to be a string. Your passing a variable you haven’t defined yet.
Try:

let prefix = client.settings.get(message.guild.id, 'prefix')
2 Likes

:open_mouth: Thanks lol! Fixed.

if (!this.has(key)) throw new Err(`The key "${key}" does not exist in the enmap "${this.name}"`, 'EnmapPathError');
                        ^

CustomError [EnmapPathError]: The key "755528648152580167" does not exist in the enmap "settings"

I’m not really familiar with the “key”

:frowning:

@WilsonTheWolf

Add this:

const guildConf = client.settings.ensure(message.guild.id, defaultsettings);
2 Likes

Ofc, but now it just don’t run any code.

:frowning:

now what

const Discord = require('discord.js')
const fs = require('fs');
const client = new Discord.Client()
const { token } = require('./config.json');
const enmap = require('enmap')

const defaultsettings = require("./util/defaultsettings")
const defaultPrefix = "-"
const config = require("./config.json")


client.settings = new enmap({
	name: "settings",
	fetchAll: false,
	autoFetch: true,
	cloneLevel: 'deep'
  });

// create a new Discord client
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
	const command = require(`./commands/${file}`);
	client.commands.set(command.name, command);
}


client.once('ready', () => {
	console.log('Ready!');
	client.user.setActivity('USERS TYPE -help', { type: 'WATCHING' });
});
function createserver(guildid, object){
    client.settings.ensure(guildid, object)
}
client.on("guildCreate",(guild)=>{
    createserver(guild.id, defaultsettings)
})

client.on("guildDelete",(guild)=>{
    client.settings.delete(guild.id)	
})

client.on('message', message => {
	
	const guildConf = client.settings.ensure(message.guild.id, defaultsettings);
	let prefix = client.settings.get(message.guild.id,'prefix') 
	if (!prefix){
		prefix = defaultPrefix
		message.reply("no prefix configured")
	}
	if (!message.content.startsWith(prefix) || message.author.client) return;

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

	console.log(commandName)
try {
	if (command.guildOnly && message.channel.type === 'dm') {
		return message.reply('I can\'t execute that command inside DMs!');
	}
	command.execute(message, args);
} catch (error) {
	console.log(error);
	message.reply('there was an error trying to execute that command!');
}
});
process.on('unhandledRejection', error => {
	console.error('Unhandled promise rejection:', error);
});
// login to Discord with your app's token
client.login(token);

:frowning:

const guildConf = client.settings.ensure(message.guild.id, defaultsettings);
^

TypeError: Cannot read property ‘id’ of null

so its wrong :frowning:

const guildConf = client.settings.ensure(message.guild.id, defaultsettings);
^

TypeError: Cannot read property ‘id’ of null

I’m using discord.js v12. Not discord.js v11