client dereference error

After I've constructed the index code like this, I try to load it from a slashcommand file in the commands folder, but I get a circular reference error. What can I do to fix this?
const { Client, REST, Routes, Collection} = require('discord.js');

const fs = require('fs');

const config = require('./config.json');

const client = new Client({ intents: [32767] });
const token = config.token;

const commands = [];
const commandsPath = fs.readdirSync('./commands')
client.commands = new Collection();

client.once('ready', async () => {
console.log('hi')
}
)

client.on('interactionCreate', async interaction => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);

if (!command) return console.log(`커맨드 없음!: ${interaction.commandName}`);

try { await command.execute(interaction) }
catch (err) { console.log(err) }
} else if (interaction.isButton() || interaction.isStringSelectMenu()) {
const event = require(`./interactions/${interaction.message.interaction.commandName}/${interaction.customId}`)
if (interaction.componentType === 2 && event.type === "button") {
try { await event.execute(interaction) }
catch (err) { console.log(err) }
} else if (interaction.componentType === 3 && event.type === "selectMenu") {
try { await event.execute(interaction) }
catch (err) { console.log(err) }
}
}
})

for (const file of commandsPath) {
if (!file.endsWith('.js')) continue;

const handler = require(`./commands/${file}`)

if (!handler.data || !handler.execute) continue;

commands.push(handler.data.toJSON());
client.commands.set(handler.data.name, handler);
}

const rest = new REST().setToken(token);

(async () => {
try {
const data = await rest.put(
Routes.applicationGuildCommands("1259900443073576970", "1050934994861821985"),
{ body: commands }
)

console.log(`${data.length}개의 명령어를 등록했습니다.`)
} catch (err) {
console.log(err)
}
})();

client.login(token);

module.exports = { client };
const { Client, REST, Routes, Collection} = require('discord.js');

const fs = require('fs');

const config = require('./config.json');

const client = new Client({ intents: [32767] });
const token = config.token;

const commands = [];
const commandsPath = fs.readdirSync('./commands')
client.commands = new Collection();

client.once('ready', async () => {
console.log('hi')
}
)

client.on('interactionCreate', async interaction => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);

if (!command) return console.log(`커맨드 없음!: ${interaction.commandName}`);

try { await command.execute(interaction) }
catch (err) { console.log(err) }
} else if (interaction.isButton() || interaction.isStringSelectMenu()) {
const event = require(`./interactions/${interaction.message.interaction.commandName}/${interaction.customId}`)
if (interaction.componentType === 2 && event.type === "button") {
try { await event.execute(interaction) }
catch (err) { console.log(err) }
} else if (interaction.componentType === 3 && event.type === "selectMenu") {
try { await event.execute(interaction) }
catch (err) { console.log(err) }
}
}
})

for (const file of commandsPath) {
if (!file.endsWith('.js')) continue;

const handler = require(`./commands/${file}`)

if (!handler.data || !handler.execute) continue;

commands.push(handler.data.toJSON());
client.commands.set(handler.data.name, handler);
}

const rest = new REST().setToken(token);

(async () => {
try {
const data = await rest.put(
Routes.applicationGuildCommands("1259900443073576970", "1050934994861821985"),
{ body: commands }
)

console.log(`${data.length}개의 명령어를 등록했습니다.`)
} catch (err) {
console.log(err)
}
})();

client.login(token);

module.exports = { client };
3 Replies
d.js toolkit
d.js toolkit3d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
TÆMBØ
TÆMBØ3d ago
You shouldn't be exporting your client at all for this very reason. What are you doing in that command file that requires you to have the client? You should ideally just be using interaction.client
선하루
선하루OP3d ago
I want to set the embed title to client.user.username in a command that outputs information about the bot. Would using interaction.client work? Resolved, thank you!

Did you find this page helpful?