简体   繁体   English

Discord.JS 命令中的表单主体无效

[英]Discord.JS Invalid Form Body in command

When im starting my discord bot on discord.js v 14.7.1 this error shows up, and I don't know how can I fix it当我在 discord.js v 14.7.1 上启动我的 discord 机器人时出现此错误,我不知道该如何解决

DiscordAPIError[50035]: Invalid Form Body
0.name[BASE_TYPE_REQUIRED]: This field is required
 rawError: {
    errors: { '0': [Object] },
    message: 'Invalid Form Body'
    errors: { '0': [Object] },
    message: 'Invalid Form Body'
  },

In error there's two objects, but I don't have any other file in commands folder Here's the only one command that I have in files.错误地有两个对象,但我在命令文件夹中没有任何其他文件这是我在文件中拥有的唯一一个命令。 in directory commands/tools/embed.js.在目录 commands/tools/embed.js 中。

const { SlashCommandBuilder, EmbedBuilder} = require('discord.js');
module.exports = {
    data: new SlashCommandBuilder()
        .setName("embed")
        .setDescription("Embeds a message"),
    async execute(interaction, client) {
        const embed = new EmbedBuilder()
            .setTitle("Embed Title")
            .setDescription("Embed Description")
            .setColor("RANDOM")
            .setThumbnail(client.user.displayAvatarURL())
            .setTimestamp(Date.now())
            .setFooter({
                iconURL: client.user.displayAvatarURL(),
                text: client.user.tag
            })
            .addFields([
                {
                    name: 'Field 1',
                    value: 'Filed value 1',
                    inline: true
                }
            ])
        await interaction.reply({ embeds: [embed] });
    }
}

and heres my commands handler这是我的命令处理程序

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(`Loaded command ${command.data.name}`);
            }
        }
        const clientId = '1040763751864995971';
        const guildId = '1000901014158856272'

        const rest = new REST({ version: '9' }).setToken(process.env.TOKEN);
        try {
            console.log('Started refreshing application (/) commands.');
            await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: client.commandArray });
            console.log('Successfully reloaded application (/) commands.');
        } catch (error) {
            console.error(error);
        }
    }
}

我没有尝试任何东西,我只是不知道是什么导致了问题。 commandArray 的 Console.log 向我展示了这个

I found a solution我找到了解决方案

When I was pushing the command to the commandArray, I was pushing the Command object too.当我将命令推送到 commandArray 时,我也在推送命令 object。 The array I needed to send to the API is only a string array, so...我需要发送到 API 的数组只是一个字符串数组,所以......

replace this替换这个

   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(`Loaded command ${command.data.name}`);
            }

with this有了这个

 for (const file of commandFiles){
                const command = require(`../../commands/${folder}/${file}`);
                commands.set(command.data.name, command)
                commandArray.push(command.data.toJSON());
                console.log(`Loaded command ${command.data.name}`);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM