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

discord.js注册带选项斜杠命令报名称无效错误排查

问题根因

报错options[x].name: Command name is invalid是斜杠命令参数不符合Discord官方校验规则导致的,你的代码共有3处明确问题:

  • 斜杠命令的名称、所有选项的名称强制要求全小写,仅允许使用字母、数字、-、_,不能包含大写字符、空格、其他特殊符号。你定义的两个选项名UTENTE、MOTIVO为全大写,直接触发API校验失败。
  • 标记参数为必填的字段名拼写错误,正确字段名是required,你写的是require(少了末尾的d),该配置不会生效。
  • 最后打印命令的语句变量名拼写错误,你定义的变量是command,打印时写的是comando,注册成功后执行到这行会抛出变量未定义的错误。
修复后的代码

把注册命令的data部分和后续打印逻辑改成如下即可:

client.on('messageCreate', async (message) => {
    if (!client.application?.owner) {
        await client.application?.fetch();
    }

    if (message.content.toLowerCase() === 'lava.registra' && message.author.id === client.application?.owner.id) {
        const data = {
            name: 'ban',
            description: 'Banna un utente dal server permanentemente',
            options: [
                {
                    name: "utente",
                    description: "Specifica l'utente da bannare",
                    type: "USER",
                    required: true,
                },
                {
                    name: "motivo",
                    description: "Specifica il motivo del ban",
                    type: "STRING",
                    required: true,
                }
            ]
        };

        // 注册全局命令
        //const command = await client.application?.commands.create(data);
        //console.log(command);

        // 注册服务器专属命令
        const command = await client.guilds.cache.get('957317299289858108')?.commands.create(data);
        console.log(command);
    }
})

补充说明:如果使用discord.js v13及以上版本,type字段建议使用内置枚举值(比如type: 'USER'对应ApplicationCommandOptionType.User),不过字符串写法不会触发本次的注册报错,可以后续再调整规范。

内容的提问来源于stack exchange,提问作者YI HAN ZHOU

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 14:27:15