#levels dont save
1 messages · Page 1 of 1 (latest)
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] });
}
}
Hey! File manipulation isn't a part of discord.js library, please check #useful-servers
ok
wich server do i chose
the data is in my file but when i use /level it says no data found
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" });
}
}
}
General JavaScript support severs are The Coding Den and The Programmer's Hangout
can you help me with the /level
Console log the error in your try catch because you send "❌ No data found" when any error occurs, the error doesn't have to be related with the data
and how do i fix it
I don't know, console log the error and send it here
i dont get a error
Because you catch it in try...catch statement, put console.log(err) inside the catch block
dont work
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
i knew java script
I don't think so, read this:
try...catch usage https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
console.log() usage https://developer.mozilla.org/en-US/docs/Web/API/Console/log
And check #resources for more