使用下面的代码时出现此错误

问题描述 投票:0回答:1

这是每次我尝试使用该命令时控制台中不断弹出的错误

UnknownEnumValueError > PRIMARY 期望该值是其中之一 以下枚举值:

|小学或 1 |中学或2 |成功或 3 |危险或4
|链接或5

这是我的代码,也许我在某个地方犯了错误

const { EmbedBuilder, ActionRowBuilder, ButtonBuilder } = require('discord.js');

module.exports = async (bot, message, args, argsF) => {
  let cards = [];
  let playerHand = [];
  let playerScore = 0;
  let gameOver = false;

  function createDeck() {
    for (let i = 2; i <= 11; i++) {
      cards.push(i);
    }
  }

  function drawCard() {
    const index = Math.floor(Math.random() * cards.length);
    const card = cards[index];
    cards.splice(index, 1);
    return card;
  }

  function updatePlayerHand() {
    const card = drawCard();
    playerHand.push(card);
  }

  function updatePlayerScore() {
    playerScore = playerHand.reduce((sum, card) => sum + card, 0);
  }

  async function playGame(msg) {
    createDeck();
    updatePlayerHand();
    updatePlayerScore();

    const row = new ActionRowBuilder().addComponents(
      new ButtonBuilder()
        .setCustomId('hit')
        .setLabel('Ещё')
        .setStyle('PRIMARY'),
      new ButtonBuilder()
        .setCustomId('stay')
        .setLabel('Стоп')
        .setStyle('SUCCESS')
    );

    const embed = new EmbedBuilder()
      .setTitle('BlackJack')
      .setColor('#FECB4E')
      .setDescription('Цель игры: набрать как можно больше очков, но не более 21.')
      .addField('Ваши карты:', playerHand.join(', '))
      .addField('Ваш счет:', playerScore.toString())
      .setFooter('🃏 Ещё или стоп? Выберите действие.');

    const gameMessage = await msg.channel.send({ embeds: [embed], components: [row] });

    const collector = gameMessage.createMessageComponentCollector({ componentType: 'BUTTON', time: 60000 });

    collector.on('collect', async (interaction) => {
      if (!gameOver) {
        if (interaction.user.id === msg.author.id) {
          await interaction.deferUpdate();

          if (interaction.customId === 'hit') {
            updatePlayerHand();
            updatePlayerScore();

            if (playerScore > 21) {
              gameOver = true;
              embed.setColor('#FF4B4B')
                .setDescription('😔 Вы проиграли! Ваш счет превысил 21.')
                .setFooter('');
              gameMessage.edit({ embeds: [embed], components: [] });
            } else {
              embed
                .spliceFields(0, 2)
                .addField('Ваши карты:', playerHand.join(', '))
                .addField('Ваш счет:', playerScore.toString());
              gameMessage.edit({ embeds: [embed] });
            }
          } else if (interaction.customId === 'stay') {
            gameOver = true;
            embed.setColor('#45F06A')
              .setDescription('🎉 Поздравляем! Вы завершили игру.')
              .setFooter('');
            gameMessage.edit({ embeds: [embed], components: [] });
          }
        }
      } else {
        interaction.deferUpdate();
      }
    });

    collector.on('end', () => {
      if (!gameOver) {
        gameOver = true;
        gameMessage.edit({ components: [] });
      }
    });
  }

  playGame(message);
};

module.exports.names = ['blackjack', 'блэкджек'];
javascript constructor discord discord.js
1个回答
0
投票

setStyle
需要枚举值或数字,而不是字符串

const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');

module.exports = async (bot, message, args, argsF) => {
  let cards = [];
  let playerHand = [];
  let playerScore = 0;
  let gameOver = false;

  function createDeck() {
    for (let i = 2; i <= 11; i++) {
      cards.push(i);
    }
  }

  function drawCard() {
    const index = Math.floor(Math.random() * cards.length);
    const card = cards[index];
    cards.splice(index, 1);
    return card;
  }

  function updatePlayerHand() {
    const card = drawCard();
    playerHand.push(card);
  }

  function updatePlayerScore() {
    playerScore = playerHand.reduce((sum, card) => sum + card, 0);
  }

  async function playGame(msg) {
    createDeck();
    updatePlayerHand();
    updatePlayerScore();

    const row = new ActionRowBuilder().addComponents(
      new ButtonBuilder()
        .setCustomId('hit')
        .setLabel('Ещё')
        .setStyle(ButtonStyle.Primary),
      new ButtonBuilder()
        .setCustomId('stay')
        .setLabel('Стоп')
        .setStyle(ButtonStyle.Success)
    );

    const embed = new EmbedBuilder()
      .setTitle('BlackJack')
      .setColor('#FECB4E')
      .setDescription('Цель игры: набрать как можно больше очков, но не более 21.')
      .addField('Ваши карты:', playerHand.join(', '))
      .addField('Ваш счет:', playerScore.toString())
      .setFooter('🃏 Ещё или стоп? Выберите действие.');

    const gameMessage = await msg.channel.send({ embeds: [embed], components: [row] });

    const collector = gameMessage.createMessageComponentCollector({ componentType: 'BUTTON', time: 60000 });

    collector.on('collect', async (interaction) => {
      if (!gameOver) {
        if (interaction.user.id === msg.author.id) {
          await interaction.deferUpdate();

          if (interaction.customId === 'hit') {
            updatePlayerHand();
            updatePlayerScore();

            if (playerScore > 21) {
              gameOver = true;
              embed.setColor('#FF4B4B')
                .setDescription('😔 Вы проиграли! Ваш счет превысил 21.')
                .setFooter('');
              gameMessage.edit({ embeds: [embed], components: [] });
            } else {
              embed
                .spliceFields(0, 2)
                .addField('Ваши карты:', playerHand.join(', '))
                .addField('Ваш счет:', playerScore.toString());
              gameMessage.edit({ embeds: [embed] });
            }
          } else if (interaction.customId === 'stay') {
            gameOver = true;
            embed.setColor('#45F06A')
              .setDescription('🎉 Поздравляем! Вы завершили игру.')
              .setFooter('');
            gameMessage.edit({ embeds: [embed], components: [] });
          }
        }
      } else {
        interaction.deferUpdate();
      }
    });

    collector.on('end', () => {
      if (!gameOver) {
        gameOver = true;
        gameMessage.edit({ components: [] });
      }
    });
  }

  playGame(message);
};

module.exports.names = ['blackjack', 'блэкджек'];
© www.soinside.com 2019 - 2024. All rights reserved.