【问题标题】:DiscordJS v14 - Command HandlerDiscordJS v14 - 命令处理程序
【发布时间】:2022-11-18 06:17:13
【问题描述】:

我正在尝试创建一个命令句柄函数,这是我的代码,但是它在控制台中给我一个错误。

有时当我去重新加载机器人时,错误不会出现,但它不会显示斜杠命令。其他时候,我会收到下面列出的错误。

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

module.exports = (client) => {
  client.handleCommands = async () => {
    const commandFolders = fs.readdirSync("./src/commands");
    for (const folder of commandFolders) {
      const commandFiles = fs
        .readdirSync(`./src/commands/${folder}`)
        .filter((file) => file.endsWith(".js"));

      const { commands, commandArray } = client;
      for (const file of commandFiles) {
        const command = require(`../../commands/${folder}/${file}`);
        commands.set(command.data.name, command);
        commandArray.push(command, command.data.toJSON());
        console.log(
          chalk.white("Command: ") +
            chalk.cyan.bold`${command.data.name} ` +
            chalk.white("has successfully loaded.")
        );
      }
    }

    const clientId = "(Client_ID)";
    const rest = new REST({ version: "10" }).setToken(
      process.env.DISCORD_DEV_TOKEN
    );

    try {
      console.log("Started refreshing application (/) commands.");

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

      console.log("Successfully reloaded application (/) commands.");
    } catch (error) {
      console.error(error);
    }
  };
};

这是错误。

DiscordAPIError[50035]: Invalid Form Body
0.name[BASE_TYPE_REQUIRED]: This field is required
    at SequentialHandler.runRequest (/home/**/Discord Bot/node_modules/@discordjs/rest/dist/index.js:659:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (/home/**/Discord Bot/node_modules/@discordjs/rest/dist/index.js:458:14)
    at async REST.request (/home/**/Discord Bot/node_modules/@discordjs/rest/dist/index.js:902:22)
    at async Client.client.handleCommands (/home/**/Discord Bot/src/functions/handlers/handleCommands.js:35:7) {
  requestBody: { files: undefined, json: [ [Object], [Object] ] },
  rawError: {
    code: 50035,
    errors: { '0': [Object] },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'PUT',
  url: 'https://discord.com/api/v10/applications/(Client_ID)/commands'
}

我试图调试它并按照 Discord.JS 网站上的文档进行操作,但这甚至没有解决问题。

我目前只有典型的 ping 命令,这是它的代码。

const { SlashCommandBuilder } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("ping")
    .setDescription("Return's the bot's ping!"),

  async execute(interaction, client) {
    const message = await interaction.deferReply({
      fetchReply: true,
    });
    const newMessage = `API Latency: ${client.ws.ping}\n Client Ping: ${
      message.createdTimestamp - interaction.createdTimestamp
    }`;
    await interaction.editReply({
      content: newMessage,
    });
  },
};

有谁能分享我犯错的地方吗?

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    原来我的问题在commandArray.push 之内。

    我把它写成

    commandArray.push(command, command.data.toJSON());
    

    需要的时候

    commandArray.push(command.data.toJSON());
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2022-09-29
      • 1970-01-01
      • 2021-08-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2020-06-09
      相关资源
      最近更新 更多