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

Discord.js Slash Command报错:ExpectedConstraintError修复求助

问题:Discord.js开发Slash Command时出现"ExpectedConstraintError: Invalid string format"错误

在使用Discord.js开发Slash Command时,遇到如下报错:

ExpectedConstraintError: Invalid string format

相关代码

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('Ping')
        .setDescription('Return my ping'),
    async execute(interaction, client) {
        const message = await interaction.deferReply({
            fetchReply : true
        });

        const newMessage = `API Latency: ${client.ws.ping}\nClient Ping: ${message.createdTimestamp - interaction.createdTimestamp}`
        await interaction.editReply({
            content: newMessage
        });

    }
}

控制台报错信息

C:\Users\The Friesen Boy's\Desktop\georgebot\node_modules\@sapphire\shapeshift\dist\index.js:1564
      return regex.test(input) ? Result.ok(input) : Result.err(new ExpectedConstraintError(type, "Invalid string format", input, expected));
                                                               ^

ExpectedConstraintError: Invalid string format
    at Object.run (C:\Users\The Friesen Boy's\Desktop\georgebot\node_modules\@sapphire\shapeshift\dist\index.js:1564:64)
    at C:\Users\The Friesen Boy's\Desktop\georgebot\node_modules\@sapphire\shapeshift\dist\index.js:142:66
    at Array.reduce (<anonymous>)
    at StringValidator.parse (C:\Users\The Friesen Boy's\Desktop\georgebot\node_modules\@sapphire\shapeshift\dist\index.js:142:29)
    at validateName (C:\Users\The Friesen Boy's\Desktop\georgebot\node_modules\@discordjs\builders\dist\index.js:782:17)
    at MixedClass.setName (C:\Users\The Friesen Boy's\Desktop\georgebot\node_modules\@discordjs\builders\dist\index.js:856:5)
    at Object.<anonymous> (C:\Users\The Friesen Boy's\Desktop\georgebot\src\commands\tools\ping.js:5:10)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32) {
  constraint: 's.string.regex',
  given: 'Ping',
  expected: 'expected /^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{N}\\p{sc=Devanagari}\\p{sc=Thai}_-]+$/u.test(expected) to be true'
}

Node.js v18.6.0

找不到解决办法,相关讨论帖也无法解决问题,求修复方案。


修复方案

问题根源在于Discord的Slash Command名称必须全小写,且只能包含小写字母、数字、下划线和连字符,不能有大写字母。你的代码里.setName('Ping')用了大写首字母,不符合规则。

修改后的代码:

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping') // 改为全小写
        .setDescription('Return my ping'),
    async execute(interaction, client) {
        const message = await interaction.deferReply({
            fetchReply : true
        });

        const newMessage = `API Latency: ${client.ws.ping}\nClient Ping: ${message.createdTimestamp - interaction.createdTimestamp}`
        await interaction.editReply({
            content: newMessage
        });

    }
}

修改后重新部署命令即可解决该错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:48:51