Send a message only once when a condition is met discord.js

I want to make a quest system for my discord.js bot and im making some code that runs everytime a user messages.

async function quests() {

    const {
        message,
        bot,
        Discord
    } = require('../index')

    const Yen = require('../models/yen')

    var user = message.author;

    const yenData = await Yen.findOne({
        guildID: bot.guilds.cache.get(message.guild.id).id,
        userID: user.id
    }, (err, data) => {
        if (err) console.log(err)

        if (!data) return;
    })

}

module.exports.quests = quests;

This code right now runs every time someone messages but I want it so if a condition is met like if a user gets 200 coins they get an extra reward of maybe a weapon.

I can’t just do:

if(coins === 200) {
//give weapon script
}

because then it would keep giving the person a weapon every time he messages until he hits 201+ coins. How can I make it so it only sends once?

Send the weapon once? That’s what you want?

Yes, because the code runs every time a message is executed so I want it so it only gives it once when the condition is met, not multiple times.

Wait, so when you message once, you get multiple weapons right? Or every message gives you the weapon? Sorry I am bit confused.

Well you either have to take out coins or store a data that tells server that user already has a weapon and not give it again. If it’s the second case.

I already i have a mongodb database for all that. What im saying is that the code that i showed is in the message function so it’s run everytime a message is sent in the server. I want to make it so it finds the message authors data (if theres no data for the user its returned) and checks if they have 200 coins (if not it returns). If they have 200 coins then it will give them a weapon or something. The thing is, since it’s checked every message, if they message again right after with 200 coins, it’ll see that they have 200 coins and give them another weapon, I don’t want that to happen. I want it so it’s only a one time thing.

Then store somewhere, and check if they have a weapon already.

if (user.hasWeapon()) return;
if (!user.hasCurrency(200)) return;
1 Like

Is that the only way i can do this?

Well how you want a code to know if user has the weapon already? You have to store it somewhere…

Welp, that’s gonna be a pain, thanks anyways. I might come back to the quest system later, lol. I already knew i had to store it somewhere but i wanted to see if there was any other way that would be simpler…

Yes I hate databases they are true pain. I wish I could just do this.

var database = require("database.js");
var userData = database.read(user.id);
userData.currency += 1;
database.save(user.id, userData);
3 Likes

Global object noises haha. I still don’t understand why global objects are evil…

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.