'Discord-xp / Canvacord Ranking system
I am having issues with my rank system, when I attempted to level up (by spamming), I started to notice that I don't actually reach the required amount of XP before leveling up (I have an image below to further explain this).
I've asked a few people about the issue, but they can't seem to know the problem, so here I am.. asking the wonderful people of stackoverflow!
I have tried a few things to try to fix the issue, but at this point I feel like its a dead-end, perhaps I'm doing something wrong, or just don't fully understand it, or maybe I am calculating the XP wrong, no idea at this time.
Below I have provided my code for the portion I have in the messageCreate
event, and I've also provided my code the the entire rank command in hopes that I would get some assistance or a point in the right direction on what I need to do to solve this issue.
messageCreate.js
const Levels = require("discord-xp");
Levels.setURL("");
client.on("messageCreate", async (message) => {
if (!message.guild) return;
if (message.author.bot) return;
const xp = Math.floor(Math.random() * 29) + 1;
const hasLeveledUp = await Levels.appendXp(message.author.id, message.guild.id, xp);
if (hasLeveledUp) {
const user = await Levels.fetch(message.author.id, message.guild.id);
message.channel.send({ content: `<a:CL_Confetti:909438449910681680> ${message.author} has reached **level ${user.level}**!` });
}
...
});
rank.js
const { MessageEmbed, MessageAttachment } = require("discord.js");
const Levels = require("discord-xp");
const canvacord = require("canvacord");
module.exports = {
name: "rank",
aliases: [],
description: "Displays a users current rank!",
usage: "<user>",
run: async (client, message, args) => {
let target = message.mentions.users.first()
? message.mentions.users.first()
: message.author;
const user = await Levels.fetch(target.id, message.guild.id, true);
if (!user) return message.reply("That user does not have any xp.");
const rank = new canvacord.Rank()
.setAvatar(target.displayAvatarURL({ dynamic: false, format: "png", size: 512 }))
.setCurrentXP(user.cleanXp)
.setRequiredXP(Levels.xpFor(user.level + 1))
.setRank(user.position)
.setLevel(user.level)
.setStatus(message.member.presence.status)
.setProgressBar("#DC143C", "COLOR")
.setUsername(target.username)
.setDiscriminator(target.discriminator);
rank.build().then((data) => {
const attachment = new MessageAttachment(data, "rank.png");
message.reply({ files: [attachment] });
});
},
};
Image
I also have this image here of what it is doing inside discord, any help would be greatly appreciated as I have recently taken my first steps into canvacord, and am willing to learn from this issue!
Solution 1:[1]
I found the issue.
In messageCreate.js you have an XP mulitpleir. The problem is that its also not reading rank.js/its not required.
const xp = Math.floor(Math.random() * 29) + 1;
.setRequiredXP(Levels.xpFor(user.level + 1))
more or less your multiplier is surpassing the amount required in my opinion, and with the two different files your assigning 2 different values, so it gets called in rank.js and then it checks if it has surpassed the xp limit. rank.js added not messagecreate but messagecreative is updating the value by that multiplier without checking whatever updates the leveling in rank js which is basically 1.
You don't need messagecreate.js you can technically just check if someone has did whatever message.js is doing. and also take out.
ANYWAYS the real issue is that in messagejs its checking if it leveled up and then responding so you are leveling up without being told the rank embed to update nor the value of hasLeveled up when it does level up in messageCreate.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | D J |