Commands dont load

Whenever I create a new command, I have to remove my bot from the server and add it back. How can I fix this?
19 Replies
d.js toolkit
d.js toolkit5mo 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!
Question
Question5mo ago
Do you have 'Use Application Command' Permission?
Manuel
ManuelOP5mo ago
Yes! 🙂
Question
Question5mo ago
Did you use rest?
Manuel
ManuelOP5mo ago
No description
Manuel
ManuelOP5mo ago
Yes!
Question
Question5mo ago
is there any error? If no, send the code
Manuel
ManuelOP5mo ago
Basically, there is no error. What happens is: I create a command and restart the bot. The command does not appear in the bot's command list.
Question
Question5mo ago
have you loaded the commands path correctly? If yes, send the code again
Manuel
ManuelOP5mo ago
const fs = require("node:fs");
const chalk = require("chalk");
const { Collection } = require("discord.js");

const loadSlashCommands = async function (client) {
client.slash = new Collection()
let slash = []

const commandFolders = fs.readdirSync("./Commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./Commands/${folder}`)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`../Commands/${folder}/${file}`);

if (command.data.name) {
client.slash.set(command.data.name, command);
slash.push(command.data.toJSON())
console.log(chalk.bgBlueBright.black(` ✔️ => slashcommand ${file} carregado.`));
} else {
console.log(chalk.bgRedBright.black(` ❌ => slashcommand ${file} falta um help.name ou help.name não está na string.`));
continue;
}
}
}

client.on("ready", async() => {
await client.application.commands.set(slash)
})
}
module.exports = { loadSlashCommands };
const fs = require("node:fs");
const chalk = require("chalk");
const { Collection } = require("discord.js");

const loadSlashCommands = async function (client) {
client.slash = new Collection()
let slash = []

const commandFolders = fs.readdirSync("./Commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./Commands/${folder}`)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`../Commands/${folder}/${file}`);

if (command.data.name) {
client.slash.set(command.data.name, command);
slash.push(command.data.toJSON())
console.log(chalk.bgBlueBright.black(` ✔️ => slashcommand ${file} carregado.`));
} else {
console.log(chalk.bgRedBright.black(` ❌ => slashcommand ${file} falta um help.name ou help.name não está na string.`));
continue;
}
}
}

client.on("ready", async() => {
await client.application.commands.set(slash)
})
}
module.exports = { loadSlashCommands };
Question
Question5mo ago
Probably path issues
Manuel
ManuelOP5mo ago
but they are loaded without errors
No description
Question
Question5mo ago
Send slash code
Manuel
ManuelOP5mo ago
slash code? an example?
Question
Question5mo ago
do you know how to code?
d.js docs
d.js docs5mo ago
guide suggestion for @Manuel: :guide: Creating Your Bot: Registering slash commands read more :guide: Creating Your Bot: Command handling read more :guide: Creating Your Bot: Event handling read more
Manuel
ManuelOP5mo ago
yes. @jö 🌈 and @Question
const fs = require("node:fs");
const chalk = require("chalk");
const { Collection } = require("discord.js");

const loadSlashCommands = async function (client) {
client.slash = new Collection()
let slash = []

const commandFolders = fs.readdirSync("./Commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./Commands/${folder}`)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`../Commands/${folder}/${file}`);

if (command.data.name) {
client.slash.set(command.data.name, command);
slash.push(command.data.toJSON())
console.log(chalk.bgBlueBright.black(` ✔️ => slashcommand ${file} carregado.`));
} else {
console.log(chalk.bgRedBright.black(` ❌ => slashcommand ${file} falta um help.name ou help.name não está na string.`));
continue;
}
}
}

client.on("ready", async() => {
await client.application.commands.set(slash)
})
}
module.exports = { loadSlashCommands };
const fs = require("node:fs");
const chalk = require("chalk");
const { Collection } = require("discord.js");

const loadSlashCommands = async function (client) {
client.slash = new Collection()
let slash = []

const commandFolders = fs.readdirSync("./Commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./Commands/${folder}`)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`../Commands/${folder}/${file}`);

if (command.data.name) {
client.slash.set(command.data.name, command);
slash.push(command.data.toJSON())
console.log(chalk.bgBlueBright.black(` ✔️ => slashcommand ${file} carregado.`));
} else {
console.log(chalk.bgRedBright.black(` ❌ => slashcommand ${file} falta um help.name ou help.name não está na string.`));
continue;
}
}
}

client.on("ready", async() => {
await client.application.commands.set(slash)
})
}
module.exports = { loadSlashCommands };
Command handler ^
const ascii = require("ascii-table");
const fs = require("node:fs");
const table = new ascii().setHeading("Eventos", "Tipo", "Estado");
const chalk = require("chalk")

const loadEvents = async function (client, database) {
const eventFolders = fs.readdirSync("./Events");
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(`./Events/${folder}`)
.filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
const event = require(`../Events/${folder}/${file}`);

if (event.name) {
console.log(chalk.bgBlueBright.black(` ✔️ => Evento ${file} carregado.`));
table.addRow(file, folder, "🟩");
} else {
console.log(chalk.bgRedBright.black(` ❌ => Evento ${file} falta um help.name ou help.name não está na string.`));
continue;
}

if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client, database));
} else {
client.on(event.name, (...args) => event.execute(...args, client, database));
}

}
}
}



module.exports = { loadEvents }
const ascii = require("ascii-table");
const fs = require("node:fs");
const table = new ascii().setHeading("Eventos", "Tipo", "Estado");
const chalk = require("chalk")

const loadEvents = async function (client, database) {
const eventFolders = fs.readdirSync("./Events");
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(`./Events/${folder}`)
.filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
const event = require(`../Events/${folder}/${file}`);

if (event.name) {
console.log(chalk.bgBlueBright.black(` ✔️ => Evento ${file} carregado.`));
table.addRow(file, folder, "🟩");
} else {
console.log(chalk.bgRedBright.black(` ❌ => Evento ${file} falta um help.name ou help.name não está na string.`));
continue;
}

if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client, database));
} else {
client.on(event.name, (...args) => event.execute(...args, client, database));
}

}
}
}



module.exports = { loadEvents }
Event handler ^
Question
Question5mo ago
send the content of slash command file
treble/luna
treble/luna5mo ago
you shouldn't deploy your commands every time your bot starts. Use what jo linked and run that only when you add or edit commands. And reload your discord client to see the new commands appeae

Did you find this page helpful?