get application commands

As part of the js file that registers my commands, I want to get the existing commands before registering the new one. This is for both global and guild commands. There is no connection to the client in this file and I'm using @discordjs/rest.
const { Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
const { REST } = require('@discordjs/rest');
const token = process.env.DISCORD_TOKEN;
const clientId = process.env.clientId;
const guildId = process.env.devGuildId;

const rest = new REST({ version: '10' }).setToken(token);

let curCommands, curDevCommands, newCommands, newDevCommands, commandsPath, commandFiles, file, command;

newCommands = newDevCommands = curCommands = curDevCommands = [];


// for global commands

// get current commands
rest.get(Routes.applicationCommands(clientId))
.then(() =>
// Not sure what goes here
)
.catch(logger.error);
curCommands =

commandsPath = path.join(__dirname, './commands');
commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js') && !file.startsWith('._'));

for (file of commandFiles) {
command = require(`./commands/${file}`);
newCommands.push(command.data.toJSON());
}

logger.debug("Global commands:" + newCommands);
rest.put(Routes.applicationCommands(clientId), { body: newCommands })
.then(() => logger.info('Successfully registered ' + newCommands.length + ' global commands.'))
.catch(logger.error);

for (let commandi of newCommands) {
console.group(commandi.name);
console.log(commandi.description);
if (commandi.options) {
for (let option of commandi.options) {
console.log(option.name + ": " + option.description);
}
}
console.groupEnd();
}

// SAME FOR Guild Commands
const { Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
const { REST } = require('@discordjs/rest');
const token = process.env.DISCORD_TOKEN;
const clientId = process.env.clientId;
const guildId = process.env.devGuildId;

const rest = new REST({ version: '10' }).setToken(token);

let curCommands, curDevCommands, newCommands, newDevCommands, commandsPath, commandFiles, file, command;

newCommands = newDevCommands = curCommands = curDevCommands = [];


// for global commands

// get current commands
rest.get(Routes.applicationCommands(clientId))
.then(() =>
// Not sure what goes here
)
.catch(logger.error);
curCommands =

commandsPath = path.join(__dirname, './commands');
commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js') && !file.startsWith('._'));

for (file of commandFiles) {
command = require(`./commands/${file}`);
newCommands.push(command.data.toJSON());
}

logger.debug("Global commands:" + newCommands);
rest.put(Routes.applicationCommands(clientId), { body: newCommands })
.then(() => logger.info('Successfully registered ' + newCommands.length + ' global commands.'))
.catch(logger.error);

for (let commandi of newCommands) {
console.group(commandi.name);
console.log(commandi.description);
if (commandi.options) {
for (let option of commandi.options) {
console.log(option.name + ": " + option.description);
}
}
console.groupEnd();
}

// SAME FOR Guild Commands
9 Replies
d.js toolkit
d.js toolkit12mo 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!
sproj003 ♿
sproj003 ♿OP12mo ago
djs 1.14.1 node v18.16.1
John
John12mo ago
wait so are you trying to get global commands or both?
sproj003 ♿
sproj003 ♿OP12mo ago
both
entropy
entropy12mo ago
Routes is just a wrapper around all the discord api endpoints. If you hover over the applicationsCommands method, it will show you what endpoint it wraps for each HTTP method. With that you can read the Discord api documentation for those routes and figure what it returns. Guild Specific: https://discord.com/developers/docs/interactions/application-commands#get-guild-application-commands Global: https://discord.com/developers/docs/interactions/application-commands#get-global-application-command
sproj003 ♿
sproj003 ♿OP12mo ago
Purely so that devs can check the permissions are correct
monbrey
monbrey12mo ago
But it shouldn't be up to the devs to set permissions, that's for server admins to do
sproj003 ♿
sproj003 ♿OP12mo ago
The default command permissions new SlashCommandBuilder.setDefaultMemberPermissions() (If I got the code wrong, it's cause I'm not looking at the code right now)
monbrey
monbrey12mo ago
Yeah fair enough that
Want results from more Discord servers?
Add your server