You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js欢迎消息命令报错:Invalid Form Body问题求助

问题排查与修复:Discord机器人欢迎命令Invalid Form Body错误

错误根源分析

报错APPLICATION_COMMAND_INVALID_NAME明确指向命令选项不符合Discord规范,同时代码存在两处问题:

  • Discord要求Slash命令的选项名称只能包含小写字母、数字和连字符,你的第一个选项name:"Channel"使用了大写字母C,违反命名规则;
  • 第二个选项里的reqired:true是拼写错误,正确应为required:true,会导致必填参数校验失败。

修复后的完整代码

const {Message, Client, SlashCommandBuilder, PermissionFlagsBits} = require("discord.js");
const welcomeSchema = require("../../Models/Welcome");
const {model, Schema} = require("mongoose");

module.exports = {
    name:"setup-welcome",
    description:"Set up your welcome message for the discord bot.",
    UserPerms:["BanMembers"],
    category:"Moderation",
    options: [
        {
            name:"channel", // 修正为全小写名称
            description:"Channel for welcome messages.",
            type:7,
            required:true
        },

        {
            name:"welcome-message",
            description:"Enter your welcome message.",
            type:3,
            required:true // 修正拼写错误
        },

        {
            name:"welcome-role",
            description:"Enter your welcome role.",
            type:8,
            required:true
        }
    ],
                
    async execute(interaction) {
        const {channel, options} = interaction;

        const welcomeChannel = options.getChannel("channel");
        const welcomeMessage = options.getString("welcome-message");
        const roleId = options.getRole("welcome-role");

        if(!interaction.guild.members.me.permissions.has(PermissionFlagsBits.SendMessages)) {
            return interaction.reply({content: "I don't have permissions for this.", ephemeral: true}); // 添加return避免后续代码执行
        }

        welcomeSchema.findOne({Guild: interaction.guild.id}, async (err, data) => {
            if(!data) {
                await welcomeSchema.create({
                    Guild: interaction.guild.id,
                    Channel: welcomeChannel.id,
                    Msg: welcomeMessage,
                    Role: roleId.id
                });
            }
            await interaction.reply({content: 'Successfully created a welcome message', ephemeral: true}); // 修正拼写错误,添加await确保异步操作完成
        })
    }
}

额外优化说明

  • 在权限判断后添加return,避免机器人无权限时仍执行后续逻辑;
  • 修正回复内容里的拼写错误Succesfully为Successfully;
  • 对interaction.reply添加await,确保异步回复操作完成,避免潜在的未处理Promise警告。

内容的提问来源于stack exchange,提问作者PrayNikita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 03:05:42