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

Discord.js测试斜杠命令报错:setDMPermissions不是函数

问题

我是Discord.js新手,编写测试斜杠命令时遇到错误,错误信息如下:

[ERROR] An error occured inside the command registery:
TypeError: (intermediate value).setName(...).setDescription(...).setDMPermissions is not a function

我的代码:

const {SlashCommandBuilder, PermissionFlagsBits} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
     .setName('test')
     .setDescription('Test if everything is working.')
     .setDMPermissions(false)
     .addSubcommandGroup((subcommandgroup) => 
        subcommandgroup
         .setName("user")
         .setDescription("Configure an user.")
         .addSubcommand((subcommand) => 
            subcommand
                .setName("role")
                .setDescription("Configure an user's role.")
                .addUserOption((option) => 
                    option
                    .setName("user")
                    .setDescription("The user to configure.")
            ),
        )
        .addSubcommand((subcommand) => 
            subcommand
                .setName("nickname")
                .setDescription("Configure an user's nickname.")
                .addStringOption((option) => 
                    option
                    .setName("nickname")
                    .setDescription("The nickname the user should have.")
                .addUserOption((option) => 
                    option
                    .setName("user")
                    .setDescription("The user to configure.")
            ),
            )
        )
        .addSubcommand((subcommand) => 
            subcommand
                .setName("message")
                .setDescription("Configure a message.")
        )
    )
    .toJSON(),
    userPermissions: [PermissionFlagsBits.ManageMessages],
    botPermissions: [PermissionFlagsBits.Connect],

    run: (client, interaction) => {
        return interaction.reply("This is a test command.")
    }
};

我试过增删代码、合并代码行,都没解决,求帮忙修复。

解决方案

这个错误的核心原因是:带有子命令/子命令组的斜杠命令,不能直接在根命令上调用setDMPermissions()方法。Discord.js的规则是,当命令包含子命令或子命令组时,DM权限需要设置在子命令层级,而非根命令。

另外你的代码还有两处语法错误,一并修复:

  1. addStringOption的回调未正确闭合,导致语法结构混乱
  2. 存在英文拼写小错误(an user应为a user,不影响运行但不够规范)

修复后的完整代码:

const {SlashCommandBuilder, PermissionFlagsBits} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
     .setName('test')
     .setDescription('Test if everything is working.')
     .addSubcommandGroup((subcommandgroup) => 
        subcommandgroup
         .setName("user")
         .setDescription("Configure a user.")
         .addSubcommand((subcommand) => 
            subcommand
                .setName("role")
                .setDescription("Configure a user's role.")
                .setDMPermissions(false) // 将DM权限移至子命令层级
                .addUserOption((option) => 
                    option
                    .setName("user")
                    .setDescription("The user to configure.")
                )
         )
        .addSubcommand((subcommand) => 
            subcommand
                .setName("nickname")
                .setDescription("Configure a user's nickname.")
                .setDMPermissions(false)
                .addStringOption((option) => 
                    option
                    .setName("nickname")
                    .setDescription("The nickname the user should have.")
                )
                .addUserOption((option) => 
                    option
                    .setName("user")
                    .setDescription("The user to configure.")
                )
         )
        .addSubcommand((subcommand) => 
            subcommand
                .setName("message")
                .setDescription("Configure a message.")
                .setDMPermissions(false)
         )
     )
    .toJSON(),
    userPermissions: [PermissionFlagsBits.ManageMessages],
    botPermissions: [PermissionFlagsBits.Connect],

    run: (client, interaction) => {
        return interaction.reply("This is a test command.")
    }
};

关键修复点:

  • 移除根命令上的.setDMPermissions(false),将该方法添加到每个子命令的链式调用中
  • 补全所有未闭合的回调括号,确保语法结构正确
  • 修正拼写错误(可选,提升代码规范性)

修改后,命令注册时的TypeError会消失,代码也符合Discord.js的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 04:36:07