Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

86 linhas
1.8 KiB

  1. const { GoogleSpreadsheet: googleSheets } = require('google-spreadsheet');
  2. let creds = require('./creds');
  3. let sheetsConfig = require('./sheetsConfig');
  4. module.exports = {
  5. doc: null,
  6. sheet: null,
  7. records: null,
  8. init: async function () {
  9. if (sheetsConfig.roles) {
  10. this.update = function () {};
  11. this.onGetRows(null, sheetsConfig.roles);
  12. return;
  13. }
  14. this.doc = new googleSheets(sheetsConfig.sheetId);
  15. await this.doc.useServiceAccountAuth(creds);
  16. await this.loadInfo();
  17. },
  18. loadInfo: async function () {
  19. await this.doc.loadInfo();
  20. this.onGetInfo();
  21. },
  22. onGetInfo: function () {
  23. this.sheet = this.doc.sheetsByIndex[0];
  24. if (!this.sheet) {
  25. setTimeout(this.loadInfo.bind(this), 300000);
  26. return;
  27. }
  28. this.update();
  29. },
  30. getRecord: function (name) {
  31. return (this.records || []).find(r => (r.username === name));
  32. },
  33. onGetRows: function (err, rows) {
  34. if (rows) {
  35. try {
  36. let records = (rows || []).map(function (r) {
  37. let o = {};
  38. Object.keys(r).forEach(function (p) {
  39. if (['id', 'app:edited', '_links', '_xml', 'save', 'del'].indexOf(p) > -1)
  40. return;
  41. o[p] = r[p];
  42. });
  43. o.messageStyle = o.messagestyle;
  44. delete o.messagestyle;
  45. o.messagePrefix = o.messageprefix;
  46. delete o.messageprefix;
  47. o.extraStashSlots = ~~o.extrastashslots;
  48. if (typeof (o.items) === 'string')
  49. o.items = JSON.parse(o.items || '[]');
  50. if (typeof (o.skins) === 'string')
  51. o.skins = JSON.parse(o.skins || '[]');
  52. return o;
  53. });
  54. this.records = records;
  55. } catch (e) {}
  56. }
  57. setTimeout(this.update.bind(this), 300000);
  58. },
  59. update: async function () {
  60. try {
  61. const records = await this.sheet.getRows();
  62. this.onGetRows(null, records);
  63. } catch (e) {
  64. setTimeout(this.update.bind(this), 300000);
  65. }
  66. }
  67. };