Deploy Commands

Hi, I'm a new user on discord.js, I would like to know if I can make it so that when the bot joins a server it creates commands in that server (I use slash commands and module.exports) as far as I know this function works calls deploy-commands, but I can't find anything on the internet. Can you help me?
18 Replies
d.js toolkit
d.js toolkit16mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
mr.energetico
mr.energetico16mo ago
discord.js : discord.js@14.9.0 node : v16.17.0
EndergamerMC
EndergamerMC16mo ago
You just have to deploy the commands globally
mr.energetico
mr.energetico16mo ago
How do you do ? Excuse my ignorance but I'm new and know almost nothing
d.js docs
d.js docs16mo ago
guide Creating Your Bot: Command registration - Global commands read more
EndergamerMC
EndergamerMC16mo ago
There is a code example or show your code where you deploy the commands please
mr.energetico
mr.energetico16mo ago
C:\Users\MATTEO\Desktop\PizzaBot\index.js:20 await rest.put( ^^^^^ SyntaxError: await is only valid in async functions and the top level bodies of modules at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1055:15) at Module._compile (node:internal/modules/cjs/loader:1090:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47
global.Discord = require("discord.js")
const fs = require('node:fs');
const path = require('node:path');
const key = require("./JSON/bot/key.json")
global.client = new Discord.Client({
intents: 32767,
partials: ["CHANNEL", "MESSAGE", "REACTION"]

})
client.commands = new Discord.Collection()


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

await rest.put(
Routes.applicationCommands(key.clientId),
{ body: command },
);
}
}



const eventsFolder = fs.readdirSync("./events")
for (const folder of eventsFolder) {
const eventsFiles = fs.readdirSync(`./events/${folder}`).filter(file => file.endsWith(".js"));
for (const file of eventsFiles) {
const event = require(`./events/${folder}/${file}`);
client.on(event.name, (...args) => event.execute(...args))
}
}

client.on("interactionCreate", interaction => {
if (!interaction.isCommand()) return

const command = client.commands.get(interaction.commandName)
if (!command) return

command.execute(interaction, client)
})


client.login(key.token)
global.Discord = require("discord.js")
const fs = require('node:fs');
const path = require('node:path');
const key = require("./JSON/bot/key.json")
global.client = new Discord.Client({
intents: 32767,
partials: ["CHANNEL", "MESSAGE", "REACTION"]

})
client.commands = new Discord.Collection()


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

await rest.put(
Routes.applicationCommands(key.clientId),
{ body: command },
);
}
}



const eventsFolder = fs.readdirSync("./events")
for (const folder of eventsFolder) {
const eventsFiles = fs.readdirSync(`./events/${folder}`).filter(file => file.endsWith(".js"));
for (const file of eventsFiles) {
const event = require(`./events/${folder}/${file}`);
client.on(event.name, (...args) => event.execute(...args))
}
}

client.on("interactionCreate", interaction => {
if (!interaction.isCommand()) return

const command = client.commands.get(interaction.commandName)
if (!command) return

command.execute(interaction, client)
})


client.login(key.token)
this is my index.js and this is the error
EndergamerMC
EndergamerMC16mo ago
hmm please read your error You’re using await in a non asynchronous function Remove the await
mr.energetico
mr.energetico16mo ago
ok C:\Users\MATTEO\Desktop\PizzaBot\index.js:20 rest.put( ^ ReferenceError: rest is not defined at Object.<anonymous> (C:\Users\MATTEO\Desktop\PizzaBot\index.js:20:9) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 now i have this error it says rest is not defined, what library should i use to define it? @endergamermc
EndergamerMC
EndergamerMC16mo ago
You need to define the REST const rest = new REST({ version: '10' }).setToken(token);
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs16mo ago
guide Creating Your Bot: Command registration - Guild commands read more
EndergamerMC
EndergamerMC16mo ago
There is a code example
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
mr.energetico
mr.energetico16mo ago
so I should make another file named deploy.js and put the global commands code there?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs16mo ago
guide Creating Your Bot: Command registration - Guild commands read more
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View