731018913097449533
731018913097449533
DIAdiscord.js - Imagine an app
Created by 731018913097449533 on 6/30/2024 in #djs-questions
Application commands are not updated in chat, but in "Manage Integration" are up-to-date.
Here is my code that updates commands: When the bot starts to run, it loads all commands
client.commands = new Map();
const commandPath = './commands';
const commandFiles = fs.readdirSync(commandPath).filter((file) => file.endsWith('.mjs'));
for (const file of commandFiles) {
const filePath = './' + path.join(commandPath, file);
let command = await import(filePath);
command = command.default;

if (command.ignore) continue;
if (command.data && command.execute) {
client.commands.set(command.data.name, command);
} else {
console.error(`Error: ${filePath} command is missing a required property "data" or "execute"`);
}
}
client.commands = new Map();
const commandPath = './commands';
const commandFiles = fs.readdirSync(commandPath).filter((file) => file.endsWith('.mjs'));
for (const file of commandFiles) {
const filePath = './' + path.join(commandPath, file);
let command = await import(filePath);
command = command.default;

if (command.ignore) continue;
if (command.data && command.execute) {
client.commands.set(command.data.name, command);
} else {
console.error(`Error: ${filePath} command is missing a required property "data" or "execute"`);
}
}
After the bot is ready, it sets client.application.commands
import { Client, Events } from 'discord.js';

export default {
once: true,
name: Events.ClientReady,

/**
* execute
* @param {Client} client
*/
async execute(client) {
console.info('Client is Ready');

// load commands
const commands = Array.from(client.commands.values()).map(command => command.data.toJSON());
client.application.commands.set(commands)
.catch(console.error);

const commandNames = commands.map(command => command.name);
console.info(`Commands loaded: ${commandNames.join(', ')}`);
},
};
import { Client, Events } from 'discord.js';

export default {
once: true,
name: Events.ClientReady,

/**
* execute
* @param {Client} client
*/
async execute(client) {
console.info('Client is Ready');

// load commands
const commands = Array.from(client.commands.values()).map(command => command.data.toJSON());
client.application.commands.set(commands)
.catch(console.error);

const commandNames = commands.map(command => command.name);
console.info(`Commands loaded: ${commandNames.join(', ')}`);
},
};
Here's an example of my command file:
import { SlashCommandBuilder, CommandInteraction } from 'discord.js';

export default {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Reply with ms'),

/**
* execute
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
await interaction.reply(`🏓 Ping! ${interaction.client.ws.ping}ms`);
},
};
import { SlashCommandBuilder, CommandInteraction } from 'discord.js';

export default {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Reply with ms'),

/**
* execute
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
await interaction.reply(`🏓 Ping! ${interaction.client.ws.ping}ms`);
},
};
Discord.js Version: 14.7.1 Nodejs Version: 22.2.0
26 replies