#levels dont save

1 messages · Page 1 of 1 (latest)

hybrid ginkgo
#

my levels dont save in the file

#

function LevelXP(message) {

var levelXP = Math.floor(Math.random() * 15) + 1

console.log(levelXP);

var idUser = message.author.id;

if (!levelFile[idUser]) {

    levelFile[idUser] = {
        xp: 0,
        level: 0
    }

}

levelFile[idUser].xp += levelXP;

var levelUser = levelFile[idUser].level;
var xpUser = levelFile[idUser].xp;
var nextLevelXp = levelUser * 300;

if (nextLevelXp == 0) nextLevelXp = 100;

if (xpUser >= nextLevelXp) {

    levelFile[idUser].level += 1;

    fs.writeFile("./Data/levels.json", JSON.stringify(levelFile),
        error => {
            if (err) return console.log("Something went wrong.");
        });

    var embedLevel = new MessageEmbed()
        .setDescription("***New level***")
        .setColor("GREEN")
        .addField("New level:", levelFile[idUser].level.toString());
    message.channel.send({ embeds: [embedLevel] });

}

}

wheat frigate
hybrid ginkgo
#

ok

hybrid ginkgo
#

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

module.exports = {

data: new SlashCommandBuilder()
    .setName('level')
    .setDescription('Shows your level!'),
async execute(client, interaction) {

    var levelFile = JSON.parse(fs.readFileSync("./Data/levels.json"));

    var userID = interaction.user.id;

    try {
        var nextLevelXP = levelFile[userID].level * 300;

        if (nextLevelXP == 0) nextLevelXP == 100;

        const rank = new canvacord.Rank()
            .setAvatar(interaction.user.displayAvatarURL({ dynamic: false, format: "png" }))
            .setCurrentXP(levelFile[userID].xp)
            .setLevel(levelFile[userID].level)
            .setRequiredXP(nextLevelXP)
            .setProgressBar("#FFA500", "COLOR")
            .setUsername(interaction.user.username)
            .setDiscriminator(interaction.user.discriminator);

        rank.build().then(data => {
            const attachment = new MessageAttachment(data, "RankCard.png");
            interaction.reply({ files: [attachment] });
        })    

    } catch(err){
        interaction.reply({ content: "❌ No data found" });
    }



}

}

wheat frigate
hybrid ginkgo
wheat frigate
hybrid ginkgo
#

and how do i fix it

wheat frigate
#

I don't know, console log the error and send it here

hybrid ginkgo
wheat frigate
#

Because you catch it in try...catch statement, put console.log(err) inside the catch block

wheat frigate
#

You should learn JavaScript before trying to create a discord bot or before trying to get help here because if you read the #rules you will find out we expect you to have a solid understanding of JavaScript

hybrid ginkgo
#

i knew java script

wheat frigate