DiscordAPIError: Unknown application command

DiscordAPIError: Unknown application command

当我尝试执行斜杠命令时,它崩溃并出现 404 错误。 我已经尝试删除所有命令,但没有用。

这是我正在使用的代码


const commands = [
    {
  name: 'ping',
  description: 'Replies with Pong!'
    }];

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

(async () => {
  try {
    console.log('Reloading slash commands');

    await rest.put(
      Routes.applicationGuildCommands("client_id", "server_id"),
      { body: commands },
    );
    console.log("Reloaded slash commands");
  } catch (error) {
    console.error(error);
  }
})();

这是错误

node_modules/discord.js/src/rest/RequestHandler.js:350
      throw new DiscordAPIError(data, res.status, request);
            ^
DiscordAPIError: Unknown application command
    at RequestHandler.execute (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
    at async GuildApplicationCommandManager.fetch (/home/container/node_modules/discord.js/src/managers/ApplicationCommandManager.js:93:23)
    at async Client.<anonymous> (/home/container/index.js:101:17) {
  method: 'get',
  path: '/applications/client_id/guilds/guild_id/commands/command_id',
  code: 10063,
  httpStatus: 404,
  requestData: { json: undefined, files: [] }
}

如果您想使用较新的 Discord.js Rest 库注册您的 Slash 命令,您还必须使用构建器库。您也没有将 JSON 传递给传递对象数组的正文。

来自Discord.js Guide on Making Slash Commands

const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

const commands = [
    new SlashCommandBuilder().setName('ping').setDescription('Replies with pong!'),
    new SlashCommandBuilder().setName('server').setDescription('Replies with server info!'),
    new SlashCommandBuilder().setName('user').setDescription('Replies with user info!'),
]
.map(command => command.toJSON());

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

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(() => console.log('Successfully registered application commands.'))
    .catch(console.error);