Advertisement
Jordimario

NodeJs Processi a collaboarizione

Oct 30th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require("discord.js");
  2. var fs=require("fs");
  3. const https = require('https');
  4. // this is what we're refering to. Your client.
  5. const client = new Discord.Client();
  6. // Here we load the config.json file that contains our token and our prefix values.
  7. const config = require("../config.json");
  8.  
  9.  
  10. async function loadJSON(){
  11. //la funzione ritorna una promessa
  12. return new Promise((resolve, reject) => {
  13.        
  14.          fs.readFile('variabili.json','utf8', (err,data) => {resolve(JSON.parse(data.match(/{"persone":\d+,"process":(true|false)}/gi)))})//risolvera una lettura per un file e lo trasformera in un oggetto
  15.          
  16.     })
  17.  
  18. }
  19.  
  20. async function postJSON(jsonData){
  21. //la funzione non ritorna nulla ma aggiorna il json variabile
  22.    await fs.writeFile("variabili.json",JSON.stringify(jsonData), err => {//funzione per aggiornare il file
  23.         if(err) return console.log(err);
  24.     })
  25.  
  26. }
  27.  
  28.  
  29. client.on("ready", guild => {
  30.           console.log(`processo1 inizializzato in ${client.guilds.size} gilde`)
  31.  
  32.  
  33. });
  34.  
  35. client.on("message", async message => {
  36.          
  37.  
  38.  
  39.          /*var x = await  loadJSON() //aspetta per la risposta alla promessa oggetto
  40.           console.log(x.persone) *///pubblica il valore persone della promessa oggetto
  41.  
  42.   // This event will run on every single message received, from any channel or DM.
  43.  
  44.   if(message.author.bot) return; //non acetta messaggi inviati dai bot
  45.   if(message.content.indexOf(config.prefix) !== 0) return; //accetta solo messaggi il prfisso posto in config
  46.   // Here we separate our "command" name, and our "arguments" for the command.
  47.   // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  48.   // command = say
  49.   // args = ["Is", "this", "the", "real", "life?"]
  50.   var args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  51.   var command = args.shift().toLowerCase();
  52.  
  53.  
  54. if(command==="inizia"){
  55. message.channel.send("Processo **1** iniziato")//indica l'inizio del primo processo
  56.  
  57. pro=setInterval(async ()=>{
  58. //inizio ciclo
  59. variabili = await loadJSON();//prende il file delle variabili come oggetto
  60.    
  61. if(variabili.persone>0){
  62.     if(!variabili.process){//controllo se è aperto il canale
  63.             variabili.process=true //chiusura sezione critica
  64.             variabili.persone--//leva un biglietto
  65.  
  66.             message.channel.send("**P1:** biglietti rimanenti: "+variabili.persone);
  67.  
  68.             await postJSON(variabili) //apporto le modifiche in modo permanenete
  69.  
  70.         //}
  71.     }
  72.     return 0;
  73. }
  74. else{
  75.     console.log("stop!");
  76.     message.channel.send("il processo è finito");
  77.     return clearInterval(pro);
  78. }
  79.    
  80. },1000)
  81.     //console.log(`**Processo 1:** ho Consegnato un biglietto, *Biglietti mancanti: ${variabili.persone}*`) //messaggio per avvisare l'utente del biglietto
  82.  
  83.  
  84. }
  85.  
  86.                                       });
  87. client.login(config.token);
  88.  
  89.  
  90.  
  91. ///---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SECONDO PROCESSO---------------------------------------------------------------------------------------------------//
  92.  
  93. const Discord = require("discord.js");
  94. var fs=require("fs");
  95. const https = require('https');
  96. // this is what we're refering to. Your client.
  97. const client = new Discord.Client();
  98. // Here we load the config.json file that contains our token and our prefix values.
  99. const config = require("../config.json");
  100.  
  101.  
  102. async function loadJSON(){
  103. //la funzione ritorna una promessa
  104. return new Promise((resolve, reject) => {
  105.        
  106.          fs.readFile('variabili.json','utf8', (err,data) => {resolve(JSON.parse(data.match(/{"persone":\d+,"process":(true|false)}/gi)))})//risolvera una lettura per un file e lo trasformera in un oggetto
  107.          
  108.     })
  109.  
  110. }
  111.  
  112. async function postJSON(jsonData){
  113. //la funzione non ritorna nulla ma aggiorna il json variabile
  114.    await fs.writeFile("variabili.json",JSON.stringify(jsonData), err => {//funzione per aggiornare il file
  115.         if(err) return console.log(err);
  116.     })
  117.  
  118. }
  119.  
  120.  
  121. client.on("ready", guild => {
  122.           console.log(`processo1 inizializzato in ${client.guilds.size} gilde`)
  123.  
  124.  
  125. });
  126.  
  127. client.on("message", async message => {
  128.          
  129.  
  130.  
  131.          /*var x = await  loadJSON() //aspetta per la risposta alla promessa oggetto
  132.           console.log(x.persone) *///pubblica il valore persone della promessa oggetto
  133.  
  134.   // This event will run on every single message received, from any channel or DM.
  135.  
  136.   if(message.author.bot) return; //non acetta messaggi inviati dai bot
  137.   if(message.content.indexOf(config.prefix) !== 0) return; //accetta solo messaggi il prfisso posto in config
  138.   // Here we separate our "command" name, and our "arguments" for the command.
  139.   // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  140.   // command = say
  141.   // args = ["Is", "this", "the", "real", "life?"]
  142.   var args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  143.   var command = args.shift().toLowerCase();
  144.  
  145.  
  146. if(command==="inizia"){
  147. message.channel.send("Processo **2** iniziato")//indica l'inizio del primo processo
  148.  
  149. pro=setInterval(async ()=>{
  150. //inizio ciclo
  151. variabili = await loadJSON();//prende il file delle variabili come oggetto
  152.    
  153. if(variabili.persone>0){
  154.     if(variabili.process){//controllo se è aperto il canale
  155.             variabili.process=false //chiusura sezione critica
  156.             variabili.persone--//leva un biglietto
  157.  
  158.             message.channel.send("**P2:** biglietti rimanenti: "+variabili.persone);
  159.  
  160.             await postJSON(variabili) //apporto le modifiche in modo permanenete
  161.  
  162.         //}
  163.     }
  164.     return 0;
  165. }
  166. else{
  167.     console.log("stop!");
  168.     message.channel.send("il processo è finito");
  169.     return clearInterval(pro);
  170. }
  171.    
  172. },1000)
  173.     //console.log(`**Processo 1:** ho Consegnato un biglietto, *Biglietti mancanti: ${variabili.persone}*`) //messaggio per avvisare l'utente del biglietto
  174.  
  175.  
  176. }
  177.  
  178.                                       });
  179. client.login(config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement