Home > database >  ExpectedConstraintError: Invalid string format discord.js
ExpectedConstraintError: Invalid string format discord.js

Time:07-21

I am trying to make a slashcommandbuilder for my coinflip command but something is going on. Btw I am new to discord.js. This is the error I get.

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\dhart\Desktop\Projects\ExistentialThreat\node_modules\@sapphire\shapeshift\dist\index.js:1564:64)
    at C:\Users\dhart\Desktop\Projects\ExistentialThreat\node_modules\@sapphire\shapeshift\dist\index.js:142:66
    at Array.reduce (<anonymous>)
    at StringValidator.parse (C:\Users\dhart\Desktop\Projects\ExistentialThreat\node_modules\@sapphire\shapeshift\dist\index.js:142:29)     
    at validateName (C:\Users\dhart\Desktop\Projects\ExistentialThreat\node_modules\@discordjs\builders\dist\index.js:782:17)
    at SlashCommandUserOption.setName (C:\Users\dhart\Desktop\Projects\ExistentialThreat\node_modules\@discordjs\builders\dist\index.js:856:5)
    at C:\Users\dhart\Desktop\Projects\ExistentialThreat\commands\coinflip.js:8:37
    at MixedClass._sharedAddOptionMethod (C:\Users\dhart\Desktop\Projects\ExistentialThreat\node_modules\@discordjs\builders\dist\index.js:1256:50)
    at MixedClass.addUserOption (C:\Users\dhart\Desktop\Projects\ExistentialThreat\node_modules\@discordjs\builders\dist\index.js:1230:17)
    at Object.<anonymous> (C:\Users\dhart\Desktop\Projects\ExistentialThreat\commands\coinflip.js:8:4) {
  constraint: 's.string.regex',
  given: 'my guess',
  expected: 'expected /^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{N}\\p{sc=Devanagari}\\p{sc=Thai}_-] $/u.test(expected) to be true'

My code looks like this.

const { EmbedBuilder } = require("discord.js");
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
  data: new SlashCommandBuilder()
  .setName('coinflip')
  .setDescription('Returns value heads or tails.')
  .addUserOption((option) => option.setName('my guess').setDescription('Enter heads or tails.').setRequired(false)),
  //.addChoices({ name: 'Heads', value: 'guess heads'}, { name: 'Tails', value: 'guess tails'}, { name: 'Other', value: 'You are making a mistake. The chance of this is low.'}),

  async execute(interaction) {
    let coinflip = (Math.random() * 100).toFixed(0) % 2;
    let embedThumbnail = `https://m.media-amazon.com/images/I/51bcZy HZpL._AC_.jpg`;
    console.log(coinflip);
    let result = 'tails';
    if (coinflip == 1){
        result = 'heads';
        embedThumbnail = `https://i.ebayimg.com/images/g/xtcAAOSwLwBaZigS/s-l400.jpg`;
    }
    let guess = interaction.options.getString('my guess');
    let guessedmessage = 'You guessed it correct!'
    if (!result == guess){
        guessedmessage = 'You guessed it wrong, but good try.';
    };

I would appreciate some help in fixing this issue.

CodePudding user response:

The 'my guess' option is a user option, not a string option. Change it to:

.addStringOption((option) => option.setName('my guess').setDescription('Enter heads or tails.').setRequired(false)),
  • Related