var ComfyJS = require("comfy.js");
var fs = require('fs');
const dataBuffer = fs.readFileSync('database.json');
const dataJSON = dataBuffer.toString();
const scoreBoard = JSON.parse(dataJSON);
ComfyJS.onChat = (user, message, flags, self, extra) => {
for (let i = 0; i < scoreBoard.length; i++) {
if (scoreBoard[i].name == user) {
console.log('The name already exist');
}
else{
scoreBoard.push({name:user,score:message});
}
}
var data = JSON.stringify(scoreBoard);
fs.writeFile('database.json', data, function (err) {
if (err) {
console.log('There has been an error saving your configuration data.');
console.log(err.message);
return;
}
console.log('Configuration saved successfully.')
});
}
Hi I'm new to code and I'd like to build a twitch bot and I want to save my data on a JSON file. ComfyJS.onchat triggers when somebody types something on chat and I want to take their name and message(score) as value and save it on my database but I need to save them one by one so if the name already exists in JSON file I don't want to add it. How should I fix it?
ComfyJS.onChat = (user, message, flags, self, extra) => {
const exists = scoreBoard.find(fn => fn.name === user)
if (exists) return;
scoreBoard.push({
name: user,
score: message
});
var data = JSON.stringify(scoreBoard);
fs.writeFile('database.json', data, function(err) {
if (err) {
console.log('There has been an error saving your configuration data.');
console.log(err.message);
return;
}
console.log('Configuration saved successfully.')
});
}