선하루
선하루
DIAdiscord.js - Imagine an app
Created by 선하루 on 3/30/2025 in #djs-questions
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 };
5 replies