I'm following code from a channel on YT and im given this error and cant figure out how to resolve it.
This is the error
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' }
HERE IS MY index.js CODE
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);
HERE IS MY COMMAND CODE FOR PING
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");
},
};
HERE IS MY interactionCreate.js CODE
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);
}
},
};
https://discordjs.guide/creating-your-bot/slash-commands.html#before-you-continue
You call the command 'Ping', but discord.js requires slash commands not include capital letters. I believe this is likely the issue you are experiencing.
You can find this from the given error by looking through until you find something that references a line in your code (in this case "C:\Discord Bot\commands\ping.js:5:6" means the issue is on line 5 of ping.js). Then check to see if you can find anything about the function used here (
setName()) online. From here, you will be able to find the restrictions on this function. (You can tell the issue is likely with the string because the error says it's an expected constraint "ExpectedConstraintError: Invalid string format" meaning it expected your string to follow some rules that it didn't.)