Quistty
Quistty

Reputation: 13

Discord bot that counts a specific word per user Discord.js

Currently I'm making a bot that will count how many times a certain user says the word ':p', whether it's in a sentence or used by itself. When That happens I would like it to add +1 to a variable so I can use a function to check the number in the variable. With the code I have so far, I can detect when the word ':p' is used by itself but I can't detect who sent it or when its in a sentance. I tried using author.id, but that is unidentified. (code)

var numberofp = 0; 

client.on('message', (message) => {
    if(message.content == ':p') {
        message.reply('Another one (:');
        numberofp = numberofp + 1;
        console.log(numberofp);
    }
});

When I use the variable, it also says: numberofp is undefined.

Overall what I need help with is:

  1. getting the bot to identify when ':p' is used even in the middle of a sentance
  2. Then to get it to check if it is a certain person
  3. Lastly, to get it to add +1 to the variable numberofp

If you need any more code or information, just ask :D

Upvotes: 1

Views: 1085

Answers (1)

Abhay
Abhay

Reputation: 101

I would say use quick.db for counting specific word by specific user.

I'll write sample code for you:

const db = require("quick.db");
client.on('message', (message) => {
    if(message.content == ':p') {
        let count = db.fetch(`countWord_${message.guild.id}_${message.author.id}`);
        db.add(`countWord_${message.guild.id}_${message.author.id}`, count + 1);
    }
});
`
and to see how many times specific user used it then use following code:

let count = db.fetch(`countWord_${message.guild.id}_${id of user}`);
message.channel.send(count)

Upvotes: 1

Related Questions