Discord.js require() don't actualise
I'm creating a Discord Bot in Node.js.
I have a json file that stores user's XP.
When executing rank command, the xp don't actualise. It returns the same number, and only refreshes when we restart the bot. However, the JSON file is updated with each message.
How can I do it on this page?
index.js on message
client.on("message", message => {
if (message.author.bot || message.content.startsWith("mfn help")) return;
rk = require("./xp.json")[message.author.id];
if (!rk) {
writeWithoutKey("./xp.json", `"${message.author.id}": { "xp": 0, "lvl": 0, "rank": 0 }`);
delete rk;
} else {
delete rk;
}
if (message.channel.parentID == "807164915697451018") {
if (message.content == "//cancel") return;
var usr = message.channel.name;
client.users.fetch(usr).then(u => {
u.createDM().then(c => {
c.send(`**[${message.author.username}]**\n${message.content}`);
})
});
} else if (message.channel.type === 'dm') {
if (client.guilds.cache.get("794847932226338826").channels.cache.some(c => c.name == message.author.id)) {
var ch = client.channels.cache.find(c => c.name == message.author.id);
var id = ch.id;
if (message.content == "//close") {
message.author.createDM().then(c => {
c.send(`Ticket succefully closed.`);
})
.then(u => {
ch.send(`:lock: **Ticket Closed by ${message.author.tag}.** Deleting channel in 60 seconds. Type \`//cancel\` to not delete the channel.`)
.then(async msg => {
const filter = m => m.content.toLowerCase().startsWith("//cancel");
const msgacollector = msg.channel.createMessageCollector(filter, { max: 1, time: 60000 });
msgacollector.on('collect', msg => {
closeticket = false;
ch.send("Channel deletion cancelled. You have to delete channel yourself.");
ch.setName(`old-${message.author.id}`);
});
msgacollector.on('end', (collected, reason) => {
if (closeticket != false) {
ch.delete();
closeticket = true;
} else {
closeticket = true;
}
})
});
})
return;
} else {
ch.send(`**[${message.author.username}]**\n${message.content}`);
}
} else {
if (mcc == true) return;
message.channel.send("Do you want to chat with Muffin Bot Staff ? [ Yes | No ]")
.then(async msg => {
const filter = m => m.content.toLowerCase().includes("yes") || m.content.toLowerCase().includes("no");
const msgcollector = msg.channel.createMessageCollector(filter, { max: 1 });
mcc = true;
msgcollector.on('collect', msg => {
mcc = false;
msgcont = msg.content.toLowerCase();
if (msgcont == "yes") {
if (client.guilds.cache.get("794847932226338826").channels.cache.some(c => c.name == `old-${message.author.id}`)) {
client.channels.cache.find(c => c.name == `old-${message.author.id}`).delete()
.catch(() => message.channel.send("An error occured."))
}
message.channel.send("Creation of your ticket.");
cparent = client.channels.cache.get("807164915697451018");
client.guilds.cache.get("794847932226338826").channels.create(`${message.author.id}`, { parent: cparent })
.then(c => c.send(`**${message.author.tag}** just created this ticket.`))
.then(a => message.channel.send("Your ticket have been successfully created. Just write your messages here and staff will answer you. Type `//close` to close your ticket."));
} else {
return;
}
});
});
}
} else if (message.mentions.users.first() == client.user.id) {
const args = message.content.slice(config.prefix.length).split(/ +/);
client.commands.get("help").execute(message, args);
} else if (message.content.startsWith(config.prefix)) {
const args = message.content.slice(config.prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
try {
client.commands.get(command).execute(message, args, rk);
} catch (error) {
console.error(error);
message.channel.send(`⚠️ **An error has occurred !**`);
}
} else {
currentxp = require('./xp.json');
newxp = Math.random() * (20 - 5) + 5;
newxp = currentxp[message.author.id].xp + Math.floor(newxp);
writeToJsonKey(`./xp.json`, `[${message.author.id}].xp`, newxp)
delete currentxp;
delete newxp;
delete totalxp;
}
});
rank command
const Discord = require('discord.js');
const fetch = require("node-fetch");
module.exports = {
name: 'rank',
execute(message, args) {
if (rk) delete rk;
var rk = require("./../xp.json");
rk = rk[message.author.id];
console.log(rk);
const embed = new Discord.MessageEmbed()
.setColor('#0080CF')
.setAuthor(message.author.username, message.author.avatarURL())
.addFields({ name: 'XP', value: rk.xp }, { name: 'Level', value: rk.lvl }, { name: 'Rank', value: '`mfn leaderboard`' })
.setTimestamp()
.setFooter(message.guild.name);
message.channel.send(embed);
}
}
1
Please sign in to leave a comment.
Comments
0 comments