Advertisement
XZTablets

Untitled

Aug 22nd, 2020
1,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Database {
  2.     constructor(URL) {
  3.         this.URL = URL;
  4.         this.GuildInfo = {};
  5.         this.Client;
  6.     }
  7.  
  8.     setGuildInfo(GuildID, Info) {
  9.         this.GuildInfo[GuildID] = Info;
  10.     }
  11.  
  12.     getGuilds(Callback) {
  13.         let Options = {
  14.             url: (`${this.URL}/guilds`),
  15.             method: "GET",
  16.             headers: {
  17.                 "cache-control": "no-cache",
  18.                 "x-apikey": process.env.APIKEY
  19.             }
  20.         };
  21.  
  22.         request(Options, (Error, Response, Body) => {
  23.             Callback(JSON.parse(Body));
  24.         })
  25.     }
  26.  
  27.     getGuildPrefix(GuildID) {
  28.         let Prefix = this.GuildInfo[GuildID].prefix || false;
  29.         if (Prefix) {
  30.             return Prefix
  31.         }
  32.         else {
  33.             this.updateGuilds();
  34.             return "--";
  35.         }
  36.     }
  37.  
  38.     getGuildDocumentID(GuildID) {
  39.         return this.GuildInfo[GuildID].docid || false;
  40.     }
  41.  
  42.     setGuildPrefix(GuildID, NewPrefix) {
  43.         let DocumentID = this.getGuildDocumentID(GuildID);
  44.         if (DocumentID) {
  45.             let Options = {
  46.                 method: "PUT",
  47.                 url: (`${this.URL}/guilds/${DocumentID}`),
  48.                 headers: {
  49.                     "cache-control": "no-cache",
  50.                     "x-apikey": process.env.APIKEY,
  51.                     "content-type": "application/json"
  52.                 },
  53.                 body: {prefix: NewPrefix || "--"},
  54.                 json: true
  55.             }
  56.             request(Options);
  57.             this.updateGuilds();
  58.         }
  59.     }
  60.  
  61.     updateGuilds(UseClient) {
  62.         this.getGuilds((Guilds) => {
  63.             this.Client = UseClient || this.Client;
  64.  
  65.             Guilds.forEach((Guild) => {
  66.                 let Info = {
  67.                     id: Guild.id,
  68.                     name: Guild.name,
  69.                     prefix: Guild.prefix,
  70.                     docid: Guild._id
  71.                 }
  72.                 this.setGuildInfo(Guild.id, Info);
  73.             });
  74.  
  75.             (UseClient || this.Client).guilds.cache.forEach((Guild) => {
  76.                 if (this.GuildInfo[Guild.id]) return;
  77.                 let Options = {
  78.                     url: (`${this.URL}/guilds`),
  79.                     method: "POST",
  80.                     headers: {
  81.                         "cache-control": "no-cache",
  82.                         "x-apikey": process.env.APIKEY,
  83.                         "content-type": "application/json"
  84.                     },
  85.                     body: Body,
  86.                     json: true
  87.                 }
  88.                 request(Options, (Error, Response, Body) => {
  89.                     let Info = {
  90.                         id: Guild.id,
  91.                         name: Guild.name,
  92.                         prefix: "--",
  93.                         docid: Body._id
  94.                     }
  95.                     this.setGuildInfo(Guild.id, Info);
  96.                 });
  97.             });
  98.         });
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement