So, I am having some issues with functions and passing variables through them using an interval. While I can get values to pass through I can;t quite seem to get them updated.
Here is the main function:
tradeSkill: (msg, what, type, amount, userData, stats) => {
stats.actions--;
if (stats.actions <= 0) {
// The report DM
userData.send({ embed: {
color: 3447003,
title: `Farming Report For ${type}`,
fields: [{
name: 'Stats',
value:
'Gathered: ' + stats.yield +
'\nEXP Gained: ' + stats.exp,
},
],
},
});
//Reset stats for Report
stats = {
exp: 0,
actions: amount,
yield: 0,
};
}
}
Here is how I'm passing it though:
autos(msg, what, type, amount, userData, stats);
Here is the autos function:
function autos(msg, what, type, amount, userData, stats) {
if (userData.interval === undefined || userData.interval === false) {
userData.interval = setInterval(() => {
skills.tradeSkill(msg, what, type, amount, userData, stats);
userData.intervalTime = Date.now();
}, 6000);
skills.tradeSkill(msg, what, type, amount, userData, stats);
} else {
var timing = userData.intervalTime + 5900 - Date.now();
if (timing < 0) {
timing = 0;
}
// Waits for current to end, then updates it.
setTimeout(() => {
clearInterval(userData.interval);
userData.interval = setInterval(() => {
skills.tradeSkill(msg, what, type, amount, userData, stats);
userData.intervalTime = Date.now();
}, 6000);
skills.tradeSkill(msg, what, type, amount, userData, stats);
}, timing);
}
}
My problem:
In the first function, when stats.actions
equal or less than 0 it will send a DM (using discord.js, but this isn't a discord.js problem so don't worry bout that). And then reset it back to the specified amount (which is always > 0). However, after it hits 0, it keeps sending DMs and will not set it to what is supposed to be properly. If I do console.log(stats.actions)
before the if statement and inside of it, it always produces 0 before the if statement and will always produce a 10 inside the if statement (when it triggers the first time)
If might be hard to understand without the full code, however as its near total 1k lines, I am going to link it to paste bin:
Main Function: https://pastebin.com/XmmRqgsT
Secondary with the autos: https://pastebin.com/5xcG1qyx