NodeJS Discord.js V14 Slash命令不能全局注册?

5jdjgkvh  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(144)

我使用discord.js v14,我使用这个处理程序全局部署命令文件夹中的所有文件夹,除了带有私有命令的名为“dev”的文件夹,因此所有命令都部署在“测试服务器”中,但当我邀请bot到另一个服务器时,没有一个“全局”命令会注册自己。下面是命令处理程序代码:

const { REST } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('fs');
const path = require('path');

const commands = [];

// Function to register global commands
const registerGlobalCommands = (commandFolders) => {
  for (const folder of commandFolders) {
    const commandsPath = path.join(__dirname, 'commands', folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

    for (const file of commandFiles) {
      const filePath = path.join(commandsPath, file);
      const command = require(filePath);

      if ('data' in command && 'execute' in command) {
        commands.push(command.data.toJSON());
      } else {
        console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
      }
    }
  }
};

// Function to register guild commands for a specific guild
const registerGuildCommands = (commandFolder) => {
  const commandsPath = path.join(__dirname, 'commands', commandFolder);
  const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

  for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);

    if ('data' in command && 'execute' in command) {
      commands.push({
        ...command.data.toJSON(),
        guilds: [guildId] // Set the specific guild ID for guild commands
      });
    } else {
      console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
    }
  }
};

// Grab all the command folders from the commands directory
const commandFolders = fs.readdirSync(path.join(__dirname, 'commands'));

// Register global commands for utility, fun, and main categories
registerGlobalCommands(commandFolders.filter(folder => folder !== 'dev'));

// Register guild commands for employee category in a specific guild
registerGuildCommands('dev');

// Construct and prepare an instance of the REST module
const rest = new REST({ version: '9' }).setToken(token);

// Deploy the commands
(async () => {
  try {
    console.log(`Started refreshing ${commands.length} application commands.`);

    // Deploy all the commands
    await rest.put(
      '/applications/' + clientId + '/guilds/' + guildId + '/commands',
      { body: commands }
    );

    console.log(`Successfully deployed ${commands.length} application commands.`);
  } catch (error) {
    console.error(`Failed to deploy application commands: ${error}`);
  }
})();
bakd9h0s

bakd9h0s1#

我想你可以按照教程:https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands
请注意此导入:const { REST, Routes } = require('discord.js');

// Deploy the commands
(async () => {
  try {
    console.log(`Started refreshing ${commands.length} application commands.`);

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

    console.log(`Successfully deployed ${commands.length} application commands.`);
  } catch (error) {
    console.error(`Failed to deploy application commands: ${error}`);
  }
})();

相关问题