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

DiscordJS v13斜杠命令permission.set()报INVALID_TYPE错误咨询

问题原因
  • 核心传参错误:ApplicationCommandPermissionsManager.set() 方法要求传入的配置对象键名为复数的permissions,你的代码中使用了单数键名permission,导致方法无法识别到传入的权限数组,抛出类型错误。
  • 逻辑错误:代码中固定拉取ID为884318697794195486的命令修改权限,循环执行时所有命令的权限配置都会作用到同一个固定命令上,和你期望的为每个命令单独设置权限的逻辑不符。
  • 冗余操作:创建斜杠命令后会直接返回命令实例,不需要额外调用fetch方法拉取命令信息。
修复方案

修改对应代码段即可,参考修改后的逻辑:

client.on('messageCreate', async (msg) => {
    if (msg.content.startsWith('$')) {
        if (msg.content === '$add') {
            const slashCommandData = {};
            const commandFiles = fs
                .readdirSync('./src/commands')
                .filter((file) => file.endsWith('.js'));
            for (const file of commandFiles) {
                const command = require('./commands/' + file);
                console.log("- " + command.data.name);
                slashCommandData['name'] = command.data.name;
                slashCommandData['description'] = command.data.description;
                // 空值合并运算符简化options赋值逻辑
                slashCommandData['options'] = command.data.options ?? [];
                
                // 接收创建成功的命令实例
                const createdCommand = await client.guilds.cache
                    .get('877098630299934740')
                    ?.commands.create(slashCommandData);
                
                if (command.data.permission !== undefined) {
                    console.log("Permission for " + command.data.name);
                    const permissions = [
                        {
                            id: '461976441110921218',
                            type: 'USER',
                            permission: true,
                        },
                    ]
                    try {
                        // 传入正确的复数键名 permissions
                        await createdCommand.permissions.set({ permissions })
                    }
                    catch (error) {
                        console.log(error)
                    }
                }
            }
            msg.reply('Aggiunti i comandi nuovi!');
        }
    }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 15:09:02