javascript DiscordAPIError:表单体11.options[0]无效:字段“type”是确定模型类型所必需的

ubof19bj  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(81)

尝试使用SlashCommandBuilders添加子命令时,我得到此错误。

Full Error(very long):

throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Invalid Form Body
11.options[0]: Field "type" is required to determine the model type.
    at RequestHandler.execute (C:\Users\stixy\Documents\BD-1\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RequestHandler.push (C:\Users\stixy\Documents\BD-1\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async GuildApplicationCommandManager.set (C:\Users\stixy\Documents\BD-1\node_modules\discord.js\src\managers\ApplicationCommandManager.js:162:18)
    at async Client.<anonymous> (C:\Users\stixy\Documents\BD-1\handler\index.js:46:9) {
  method: 'put',
  path: '/applications/1119579539610284062/guilds/975834098549522462/commands',
  code: 50035,
  httpStatus: 400,
  requestData: {
    json: [
      {
        name: 'ban',
        name_localizations: undefined,
        description: 'Ban anybody in the server',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 6,
            name: 'user',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The user you want to ban',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          },
          {
            type: 3,
            name: 'reason',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The reason you want to ban someone',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'echo',
        name_localizations: undefined,
        description: 'echos your message!',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 3,
            name: 'message',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'the message you want to echo',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          },
          {
            type: 6,
            name: 'user',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'a user you want to send the message to(not required)',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'kick',
        name_localizations: undefined,
        description: 'Kick anybody in the server',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 6,
            name: 'user',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The user you want to kick',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          },
          {
            type: 3,
            name: 'reason',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The reason you want to kick someone',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'purge',
        name_localizations: undefined,
        description: 'Purge messages in the channel you are currently in',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 4,
            name: 'amount',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'How many messages you want to purge.',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'softban',
        name_localizations: undefined,
        description: 'Ban and immediately unban a user to delete their messages and kick them',    
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 6,
            name: 'user',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'Who you want to softban',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          },
          {
            type: 3,
            name: 'reason',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The reason you want to ban the user',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'timeout',
        name_localizations: undefined,
        description: 'Timeout anybody in the server',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 6,
            name: 'user',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The user you want to timeout',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          },
          {
            type: 3,
            name: 'length',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The amount of time you want to timeout someone.',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          },
          {
            type: 3,
            name: 'reason',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The reason you want to timeout someone',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'unban',
        name_localizations: undefined,
        description: 'Unban a member this is currently banned',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 3,
            name: 'userid',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'The user you want to unban',
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'luke',
        name_localizations: undefined,
        description: 'Get informaton for Luke Skywalker.',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 5,
            name: 'ephemeral',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'Make the command only visibale to you.',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'ping',
        name_localizations: undefined,
        description: 'returns websocket ping',
        description_localizations: undefined,
        type: 1,
        options: undefined,
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'spam',
        name_localizations: undefined,
        description: 'Spam Lxsiz to fucking hell',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: 4,
            name: 'value',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'How many times you want to spam lxsiz because the cunt isnt answering',  
            description_localizations: undefined,
            description_localized: undefined,
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'avatar',
        name_localizations: undefined,
        description: undefined,
        description_localizations: undefined,
        type: 2,
        options: undefined,
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'user',
        name_localizations: undefined,
        description: 'User category',
        description_localizations: undefined,
        type: undefined,
        options: [
          {
            type: undefined,
            name: 'info',
            name_localizations: undefined,
            name_localized: undefined,
            description: 'idek',
            description_localizations: undefined,
            description_localized: undefined,
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: [],
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined,
            min_length: undefined,
            max_length: undefined
          }
        ],
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      },
      {
        name: 'whois',
        name_localizations: undefined,
        description: undefined,
        description_localizations: undefined,
        type: 2,
        options: undefined,
        default_permission: undefined,
        default_member_permissions: undefined,
        dm_permission: undefined
      }
    ],
    files: []
  }
}

Node.js v18.16.0

我真的不知道为什么我得到这个错误根据教程我做的一切权利。我知道类型是未定义的,这就是为什么我得到一个错误,但我不知道为什么类型是未定义的,因为所有的youtubers,甚至文档这样做,就像我有它在我的命令设置。是的,我正在使用discord.jsv13,除了我太懒而不切换到v14之外,没有特别的原因。

命令:

const { SlashCommandBuilder, SlashCommandSubcommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");

module.exports = {
    ...new SlashCommandBuilder()
        .setName("user")
        .setDescription("User category")
        .addSubcommand((subcommand) => 
        subcommand
            .setName("info")
            .setDescription("Get information about a user")),
  /**
   * @param {Client} client
   * @param {CommandInteraction} interaction
   * @param {string[]} args
   */
  run: async (client, interaction, args) => {
  },
};

斜杠命令处理:

// Slash Command Handling
    if (interaction.isCommand()) {

        const cmd = client.slashCommands.get(interaction.commandName);
        if (!cmd)
            return interaction.followUp({ content: "An error has occured " });

        const args = [];

        for (let option of interaction.options.data) {
            if (option.type === "SUB_COMMAND") {
                if (option.name) args.push(option.name);
                option.options?.forEach((x) => {
                    if (x.value) args.push(x.value);
                });
            } else if (option.value) args.push(option.value);
        }
        interaction.member = interaction.guild.members.cache.get(interaction.user.id);
        const command = cmd
        if(command.permission) {
            const authorPerms = interaction.channel.permissionsFor(interaction.member);
            if(!authorPerms || !authorPerms.has(command.permission)) {
                const err = new MessageEmbed()
                    .setDescription("Im sorry but you do not have the permissions to use this command")
                    .setColor("RED")
                await interaction.deferReply({ephemeral: true})
                return interaction.followUp({embeds: [err]}).then((sent) => {
                    setTimeout(() => {
                        sent.delete
                    }, 5000);
                })
            }
        }

        cmd.run(client, interaction, args);
    }
pgvzfuti

pgvzfuti1#

基本上,我没有使用SlashComamndBuilder,而是使用默认的方式创建斜杠命令,在斜杠命令中定义类型等。

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

module.exports = {
  name: 'user',
  description: 'User category',
  options: [
    {
        name: "whois",
        description: "Get information about a user",
        type: "SUB_COMMAND",
        options: [
            {
                name: "user",
                description: "The user whos information you want to view",
                type: "USER",
                required: true,
            }
        ]
    },
    {
        name: "avatar",
        description: "Get a users avatar",
        type: "SUB_COMMAND",
        options: [
            {
                name: "user",
                description: "The user whos information you want to view",
                type: "USER",
                required: true,
            }
        ]
    }
  ],
  /**
   * @param {Client} client
   * @param {CommandInteraction} interaction
   * @param {string[]} args
   */
  run: async (client, interaction, args) => {},
};

这给了我没有错误,虽然它是一个多一点的工作。不幸的是,我仍然没有弄清楚为什么SlashCommandBuilder在使用. addSubcommand时不提供类型。

uinbv5nw

uinbv5nw2#

我不知道你遵循的是什么指南,但官方指南显示的结构与你在这里看到的有点不同。
例如,命令本身被扩展到导出中,加上一个接受三个参数的run函数。文档(以及我过去的经验)显示,数据保存在data成员上,run函数应该是execute,只接受interaction和命令args。

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),
    async execute(interaction) {
        await interaction.reply('Pong!');
    },
};

但是,由于您自己处理命令,因此命令的执行函数的名称/args并不重要。我相信使用data而不是spreading仍然可以。
类似地,对于命令处理,您使用的是client.slashCommands,根据文档,它不存在。这是不是你在代码中的其他地方设置了什么东西而忽略了呢?官方指南显示了一个更像这样的处理命令的设置:

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

这里的一些关键注意事项是使用client.commands以及确保您await执行命令的功能,因为它是异步的。
我怀疑这里的确切问题可能在于options上的循环。然而,由于这不存在或没有文档记录,因此在没有完整代码的情况下很难说问题可能是什么。也许您正在对interaction使用某种自定义 Package 器来添加options

相关问题