部署Discord Bot Ping命令时遇ExpectedConstraintError错误求助
解决Discord Bot Ping命令的ExpectedConstraintError错误
问题详情
参照YouTube教程编写Discord Bot的Ping命令时,部署触发如下错误:
ExpectedConstraintError: Invalid string format at Object.run (C:\Discord Bot\node_modules@sapphire\shapeshift\dist\cjs\index.cjs:2350:64) at C:\Discord Bot\node_modules@sapphire\shapeshift\dist\cjs\index.cjs:939:67 at Array.reduce () at _StringValidator.parse (C:\Discord Bot\node_modules@sapphire\shapeshift\dist\cjs\index.cjs:939:29) at validateName (C:\Discord Bot\node_modules@discordjs\builders\dist\index.js:1578:17) at MixedClass.setName (C:\Discord Bot\node_modules@discordjs\builders\dist\index.js:1678:5) at Object. (C:\Discord Bot\commands\ping.js:5:6) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207: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' }
相关代码
index.js
require("dotenv").config(); const fs = require("node:fs"); const path = require("node:path"); const { DISCORD_TOKEN: token } = process.env; // Require the neccesary .js classes const { Client, GatewayIntentBits, Collection } = require("discord.js"); // Create a new client instance const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions, ], }); //load the events file on startup const eventsPath = path.join(__dirname, "events"); const eventFiles = fs .readdirSync(eventsPath) .filter((file) => file.endsWith(".js")); for (const file of eventFiles) { const filePath = path.join(eventsPath, file); const event = require(filePath); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } //load the command files on startup client.commands = new Collection(); const commandsPath = path.join(__dirname, "commands"); const commandFiles = fs .readdirSync(commandsPath) .filter((file) => file.endsWith(".js")); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ("data" in command && "execute" in command) { client.commands.set(command.data.name, command); } else { console.log( `[WARNING] The command at ${filePath} is missing a require "data" or "execute property"` ); } client.login(token);
ping.js
const { SlashCommandBuilder } = require("discord.js"); module.exports = { data: new SlashCommandBuilder() .setName("Ping") .setDescription("Replies with pong"), async execute(interaction) { console.log(interaction); await interaction.reply("pong"); }, };
interactionCreate.js
const { Events } = require("discord.js"); module.exports = { name: Events.InteractionCreate, async execute(interaction) { if (!interaction.isChatInputCommand()) return; const command = interaction.client.commands.get(interaction.commandName); if (!command) { console.error( `No command matching ${interaction.commandName} was found.` ); return; } try { await command.execute(interaction); } catch (error) { console.error(`Error executing ${interaction.commandName}`); console.error(error); } }, };
解决方案
错误根源是Slash命令名称不符合Discord的规则:命令名必须全小写,仅允许包含小写字母、数字、下划线_和连字符-,不能出现大写字母。
你的ping.js中把命令名设为了"Ping"(首字母大写),违反了格式限制。修改成全小写的"ping"即可解决问题,修改后的ping.js代码如下:
const { SlashCommandBuilder } = require("discord.js"); module.exports = { data: new SlashCommandBuilder() .setName("ping") .setDescription("Replies with pong"), async execute(interaction) { console.log(interaction); await interaction.reply("pong"); }, };
内容的提问来源于stack exchange,提问作者Jake Carpenter
相关产品推荐
相关产品推荐

