How can I make a level system with my current xp system?

I have a mongodb database with an xp system already in place. I want to make it so if someone has 100 xp they level up.

My schema looks like:

const mongoose = require('mongoose');

const yenSchema = mongoose.Schema({
    guildID: String,
    guildName: String,
    name: String,
    userID: String,
    lb: String,
    yen: Number,
    daily: Number,
    xp: Number,
    level: Number,
    rank: Number,
    gear: String,
    class: String,
})

module.exports = mongoose.model("Yen", yenSchema)

So basically, I get xp between 1 and 3 when I use the daily command and so every 100 xp I want to make it so it says that I leveled up. So, when the xp reaches 100 it levels up then when Im level two it costs 200 and then the xp resets and so on…

Every time they get xp, do something like

if (user.xp == 100) {
 user.xp++;
user.xp = 0;
}

Or, instead of hard coding all the numbers, try:

const levelMultipler = 100

if (user.xp = levelMultiplier * user.level) {
user.xp++
}

Basically, 100xp to level 1, 200xp to level 2, etc

Where would I put this though? In a file called levels.js, then call that in the ready event in index?

Uhhh sure…? I do not know if the MVC paradigm applies to discord bots, but probably in a controller file. Also, I assume by index you mean index.js - make sure you split up your files so you don’t have to wade through 1000 lines of code.

If you send your file tree here, I can help you more I guess.

Like

.
./folder
./folder/file
./folder2/file
./package.json

Ahh, I understand what your saying, thanks. Is there any way you can explain it in more detail? Like how I would make the command with my mongodb.

Put the leveling script in the file where you change user’s information in the database. Like if you have a file that updates number of commands sent or name, put that there too.

Sorry if it’s kind of confusing.

I structured my backend app like

.
./views
./views/scripts
./views/styles
./views/
...
...
Bunch of EJS files
...
...
./create.js
./update.js
./static.js
...
...
so on, so forth
...
...
./package.json

So I would put the leveling script inside of ./update.js

Very confusing, lmao

I think I understand a bit tho.

I have no clue how to structure this… is there anyone that can give me a video or something? I use mongodb.

2 Likes

What’s your current file structure? Expand all the folders and send a screenshot

Im working on the levels folder rn

how about in ./game/daily.js? Or in ./game/profile.js? It might be more practical to make a user update folder - for like claiming dailies, getting levels, getting Xp, gifts, etc.

I want to make a leveling system, those are for coins and exp.

You get between 1-3 exp every time you use the daily command.

Yeah, move the leveling thing into the game folder.

The whole folder?

The current structure is now:

Btw, I call the levels folder in index.js

image

Get rid of the levels folder, you won’t need it anymore. Make levels into ./game/levels.js

Ok, done, did it.

What should I write in levels.js?

Idk… the level script?

… I made this thread for someone to tell me how to make one…

1 Like

whoops I made a mistake

var level = Yen.level
var count;
count = levels * 100

if (Yen.xp == count -= 100) {
Yen.xp=0;
level+=1;
count = levels * 100
}
1 Like

:0 Ill try this in the morning!!!

Is there a way I can integrate a message every time someone levels up? Is it as easy as doing message.reply(“You have leveled up”)?

1 Like

I found this on Google, this might help you:

You could integrate it with your current one.

1 Like

Ohhhh, this dude. He’s using fs, not mongodb. I think for now Ima try @code-alt 's message tmr, then try this out if his version doesn’t work.

1 Like

Yep!

just add it

var level = Yen.level
var count;
count = levels * 100

if (Yen.xp == count -= 100) {
Yen.xp=0;
level+=1;
callback(); // you can change this to whatever code or put it in the function callback()
count = levels * 100
}

function callback() {
// code goes here
}
1 Like

What goes in the callback function? So far I havve nothing in my levels.js.

So in the callback it would be message.channel.send()?

Yes. You can add any code in the callback. If it does not work then try replacing the callback() line to that code.

It is just something I added for easier access.

Nothing seemed to work… should I try putting this in my index file?

I did this to test:

  var level = Yen.level
  var count;
  count = level * 1
  var counts = level -= 1
  
  if (Yen.xp == 1) {
  Yen.xp=0;
  level+=1;
  message.channel.send("You have leveled up!")
  count = level * 1
  }

But when I got 1 xp my level didn’t increase.

image

I guess you should. I will try and come up with a solution.

edit: == should be there, count should be 100, as you know instead of one.

You should also set a default level since the current one is undefined, which will never actually change.