{ if (!message.guild) return; if (messag" /> { if (!message.guild) return; if (messag" /> { if (!message.guild) return; if (messag"/>

Earn Credits for Each Message Sent Bot Discord.js

303 Views Asked by At

I need help with my Discord bot, I am using discord.js

So I made this:

client.on("message", async(message) => {
  if (!message.guild) return;
  if (message.author.bot) return;
  if (mentionn) {

    const randomAmountOfCredits = Math.floor(Math.random() * 29) + 1; // Min 1, Max 30
    (message.author.id, message.guild.id, randomAmountOfCredits);
    if (collected.first().content === number) {
      m.delete();
      collected.first().delete();
      credits[mentionn.id].credits += (+randomAmountOfCredits);
      fs.writeFile(path, JSON.stringify(credits, null, 5), function(err) {
        if (err) console.log(err)
      });

      if (collected.first().content !== number) {
        return m.delete();
      } else if (!mentionn) {
        credits[author].credits += (+randomAmountOfCredits);
        fs.writeFile(path, JSON.stringify(credits, null, 5), function(err) {
          if (err) console.log(err)
        });
      }
    }
  }
});

It's like earning credits by each message sent, but I am not earning any credits, there are no errors on the console:

Thats the console

And here I must get credits, but nothing happens: Thats the results

I am a starter, thank you

1

There are 1 best solutions below

2
deb On

I didn't understand a line of this code snippet, and I didn't understand your answer to Pascal Stockert's question, and neither did I understand what a collected (from a collector?) var's doing here. but I'll try my best. This is how I would add this feature, feel free to mess around with this code and add as many conditions as you want.

client.on("message", async msg => {
    // First, generate a random number
    const newXP = Math.ceiling(Math.random() * 30);
    // Read the existing XPs
    const file = fs.readFileSync("xp.json");
    // Add some XP to the author of the message
    file[msg.author.id].credits += newXP;
    // Write new XP file
    fs.writeFileSync("xp.json", JSON.stringify(file, null, 4));
    // Done.
});

I didn't add it, but be careful, it looks like you don't handle the situation where file[msg.author.id] doesn't exist.