daily pastebin goal
50%
SHARE
TWEET

Database Handler

a guest May 17th, 2018 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Airtable = require('airtable');
  2. var Base = new Airtable({apiKey: 'keyrYtsUCwQBo48Pz'}).base('appwbA5iy1LHLUaKr');
  3. const rblxJs = require('rblx-js')
  4. var rbx = new rblxJs();
  5.  
  6. let GetRecords = function() {
  7.     return new Promise(function(Resolve, Reject){
  8.         const RecordsList = [];
  9.         var index = 0
  10.  
  11.         Base('Database').select({
  12.             maxRecords: 99999,
  13.             view: "Grid view"
  14.         }).eachPage(function page(records, fetchNextPage) {
  15.  
  16.         records.forEach(function(record) {
  17.             var FieldsTable = [];
  18.  
  19.             FieldsTable[Object.entries(record.fields)[0][0]] = Object.entries(record.fields)[0][1];
  20.             FieldsTable[Object.entries(record.fields)[1][0]] = Object.entries(record.fields)[1][1];
  21.             FieldsTable[Object.entries(record.fields)[2][0]] = Object.entries(record.fields)[2][1];
  22.             FieldsTable[Object.entries(record.fields)[3][0]] = Object.entries(record.fields)[3][1];
  23.             FieldsTable[Object.entries(record.fields)[4][0]] = Object.entries(record.fields)[4][1];
  24.             FieldsTable[Object.entries(record.fields)[5][0]] = Object.entries(record.fields)[5][1];
  25.             FieldsTable[Object.entries(record.fields)[6][0]] = Object.entries(record.fields)[6][1];
  26.  
  27.             RecordsList[index] = [FieldsTable, record.getId()];
  28.             index++;
  29.         });
  30.  
  31.             fetchNextPage();
  32.         });
  33.  
  34.         setTimeout(function() {Resolve(RecordsList)}, 2500);
  35.     });
  36. };
  37.  
  38. let GetPlayerRecord = function(IdArray){
  39.     return new Promise(function(Resolve, Reject) {
  40.         let Id
  41.         let ResolveData = false
  42.  
  43.         if (IdArray[1] == null){
  44.             rbx.getUserByUsername(IdArray[0]).then(user=>{
  45.                 Id = user.userId + 0;
  46.             });
  47.  
  48.         } else {
  49.             Id = IdArray[1] + 0;
  50.         };
  51.  
  52.         setTimeout(function() {
  53.             GetRecords().then(function(RecordsList){
  54.                 for (var i in RecordsList) {
  55.                     if (RecordsList[i][0]['UserId'] == Id) {
  56.                         ResolveData = RecordsList[i];
  57.                         console.log('Player found in Database');
  58.                         break;
  59.                     };
  60.                 };
  61.              });
  62.         }, 1500);
  63.  
  64.         setTimeout(function() {
  65.             console.log(ResolveData);
  66.             Resolve(ResolveData);
  67.         }, 4750);
  68.     });
  69. };
  70.  
  71.  
  72. function UpdateRecord(IdArray, Data){
  73.     GetPlayerRecord(IdArray).then(function(PlayerRecord){
  74.         let Id
  75.  
  76.         if (IdArray[1] == null){
  77.             rbx.getUserByUsername(IdArray[0]).then(user=>{
  78.                 Id = user.userId;
  79.             });
  80.  
  81.         } else {
  82.             Id = IdArray[1];
  83.         };
  84.  
  85.         setTimeout(function() {
  86.             if (PlayerRecord == false) {
  87.                 console.log('Creating');
  88.                 Base('Database').create({
  89.                     "UserId": Id,
  90.                     "Time": Data['Time'],
  91.                     "Reason": Data['Reason'],
  92.                     "Status": Data['Status'],
  93.                     "Offenses": Data['Offenses'],
  94.                     "Whitelisted": Data['Whitelisted'],
  95.                     "Blacklisted": Data['Blacklisted']
  96.                 });
  97.             } else {
  98.                 console.log('Updating');
  99.                 Base('Database').update(PlayerRecord[1], {
  100.                     "UserId": Id,
  101.                     "Time": Data['Time'],
  102.                     "Reason": Data['Reason'],
  103.                     "Status": Data['Status'],
  104.                     "Offenses": Data['Offenses'],
  105.                     "Whitelisted": Data['Whitelisted'],
  106.                     "Blacklisted": Data['Blacklisted']
  107.                 });
  108.             };
  109.         }, 1500);
  110.     });
  111. };
  112.  
  113. function Ban(Username, Reason, Time) {
  114.     var Data = [];
  115.  
  116.     GetPlayerRecord([Username, null]).then(function(PlayerRecord){
  117.         if (Time == "Perma") {
  118.             Data['Time'] = 0.0;
  119.  
  120.         } else {
  121.             Data['Time'] = Time;
  122.         };
  123.  
  124.         Data['Reason'] = Reason;
  125.         Data['Status'] = "Banned";
  126.  
  127.         if (PlayerRecord != false) {
  128.             Data['Offenses'] = PlayerRecord[0]['Offenses'] + 1;
  129.             Data['Whitelisted'] = PlayerRecord[0]['Whitelisted'];
  130.             Data['Blacklisted'] = PlayerRecord[0]['Blacklisted'];
  131.         } else {
  132.             Data['Offenses'] = 1;
  133.             Data['Whitelisted'] = "False";
  134.             Data['Blacklisted'] = "False";
  135.         };
  136.  
  137.         setTimeout(function() {
  138.             UpdateRecord([Username, null], Data);
  139.         }, 2000);
  140.     });
  141. };
  142.  
  143.  
  144.  
  145. function Unban(Username, Reason) {
  146.     var Data = [];
  147.  
  148.     GetPlayerRecord([Username, null]).then(function(PlayerRecord){
  149.  
  150.         Data['Reason'] = Reason;
  151.         Data['Status'] = "Unbanned";
  152.         Data['Time'] = 1.0
  153.         if (PlayerRecord != false) {
  154.             Data['Offenses'] = PlayerRecord[0][4];
  155.             Data['Whitelisted'] = PlayerRecord[0]['Whitelisted'];
  156.             Data['Blacklisted'] = PlayerRecord[0]['Blacklisted'];
  157.         } else {
  158.             Data['Offenses'] = 0;
  159.             Data['Whitelisted'] = "False";
  160.             Data['Blacklisted'] = "False";
  161.         };
  162.  
  163.         setTimeout(function() {
  164.             UpdateRecord(Username, Data);
  165.         }, 2000);
  166.     });
  167. };
  168.  
  169. function Whitelist(Username, Reason) {
  170.     var Data = [];
  171.  
  172.     GetPlayerRecord([Username, null]).then(function(PlayerRecord){
  173.  
  174.         Data['Reason'] = Reason;
  175.         Data['Status'] = "Unbanned";
  176.         Data['Whitelisted'] = "True";
  177.         Data['Blacklisted'] = "False";
  178.  
  179.         if (PlayerRecord != false) {
  180.             Data['Offenses'] = PlayerRecord[0]['Offenses'];
  181.  
  182.         } else {
  183.             Data['Offenses'] = 0;
  184.             Data['Whitelisted'] = "True";
  185.             Data['Blacklisted'] = "False";
  186.         };
  187.  
  188.         setTimeout(function() {
  189.             UpdateRecord(Username, Data);
  190.         }, 2000);
  191.     });
  192. };
  193.  
  194. function Blacklist(Username, Reason) {
  195.     var Data = [];
  196.  
  197.     GetPlayerRecord([Username, null]).then(function(PlayerRecord){
  198.  
  199.         Data['Reason'] = Reason;
  200.         Data['Status'] = "Banned";
  201.         Data['Whitelisted'] = "False";
  202.         Data['Blacklisted'] = "True";
  203.  
  204.         if (PlayerRecord != false) {
  205.             Data['Offenses'] = PlayerRecord[0]['Offenses'];
  206.  
  207.         } else {
  208.             Data['Offenses'] = 0;
  209.             Data['Whitelisted'] = "False";
  210.             Data['Blacklisted'] = "True";
  211.         };
  212.  
  213.         setTimeout(function() {
  214.             UpdateRecord(Username, Data);
  215.         }, 2000);
  216.     });
  217. };
  218.  
  219. while (true) {
  220.     var Seconds = new Date().getTime()/1000
  221.     var Data = [];
  222.  
  223.     for (var i in RecordsList) {
  224.         if (RecordsList[i][0][0] >= Seconds && (RecordsList[i][0][0] != 1.0 || RecordsList[i][0][0] != 0.0)) {
  225.             Data['Reason'] = Reason;
  226.             Data['Status'] = "Unbanned";
  227.             Data['Time'] = 1.0;
  228.             Data['Offenses'] = RecordsList[i][0][4];
  229.             Data['Whitelisted'] = RecordsList[i][0][5];
  230.             Data['Blacklisted'] = RecordsList[i][0][6];
  231.             UpdateRecord([null, RecordsList[i][0][1]], Data)
  232.         };
  233.     };
  234. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top