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

Discord.js v13中SlashCommandBuilder非构造函数问题求助

解决Discord.js v13中"SlashCommandBuilder is not a constructor"错误

核心问题及修复步骤

  • 错误根源1:导入路径错误
    Discord.js v13中,SlashCommandBuilder 并不在主包 discord.js 中,而是需要从独立的 @discordjs/builders 包导入。先安装依赖:

    npm install @discordjs/builders
    

    再修改导入语句:

    const { SlashCommandBuilder } = require('@discordjs/builders');
    
  • 错误根源2:重复导出模块
    你的代码两次使用module.exports,第二次导出会完全覆盖第一次,导致命令的data属性丢失,进而引发构造器相关错误。必须把data和execute合并到同一个导出对象中:

    const { SlashCommandBuilder } = require('@discordjs/builders');
    
    module.exports = {
        data: new SlashCommandBuilder()
            .setName('smurf')
            .setDescription('Show time until smurf. 5 Minute warning will be executed as well.')
            .addStringOption(option =>
                option
                    .setName('points')
                    .setDescription('Points to win. Ex: 145,000')
                    .setRequired(true))
            .addStringOption(option =>
                option
                    .setName('current-score')
                    .setDescription('Current score. Ex: 50,000')
                    .setRequired(true))
            .addStringOption(option =>
                option
                    .setName('point-gain')
                    .setDescription('Point gain. Ex: 30')
                    .setRequired(true)),
        async execute(interaction) {
            const points = interaction.options.getString('points');
            const cScore = interaction.options.getString('current-score');
            const pGain = interaction.options.getString('point-gain');
            // 此处添加你的业务逻辑代码
        },
    };
    
  • 额外注意点
    代码中的=>是HTML转义后的箭头符号,实际编写时要替换为标准的=>,否则会触发语法错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:52:11