Search code examples
discorddiscord.jsquick.db

how to count how many times a command is used [discord.js]


Simply when people use the tutorial !ping command I want to count how many times and show it back in chat. like 'ping has been used this many times' I found something talking about quick.db but still don't know a lot. currently, the messages show as [This has been used This NaN times!!!]

module.exports = {
  name: 'ping',
  description: 'Ping!',
  execute (message, args, ) {
    const db = require('quick.db')
    var times = []
    db.set('times', {hello: 'hi'})
    db.add('times.used', 1)
      let timesused = times.used + 1;
      message.reply('pong');
      message.channel.send(`This has been used This ${timesused} times!!!`);
  },
};

Solution

  • The right solution for what you are trying to do is simple. Just do this.

    const db = require('quick.db');
    
    module.exports = {
      name: 'ping',
      description: 'Ping!',
      execute (message, args) {
        db.add('times.ping', 1); // Adding an amount of one to the countor for the ping command
    
        const timesUsed = db.get('times.ping'); // Getting the amount of uses 
    
        message.reply('pong!');
        message.channel.send('This command has been used '+timesUsed+' times!');
      },
    };
    

    For a better explanation please read the Documentation of Quick.db.