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:

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

// after
ds.setUserData({
  money: 12
});

// logging data
console.log(await ds.getUserData());

you will get this output:

{
  money: 3,
  name: 'example-person'
}

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:

await ds.setUserData({
  money: 153,
  name: 'amazing-example'
}, 'USER-ID');

// and
const data = await ds.getUserData('USER-ID');

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

[
  {
    _id: 'USER-ID', // the user's id
    data: { // the data that you game them
      money: 153,
      name: 'amazing-example'
    },
    __v: 0 // useless data
  },
  {
    _id: 'USER-ID',
    data: {
      money: 3,
      name: 'example-person'
    },
    __v: 0
  }
]

Last updated