'How to track the number of command uses and mentions by the user?

I just wanted to know if there's a way to count how many times a command has been used in my Discord server like on screenshot. I'm new with coding. I used discord.js 12.1.1 Thank you in advance!

enter image description here

upd. for example, user1 hugs user2.

User1 hugged N times

User2 has been hugged N times

how can i make count how many times the message author hugs users?

I tried to insert the count into my finished code, but it only takes into account the mentioned user.

Even if i dont mention anyone, the bot thinks i mention myself..

i tried differently, but my brain is already exploding.. 2 days i cant understand my mistakes

i changed let member to different values but it works adequately only all at once.. -_-

dont laugh much, i try(

const Discord = module.require("discord.js");
const fs = require("fs");
const config = require(`../config.json`)
const hugs = require('../db.json');


module.exports.run = async (bot, message, args) => {
 let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
    let msg =
      member === message.member
        ? `${message.author} hugs everyone`
        : `${message.author} hugged ${member}`;
    let gif = [
      "IRUb7GTCaPU8E",
      "u9BxQbM5bxvwY",
      "3EJsCqoEiq6n6", 
    ];
    let selected = gif[Math.floor(Math.random() * gif.length)];

    let id = member.id || message.author.id
    let hugCount = hugs[id];

    if (!hugCount) {
      hugs[id] = 1;

    let embed = new Discord.MessageEmbed() 
    .setDescription(`${msg}`)
      .setColor("RANDOM")
      .setFooter(`${member.user.username} hugged for the first time.`) 
      .setImage(`https://media.giphy.com/media/${selected}/giphy.gif`);


      message.channel.send(embed);

    } else {
      hugCount = (hugs[id] = hugs[id] + 1);

      let embed = new Discord.MessageEmbed() 
      .setDescription(`**${msg}**`)
        .setColor("RANDOM")
        .setFooter(`${config.prefix}${module.exports.help.name} | ${member.user.username} has been hugged ${hugCount} times.`) 
        .setImage(`https://media.giphy.com/media/${selected}/giphy.gif`);

        message.channel.send(embed);
      return message.delete()
  }

fs.writeFileSync(
  "./db.json",
  JSON.stringify(hugs),
  (err) => console.log(err)
);

}

module.exports.help = {
  name: "hugg"
};

and if possible, help me in this - how to make that the user write a message (for example, !hug user2 "hello") and the bot display message in an embed



Solution 1:[1]

I guess you can just have some database where you can store ID of users and values how many times somebody executed you command.

And every time someone uses command you just increase value for that person ID by one.

Edit #1

I think that this code

 hugCount = (hugs[id] = hugs[id] + 1);

can be reduce to this:

 hugCount = (hugs[id] + 1);

And the issue I think is here... Basically you have to know who you need to get. Here you are getting so many thing what you cannot be sure what is inside.

let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;

So I would suggest to change it to different variables... for example let author and let mentionedUser and you need to get both of them in different variables to save both of their counts.

That means you gonna have user1 who types !hug user2 "Hello" you need to store in let author = user1 and for let mentionedUser = user2
And for each user you need to have 2 different count values in database (one for how many times they hugged someone, second for how many times they have been hugged by other person)

I hope this is gonna be helpful.

Solution 2:[2]

I use Redis along side Heroku, to track how many times a command has been used,

I tried to insert the count into my finished code, but it only takes into account the mentioned user.

let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;

Whose id are you trying to retrieve here?

Even if i dont mention anyone, the bot thinks i mention myself.. let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;

message.member is the reason that even when you dont mention anyone, it will get your id.

how to make that the user write a message (for example, !hug user2 "hello") and the bot display message in an embed

Do you mean the "hello" should be embedded?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 StoneCoding