User Data

Setting individual's user data

When you start up your bot, MongoDB will automatically connect. If and error doesn't show up, you can continue. When writing a command, you can get the data of the person who sent the command by adding ds.getUserData(); It will look like this:

new ds.Command({
  name: 'ping',
  aliases: ['p'],
  async execute({send, label}) {

    const myData = await ds.getUserData();
    console.log(myData);

    //send(`Pong! (${label})`);
  }
});

when we run this command, you should get an empty object ({}) this is because we haven't set a default data, you can do this by adding (before the command) ds.defaultUserData(); Like this:

ds.defaultUserData({
  money: 0,
  name: 'unkown'
});

anything that you put in the defultUserData will be set whenever a new user is saved to the database.

you can set user data by running ds.setUserData();

ds.setUserData({
  money: 3,
  name: 'example-person'
});

if you don't add one of the parts, it will still be there in the database! adding more after setting the money to 3 will do this:

you will get this output:

both ds.getUserData(); and ds.setUserData(); are promises, so you might want to add await before them.

You can also add another argument for the user's id. this will be at the end, like this:

You can also get all user data by running ds.getAllUserData(); (also a promise) it will come in a form like this:

Last updated

Was this helpful?