0
I am currently developing a chat bot for my Twitch channel. I want to implement a system of loyalty points that are distributed after a while only to those who are present in the chat, so I have to check if a user has entered or left the chat and change the values that are stored in a .json. file. json will be stored the user points and whether they are in chat or not.
This is the code:
let data = fs.readFileSync('./data/data.json');
let viewer = JSON.parse(data);
function givePoints(user){
function intervalFunc() {
if(viewer[user].online === 1){
viewer[user].points += 10;
fs.writeFileSync('./data/data.json', JSON.stringify(viewer, null, 2));
console.log("+10 Points - " + user);
};
};
setInterval(intervalFunc, 30000);
};
client.on('join', function(channel, user){
console.log("JOINED: " + user);
if(!viewer[user]){
viewer[user] = {
online: 1,
points: 0
};
};
viewer[user].online = 1;
fs.writeFileSync('./data/data.json', JSON.stringify(viewer, null, 2));
givePoints(user);
});
client.on('part', function(channel, user){
console.log("PARTED: " + user);
viewer[user].online = 0;
fs.writeFileSync('./data/data.json', JSON.stringify(viewer, null, 2));
});
This is the . json:
{
"streamelements": {
"online": 0,
"points": 0
},
"jptron": {
"online": 0,
"points": 0
}
}
The program successfully detects whether the user exists in . json and if Join or part. The problem is when someone leaves the chat and comes back in. When this happens the program distributes the points to that user more times than it should.
I added a.log console whenever the bot distributes the points: The user streamelements is another bot connected to my channel.
I wonder what I might be changing in my code for this to work the way intended.
Thank you very much. I don’t know why I didn’t think of it before, I added a clearInterval to the function and it’s working fine.
– João Ferreira