How to you store data for each discord user in the discord server?

Required Dependencies: sqlite

Well considering that this question is very general, I’ll give a fairly general answer. Knowing that you’re using discord.js

One way you can complete your goal, is by creating an sqlite database. Note: this does not involve variables. To do this, you will have to require sqlite as a dependency:

const sql = require("sqlite");

Once completed this, you can create the file, the database will be in:

sql.open("./database.sqlite")

After which you will set up the table for the users:

sql.run("CREATE TABLE IF NOT EXISTS userData (userId TEXT, money INTEGER)").then(() => {
    sql.run("INSERT INTO userData (userId, money) VALUES (?, ?)", [<user id object here>, 0]);
});

That would create a database you can use to store information about each user in your guild.

After which to update their balance follow this format (example):

sql.get(`SELECT * FROM userData WHERE userId = ${msg.author.id}`).then(row => { //the row is the user's data
    if(!row) { //if the user is not in the database
      sql.run("INSERT INTO userData (userId, money) VALUES (?, ?)", [`${guildId}`, <user id object here>, 0]); //let's just insert them
      msg.channel.send("Registered.")
    } else { //if the user is in the database
      sql.run(`UPDATE userData SET money = ${row.money + 100} WHERE guild = ${msg.guild.id}`)
    }
});

If there are issues in the code provided or errors, feel free to comment or contact me 🙂

Leave a Comment