admin管理员组

文章数量:1125015

I'm building my discord bot on the Discord library.js and I need to make a resource system. To do this, I decided to use the sqlite3 database in conjunction with sequelize. I use json strings to store resources.

const { EmbedBuilder } = require('discord.js');
const Inventory = require('../../модели/inventory');

module.exports = async (interaction) => {
  if (!interaction.isStringSelectMenu()) return;

  if (interaction.customId === 'resourceMine') {
    const selectedValue = interaction.values[0];

    let resource = '';
    let value = 0;
    switch (selectedValue) {
      case 'wood':
        resource = 'дерево';
        break;
      case 'stone':
        resource = 'камень';
        break;
      default:
        await interaction.reply("Что-то пошло не так! Повторите попытку позже");
        return;
    }

    await interaction.update({ content: 'Загрузка...', components: [] });

    value = Math.floor(Math.random() * (10 - 0 + 1)) + 0;
    console.log(`Ресурс: ${resource}, количество: ${value}`);

    const inventory = await Inventory.findOne({ where: { id: interaction.user.id } });

    console.log(`Инвентарь пользователя: ${JSON.stringify(inventory)}`);

    if (!inventory) {
      return interaction.reply("Инвентарь не найден!");
    }

    let resources = inventory.resources || {};

    console.log('Текущие ресурсы перед обновлением:', resources);

    if (resources[resource]) {
      resources[resource] = (parseInt(resources[resource], 10) + value).toString();
    } else {
      resources[resource] = value.toString();
    }

    console.log('Обновленные ресурсы:', resources);

    try {
      await inventory.update({
        resources: resources,
      });
      console.log('Инвентарь успешно обновлен');
    } catch (error) {
      console.error('Ошибка при обновлении инвентаря:', error);
      return interaction.reply("Ошибка при сохранении данных. Попробуйте снова.");
    }

    const embed = new EmbedBuilder()
      .setColor('#9ACD32')
      .setTitle(resource)
      .setDescription(`Вы добыли ${value} ${resource}!`);

    await interaction.deleteReply();
    await interaction.channel.send({ embeds: [embed] });
  }
};

I'm new to programming, so I don't know many things yet.

I try to apply changes to the database, but the value remains the same. There are no errors in the console

本文标签: nodejsJSON changes in the sequelize database are not appliedStack Overflow