Add files via upload

This commit is contained in:
Fernando Méndez 2021-03-12 18:39:53 +01:00 committed by GitHub
parent e561c6295d
commit 00ff7e2b89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,21 +28,11 @@ const translate = require('@vitalets/google-translate-api'); //https://www.npmjs
// To overwrite the users.json file: // To overwrite the users.json file:
const fs = require('fs'); const fs = require('fs');
// File with user information: // File with user information:
try { //const usersFile = require("./users.json");
if (!(fs.existsSync("./users.json"))) {
fs.writeFile('./users.json', JSON.stringify({users:[]}), 'utf8', function(err) {
if(err) return console.log(err);
console.log("Users file not found. A new one has been created.");
});
} else {
console.log("Users file load successfully.");
}
} catch(err) {
console.log("Error creating user file: \n\n" + err);
}
const usersFile = require("./users.json");
// Stores user IDs read from "usuers.json": // Stores user IDs read from "usuers.json":
const _users = JSON.parse(JSON.stringify(usersFile.users)); //const _users = JSON.parse(JSON.stringify(usersFile.users));
const _users = createUsersFile();
bot.on('polling_error', function(error){ bot.on('polling_error', function(error){
console.log(error); console.log(error);
@ -254,3 +244,24 @@ function indexOfArray(_user){
return index; return index;
} }
function createUsersFile(){
try {
if (!(fs.existsSync("./users.json"))) {
//fs.createWriteStream('./users.json');
fs.writeFile('./users.json', JSON.stringify({users:[]}), 'utf8', function(err) {
if(err) console.log(err);
else console.log("Users file not found. A new one has been created.");
});
return [];
} else {
// File with user information:
const usersFile = require("./users.json");
console.log("Users file load successfully.");
return JSON.parse(JSON.stringify(usersFile.users));
}
} catch(err) {
console.log("Error creating user file: \n\n" + err);
return [];
}
}