簡體   English   中英

我正在嘗試在 discord.js v13 中創建一個斜杠命令處理程序

[英]I'm trying to make a slash command handler in discord.js v13

我正在嘗試更改我的命令處理程序以使用斜杠命令,但是在嘗試打開機器人時出現 Invalid Form Body 錯誤。 我不想切換處理程序,因為這是我在所有機器人上使用的,但我也無法修復錯誤。 我正在使用 discord js v13

這是我當前的命令處理程序( index.js ):

const pComandos = fs.readdirSync('Comandos');

client.commands = new Discord.Collection();
client.description = new Discord.Collection();

for (const folder of pComandos) {
    const file = fs.readdirSync('Comandos/' + folder);

    for (const comando of file) {

        let cmd = require(`./Comandos/${folder}/${comando}`);
        commandsArray = [];

        console.log(chalk.green(`[ C ] O comando ` + chalk.bold(comando) + ` foi carregado!`))

        client.commands.set(cmd.help.name, cmd)
        if (cmd.help.description) {
            cmd.desc = true
            cmd.help.description.forEach(desc => client.commands.set(desc, cmd))
        }
        client.commands.set(cmd)
        commandsArray.push(cmd.help.name);

        if (cmd.init) cmd.init(client)

        client.on("ready", async() => {
            const server = await client.guilds.cache.get("892210305558532116")
            server.commands.set(commandsArray)
        })
    }
}

Comandos/uteis/ping.js

const discord = require("discord.js");

exports.run = async (client, message, args) => {
  
  var latency = Date.now() - message.createdTimestamp

  message.reply(`<:emoji_3:892431734203891722> | My ping is: ${client.ws.ping}.\n🗺 | Timezone: ${process.env.TZ}`);

};
exports.help = {
  name: "ping",
  description: ["p"]
};

和錯誤:

DiscordAPIError: Invalid Form Body
0.name: This field is required
    at RequestHandler.execute (C:\Users\whash\OneDrive\Documentos\optiPC-BOT2\node_modules\discord.js\src\rest\RequestHandler.js:349:13)     
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\whash\OneDrive\Documentos\optiPC-BOT2\node_modules\discord.js\src\rest\RequestHandler.js:50:14)   
    at async GuildApplicationCommandManager.set (C:\Users\whash\OneDrive\Documentos\optiPC-BOT2\node_modules\discord.js\src\managers\ApplicationCommandManager.js:146:18) {
  method: 'put',
  path: '/applications/909489088904712223/guilds/892210305558532116/commands',
  code: 50035,
  httpStatus: 400,
  requestData: {
    json: [
      {
        name: undefined,
        description: undefined,
        type: undefined,
        options: undefined,
        default_permission: undefined
      }
    ],
    files: []
  }
}

您需要使用ApplicationCommandData object。

而不是這個:

commandsArray.push(cmd.help.name)

做這個:

commandsArray.push({
  name: cmd.help.name,
  description: cmd.help.description.join("") // or whatever description you want
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM