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
版权声明:本文标题:node.js - JSON changes in the sequelize database are not applied - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736658764a1946322.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论