Advertisement
Dezet_

Bot.js - Poradnik By Dezet.gfx#1023

Jul 13th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Discord Bot Maker Bot
  3.  * Version 2.0.1
  4.  * Robert Borghese
  5.  */
  6.  
  7. const DBM = {};
  8. const DiscordJS = DBM.DiscordJS = require('discord.js');
  9. const express = require('express');
  10. const keepalive = require('express-glitch-keepalive');
  11.  
  12. const app = express();
  13.  
  14. app.use(keepalive);
  15.  
  16. app.get('/', (req, res) => {
  17. res.json('Poradnik By Dezet.gfx#1023');
  18. });
  19. app.get("/", (request, response) => {
  20. response.sendStatus(200);
  21. });
  22. app.listen(process.env.PORT);
  23.  
  24. //---------------------------------------------------------------------
  25. // Bot
  26. // Contains functions for controlling the bot.
  27. //---------------------------------------------------------------------
  28.  
  29. const Bot = DBM.Bot = {};
  30.  
  31. Bot.$cmds = {}; // Normal commands
  32. Bot.$icds = []; // Includes word commands
  33. Bot.$regx = []; // Regular Expression commands
  34. Bot.$anym = []; // Any message commands
  35. Bot.$evts = {}; // Events
  36.  
  37. Bot.bot = null;
  38.  
  39. Bot.init = function() {
  40.     this.initBot();
  41.     this.reformatData();
  42.     this.initEvents();
  43.     this.login();
  44. };
  45.  
  46. Bot.initBot = function() {
  47.     this.bot = new DiscordJS.Client();
  48. };
  49.  
  50. Bot.reformatData = function() {
  51.     this.reformatCommands();
  52.     this.reformatEvents();
  53. };
  54.  
  55. Bot.reformatCommands = function() {
  56.     const data = Files.data.commands;
  57.     if(!data) return;
  58.     this._caseSensitive = Boolean(Files.data.settings.case === 'true');
  59.     for(let i = 0; i < data.length; i++) {
  60.         const com = data[i];
  61.         if(com) {
  62.             switch(com.comType) {
  63.                 case '1':
  64.                     this.$icds.push(com);
  65.                     break;
  66.                 case '2':
  67.                     this.$regx.push(com);
  68.                     break;
  69.                 case '3':
  70.                     this.$anym.push(com);
  71.                     break;
  72.                 default:
  73.                     if(this._caseSensitive) {
  74.                         this.$cmds[com.name] = com;
  75.                         if(com._aliases) {
  76.                             const aliases = com._aliases;
  77.                             for(let j = 0; j < aliases.length; j++) {
  78.                                 this.$cmds[aliases[j]] = com;
  79.                             }
  80.                         }
  81.                     } else {
  82.                         this.$cmds[com.name.toLowerCase()] = com;
  83.                         if(com._aliases) {
  84.                             const aliases = com._aliases;
  85.                             for(let j = 0; j < aliases.length; j++) {
  86.                                 this.$cmds[aliases[j].toLowerCase()] = com;
  87.                             }
  88.                         }
  89.                     }
  90.                     break;
  91.             }
  92.         }
  93.     }
  94. };
  95.  
  96. Bot.reformatEvents = function() {
  97.     const data = Files.data.events;
  98.     if(!data) return;
  99.     for(let i = 0; i < data.length; i++) {
  100.         const com = data[i];
  101.         if(com) {
  102.             const type = com['event-type'];
  103.             if(!this.$evts[type]) this.$evts[type] = [];
  104.             this.$evts[type].push(com);
  105.         }
  106.     }
  107. };
  108.  
  109. Bot.initEvents = function() {
  110.     this.bot.on('ready', this.onReady.bind(this));
  111.     this.bot.on('message', this.onMessage.bind(this));
  112.     Events.registerEvents(this.bot);
  113. };
  114.  
  115. Bot.login = function() {
  116.     this.bot.login(Files.data.settings.token);
  117. };
  118.  
  119. Bot.onReady = function() {
  120.     if(process.send) process.send('BotReady');
  121.     console.log('Bot is ready!');
  122.     this.restoreVariables();
  123.     this.preformInitialization();
  124. };
  125.  
  126. Bot.restoreVariables = function() {
  127.     Files.restoreServerVariables();
  128.     Files.restoreGlobalVariables();
  129. };
  130.  
  131. Bot.preformInitialization = function() {
  132.     const bot = this.bot;
  133.     if(this.$evts["1"]) {
  134.         Events.onInitialization(bot);
  135.     }
  136.     if(this.$evts["3"]) {
  137.         Events.setupIntervals(bot);
  138.     }
  139. };
  140.  
  141. Bot.onMessage = function(msg) {
  142.     if(!msg.author.bot) {
  143.         try {
  144.             if(!this.checkCommand(msg)) {
  145.                 this.onAnyMessage(msg);
  146.             }
  147.         } catch(e) {
  148.             console.error(e);
  149.         }
  150.     }
  151. };
  152.  
  153. Bot.checkCommand = function(msg) {
  154.     let command = this.checkTag(msg.content);
  155.     if(command) {
  156.         if(!this._caseSensitive) {
  157.             command = command.toLowerCase();
  158.         }
  159.         const cmd = this.$cmds[command];
  160.         if(cmd) {
  161.             Actions.preformActions(msg, cmd);
  162.             return true;
  163.         }
  164.     }
  165.     return false;
  166. };
  167.  
  168. Bot.checkTag = function(content) {
  169.     const tag = Files.data.settings.tag;
  170.     const separator = Files.data.settings.separator || '\\s+';
  171.     content = content.split(new RegExp(separator))[0];
  172.     if(content.startsWith(tag)) {
  173.         return content.substring(tag.length);
  174.     }
  175.     return null;
  176. };
  177.  
  178. Bot.onAnyMessage = function(msg) {
  179.     this.checkIncludes(msg);
  180.     this.checkRegExps(msg);
  181.     if(!msg.author.bot) {
  182.         if(this.$evts["2"]) {
  183.             Events.callEvents("2", 1, 0, 2, false, '', msg);
  184.         }
  185.         const anym = this.$anym;
  186.         for(let i = 0; i < anym.length; i++) {
  187.             if(anym[i]) {
  188.                 Actions.preformActions(msg, anym[i]);
  189.             }
  190.         }
  191.     }
  192. };
  193.  
  194. Bot.checkIncludes = function(msg) {
  195.     const text = msg.content;
  196.     if(!text) return;
  197.     const icds = this.$icds;
  198.     const icds_len = icds.length;
  199.     for(let i = 0; i < icds_len; i++) {
  200.         if(icds[i] && icds[i].name) {
  201.             if(text.match(new RegExp('\\b' + icds[i].name + '\\b', 'i'))) {
  202.                 Actions.preformActions(msg, icds[i]);
  203.             } else if(icds[i]._aliases) {
  204.                 const aliases = icds[i]._aliases;
  205.                 const aliases_len = aliases.length;
  206.                 for(let j = 0; j < aliases_len; j++) {
  207.                     if(text.match(new RegExp('\\b' + aliases[j] + '\\b', 'i'))) {
  208.                         Actions.preformActions(msg, icds[i]);
  209.                         break;
  210.                     }
  211.                 }
  212.             }
  213.         }
  214.     }
  215. };
  216.  
  217. Bot.checkRegExps = function(msg) {
  218.     const text = msg.content;
  219.     if(!text) return;
  220.     const regx = this.$regx;
  221.     const regx_len = regx.length;
  222.     for(let i = 0; i < regx_len; i++) {
  223.         if(regx[i] && regx[i].name) {
  224.             if(text.match(new RegExp(regx[i].name, 'i'))) {
  225.                 Actions.preformActions(msg, regx[i]);
  226.             } else if(regx[i]._aliases) {
  227.                 const aliases = regx[i]._aliases;
  228.                 const aliases_len = aliases.length;
  229.                 for(let j = 0; j < aliases_len; j++) {
  230.                     if(text.match(new RegExp('\\b' + aliases[j] + '\\b', 'i'))) {
  231.                         Actions.preformActions(msg, regx[i]);
  232.                         break;
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.     }
  238. };
  239.  
  240. //---------------------------------------------------------------------
  241. // Actions
  242. // Contains functions for bot actions.
  243. //---------------------------------------------------------------------
  244.  
  245. const Actions = DBM.Actions = {};
  246.  
  247. Actions.location = null;
  248.  
  249. Actions.server = {};
  250. Actions.global = {};
  251.  
  252. Actions.timeStamps = [];
  253.  
  254. Actions.exists = function(action) {
  255.     if(!action) return false;
  256.     return typeof(this[action]) === 'function';
  257. };
  258.  
  259. Actions.getLocalFile = function(url) {
  260.     return require('path').join(process.cwd(), url);
  261. };
  262.  
  263. Actions.getDBM = function() {
  264.     return DBM;
  265. };
  266.  
  267. Actions.callListFunc = function(list, funcName, args) {
  268.     return new Promise(function(resolve, reject) {
  269.         const max = list.length;
  270.         let curr = 0;
  271.         function callItem() {
  272.             if(curr === max) {
  273.                 resolve.apply(this, arguments);
  274.                 return;
  275.             }
  276.             const item = list[curr++];
  277.             if(item && item[funcName] && typeof(item[funcName]) === 'function') {
  278.                 item[funcName].apply(item, args).then(callItem).catch(callItem);
  279.             } else {
  280.                 callItem();
  281.             }
  282.         };
  283.         callItem();
  284.     });
  285. };
  286.  
  287. Actions.getActionVariable = function(name, defaultValue) {
  288.     if(this[name] === undefined && defaultValue !== undefined) {
  289.         this[name] = defaultValue;
  290.     }
  291.     return this[name];
  292. };
  293.  
  294. Actions.eval = function(content, cache) {
  295.     if(!content) return false;
  296.     const DBM = this.getDBM();
  297.     const tempVars = this.getActionVariable.bind(cache.temp);
  298.     let serverVars = null;
  299.     if(cache.server) {
  300.         serverVars = this.getActionVariable.bind(this.server[cache.server.id]);
  301.     }
  302.     const globalVars = this.getActionVariable.bind(this.global);
  303.     const msg = cache.msg;
  304.     const server = cache.server;
  305.     const client = DBM.Bot.bot;
  306.     const bot = DBM.Bot.bot;
  307.     const me = server ? server.me : null;
  308.     let user = '', member = '', mentionedUser = '', mentionedChannel = '', defaultChannel = '';
  309.     if(msg) {
  310.         user = msg.author;
  311.         member = msg.member;
  312.         if(msg.mentions) {
  313.             mentionedUser = msg.mentions.users.first() || '';
  314.             mentionedChannel = msg.mentions.channels.first() || '';
  315.         }
  316.     }
  317.     if(server) {
  318.         defaultChannel = server.getDefaultChannel();
  319.     }
  320.     try {
  321.         return eval(content);
  322.     } catch(e) {
  323.         console.error(e);
  324.         return false;
  325.     }
  326. };
  327.  
  328. Actions.evalMessage = function(content, cache) {
  329.     if(!content) return '';
  330.     if(!content.match(/\$\{.*\}/im)) return content;
  331.     return this.eval('`' + content.replace(/`/g,'\\`') + '`', cache);
  332. };
  333.  
  334. Actions.initMods = function() {
  335.     const fs  = require('fs');
  336.     fs.readdirSync(this.location).forEach(function(file) {
  337.         if(file.match(/\.js/i)) {
  338.             const action = require(require('path').join(this.location, file));
  339.             this[action.name] = action.action;
  340.             if(action.mod) {
  341.                 try {
  342.                     action.mod(DBM);
  343.                 } catch(e) {
  344.                     console.error(e);
  345.                 }
  346.             }
  347.         }
  348.     }.bind(this));
  349. };
  350.  
  351. Actions.preformActions = function(msg, cmd) {
  352.     if(this.checkConditions(msg, cmd) && this.checkTimeRestriction(msg, cmd)) {
  353.         this.invokeActions(msg, cmd.actions);
  354.     }
  355. };
  356.  
  357. Actions.checkConditions = function(msg, cmd) {
  358.     const isServer = Boolean(msg.guild && msg.member);
  359.     const restriction = parseInt(cmd.restriction);
  360.     const permissions = cmd.permissions;
  361.     switch(restriction) {
  362.         case 0:
  363.             if(isServer) {
  364.                 return this.checkPermissions(msg, permissions);
  365.             } else {
  366.                 return true;
  367.             }
  368.         case 1:
  369.             return isServer && this.checkPermissions(msg, permissions);
  370.         case 2:
  371.             return isServer && msg.guild.owner === msg.member;
  372.         case 3:
  373.             return !isServer;
  374.         case 4:
  375.             return Files.data.settings.ownerId && msg.author.id === Files.data.settings.ownerId;
  376.         default:
  377.             return true;
  378.     }
  379. };
  380.  
  381. Actions.checkTimeRestriction = function(msg, cmd) {
  382.     if(!cmd._timeRestriction) return true;
  383.     if(!msg.member) return false;
  384.     const mid = msg.member.id;
  385.     const cid = cmd._id;
  386.     if(!this.timeStamps[cid]) {
  387.         this.timeStamps[cid] = [];
  388.         this.timeStamps[cid][mid] = Date.now();
  389.         return true;
  390.     } else if(!this.timeStamps[cid][mid]) {
  391.         this.timeStamps[cid][mid] = Date.now();
  392.         return true;
  393.     } else {
  394.         const time = Date.now();
  395.         const diff = time - this.timeStamps[cid][mid];
  396.         if(cmd._timeRestriction <= Math.floor(diff / 1000)) {
  397.             this.timeStamps[cid][mid] = time;
  398.             return true;
  399.         } else {
  400.             const remaining = cmd._timeRestriction - Math.floor(diff / 1000);
  401.             Events.callEvents("38", 1, 3, 2, false, '', msg.member, this.generateTimeString(remaining));
  402.         }
  403.     }
  404. };
  405.  
  406. Actions.generateTimeString = function(miliSeconds) {
  407.     let remaining = miliSeconds;
  408.     const times = [];
  409.  
  410.     const days = Math.floor(remaining / 60 / 60 / 24);
  411.     if(days > 0) {
  412.         remaining -= (days * 60 * 60 * 24);
  413.         times.push(days + (days === 1 ? " day" : " days"));
  414.     }
  415.     const hours = Math.floor(remaining / 60 / 60);
  416.     if(hours > 0) {
  417.         remaining -= (hours * 60 * 60);
  418.         times.push(hours + (hours === 1 ? " hour" : " hours"));
  419.     }
  420.     const minutes = Math.floor(remaining / 60);
  421.     if(minutes > 0) {
  422.         remaining -= (minutes * 60);
  423.         times.push(minutes + (minutes === 1 ? " minute" : " minutes"));
  424.     }
  425.     const seconds = Math.floor(remaining);
  426.     if(seconds > 0) {
  427.         remaining -= (seconds);
  428.         times.push(seconds + (seconds === 1 ? " second" : " seconds"));
  429.     }
  430.  
  431.     let result = '';
  432.     if(times.length === 1) {
  433.         result = times[0];
  434.     } else if(times.length === 2) {
  435.         result = times[0] + " and " + times[1];
  436.     } else if(times.length === 3) {
  437.         result = times[0] + ", " + times[1] + ", and " + times[2];
  438.     } else if(times.length === 4) {
  439.         result = times[0] + ", " + times[1] + ", " + times[2] + ", and " + times[3];
  440.     }
  441.     return result;
  442. }
  443.  
  444. Actions.checkPermissions = function(msg, permissions) {
  445.     const author = msg.member;
  446.     if(!author) return false;
  447.     if(permissions === 'NONE') return true;
  448.     if(msg.guild.owner === author) return true;
  449.     return author.permissions.has([permissions]);
  450. };
  451.  
  452. Actions.invokeActions = function(msg, actions) {
  453.     const act = actions[0];
  454.     if(!act) return;
  455.     if(this.exists(act.name)) {
  456.         const cache = {
  457.             actions: actions,
  458.             index: 0,
  459.             temp: {},
  460.             server: msg.guild,
  461.             msg: msg
  462.         }
  463.         try {
  464.             this[act.name](cache);
  465.         } catch(e) {
  466.             this.displayError(act, cache, e);
  467.         }
  468.     } else {
  469.         console.error(act.name + " does not exist!");
  470.     }
  471. };
  472.  
  473. Actions.invokeEvent = function(event, server, temp) {
  474.     const actions = event.actions;
  475.     const act = actions[0];
  476.     if(!act) return;
  477.     if(this.exists(act.name)) {
  478.         const cache = {
  479.             actions: actions,
  480.             index: 0,
  481.             temp: temp,
  482.             server: server
  483.         }
  484.         try {
  485.             this[act.name](cache);
  486.         } catch(e) {
  487.             this.displayError(act, cache, e);
  488.         }
  489.     } else {
  490.         console.error(act.name + " does not exist!");
  491.     }
  492. };
  493.  
  494. Actions.callNextAction = function(cache) {
  495.     cache.index++;
  496.     const index = cache.index;
  497.     const actions = cache.actions;
  498.     const act = actions[index];
  499.     if(!act) {
  500.         if(cache.callback) {
  501.             cache.callback();
  502.         }
  503.         return;
  504.     }
  505.     if(this.exists(act.name)) {
  506.         try {
  507.             this[act.name](cache);
  508.         } catch(e) {
  509.             this.displayError(act, cache, e);
  510.         }
  511.     } else {
  512.         console.error(act.name + " does not exist!");
  513.     }
  514. };
  515.  
  516. Actions.getErrorString = function(data, cache) {
  517.     const type = data.permissions ? 'Command' : 'Event';
  518.     return `Error with ${type} "${data.name}", Action #${cache.index + 1}`;
  519. };
  520.  
  521. Actions.displayError = function(data, cache, err) {
  522.     const dbm = this.getErrorString(data, cache);
  523.     console.error(dbm + ":\n" + err);
  524.     Events.onError(dbm, err.stack ? err.stack : err, cache);
  525. };
  526.  
  527. Actions.getSendTarget = function(type, varName, cache) {
  528.     const msg = cache.msg;
  529.     const server = cache.server;
  530.     switch(type) {
  531.         case 0:
  532.             if(msg) {
  533.                 return msg.channel;
  534.             }
  535.             break;
  536.         case 1:
  537.             if(msg) {
  538.                 return msg.author;
  539.             }
  540.             break;
  541.         case 2:
  542.             if(msg && msg.mentions) {
  543.                 return msg.mentions.users.first();
  544.             }
  545.             break;
  546.         case 3:
  547.             if(msg && msg.mentions) {
  548.                 return msg.mentions.channels.first();
  549.             }
  550.             break;
  551.         case 4:
  552.             if(server) {
  553.                 return server.getDefaultChannel();
  554.             }
  555.             break;
  556.         case 5:
  557.             return cache.temp[varName];
  558.             break;
  559.         case 6:
  560.             if(server && this.server[server.id]) {
  561.                 return this.server[server.id][varName];
  562.             }
  563.             break;
  564.         case 7:
  565.             return this.global[varName];
  566.             break;
  567.         default:
  568.             break;
  569.     }
  570.     return false;
  571. };
  572.  
  573. Actions.getMember = function(type, varName, cache) {
  574.     const msg = cache.msg;
  575.     const server = cache.server;
  576.     switch(type) {
  577.         case 0:
  578.             if(msg && msg.mentions && msg.mentions.members) {
  579.                 return msg.mentions.members.first();
  580.             }
  581.             break;
  582.         case 1:
  583.             if(msg) {
  584.                 return msg.member || msg.author;
  585.             }
  586.             break;
  587.         case 2:
  588.             return cache.temp[varName];
  589.             break;
  590.         case 3:
  591.             if(server && this.server[server.id]) {
  592.                 return this.server[server.id][varName];
  593.             }
  594.             break;
  595.         case 4:
  596.             return this.global[varName];
  597.             break;
  598.         default:
  599.             break;
  600.     }
  601.     return false;
  602. };
  603.  
  604. Actions.getMessage = function(type, varName, cache) {
  605.     const msg = cache.msg;
  606.     const server = cache.server;
  607.     switch(type) {
  608.         case 0:
  609.             if(msg) {
  610.                 return msg;
  611.             }
  612.             break;
  613.         case 1:
  614.             return cache.temp[varName];
  615.             break;
  616.         case 2:
  617.             if(server && this.server[server.id]) {
  618.                 return this.server[server.id][varName];
  619.             }
  620.             break;
  621.         case 3:
  622.             return this.global[varName];
  623.             break;
  624.         default:
  625.             break;
  626.     }
  627.     return false;
  628. };
  629.  
  630. Actions.getServer = function(type, varName, cache) {
  631.     const server = cache.server;
  632.     switch(type) {
  633.         case 0:
  634.             if(server) {
  635.                 return server;
  636.             }
  637.             break;
  638.         case 1:
  639.             return cache.temp[varName];
  640.             break;
  641.         case 2:
  642.             if(server && this.server[server.id]) {
  643.                 return this.server[server.id][varName];
  644.             }
  645.             break;
  646.         case 3:
  647.             return this.global[varName];
  648.             break;
  649.         default:
  650.             break;
  651.     }
  652.     return false;
  653. };
  654.  
  655. Actions.getRole = function(type, varName, cache) {
  656.     const msg = cache.msg;
  657.     const server = cache.server;
  658.     switch(type) {
  659.         case 0:
  660.             if(msg && msg.mentions && msg.mentions.roles) {
  661.                 return msg.mentions.roles.first();
  662.             }
  663.             break;
  664.         case 1:
  665.             if(msg && msg.member && msg.member.roles) {
  666.                 return msg.member.roles.first();
  667.             }
  668.             break;
  669.         case 2:
  670.             if(server && server.roles) {
  671.                 return server.roles.first();
  672.             }
  673.             break;
  674.         case 3:
  675.             return cache.temp[varName];
  676.             break;
  677.         case 4:
  678.             if(server && this.server[server.id]) {
  679.                 return this.server[server.id][varName];
  680.             }
  681.             break;
  682.         case 5:
  683.             return this.global[varName];
  684.             break;
  685.         default:
  686.             break;
  687.     }
  688.     return false;
  689. };
  690.  
  691. Actions.getChannel = function(type, varName, cache) {
  692.     const msg = cache.msg;
  693.     const server = cache.server;
  694.     switch(type) {
  695.         case 0:
  696.             if(msg) {
  697.                 return msg.channel;
  698.             }
  699.             break;
  700.         case 1:
  701.             if(msg && msg.mentions) {
  702.                 return msg.mentions.channels.first();
  703.             }
  704.             break;
  705.         case 2:
  706.             if(server) {
  707.                 return server.getDefaultChannel();
  708.             }
  709.             break;
  710.         case 3:
  711.             return cache.temp[varName];
  712.             break;
  713.         case 4:
  714.             if(server && this.server[server.id]) {
  715.                 return this.server[server.id][varName];
  716.             }
  717.             break;
  718.         case 5:
  719.             return this.global[varName];
  720.             break;
  721.         default:
  722.             break;
  723.     }
  724.     return false;
  725. };
  726.  
  727. Actions.getVoiceChannel = function(type, varName, cache) {
  728.     const msg = cache.msg;
  729.     const server = cache.server;
  730.     switch(type) {
  731.         case 0:
  732.             if(msg && msg.member) {
  733.                 return msg.member.voiceChannel;
  734.             }
  735.             break;
  736.         case 1:
  737.             if(msg && msg.mentions) {
  738.                 const member = msg.mentions.members.first();
  739.                 if(member) {
  740.                     return member.voiceChannel;
  741.                 }
  742.             }
  743.             break;
  744.         case 2:
  745.             if(server) {
  746.                 return server.getDefaultVoiceChannel();
  747.             }
  748.             break;
  749.         case 3:
  750.             return cache.temp[varName];
  751.             break;
  752.         case 4:
  753.             if(server && this.server[server.id]) {
  754.                 return this.server[server.id][varName];
  755.             }
  756.             break;
  757.         case 5:
  758.             return this.global[varName];
  759.             break;
  760.         default:
  761.             break;
  762.     }
  763.     return false;
  764. };
  765.  
  766. Actions.getList = function(type, varName, cache) {
  767.     const msg = cache.msg;
  768.     const server = cache.server;
  769.     switch(type) {
  770.         case 0:
  771.             if(server) {
  772.                 return server.members.array();
  773.             }
  774.             break;
  775.         case 1:
  776.             if(server) {
  777.                 return server.channels.array();
  778.             }
  779.             break;
  780.         case 2:
  781.             if(server) {
  782.                 return server.roles.array();
  783.             }
  784.             break;
  785.         case 3:
  786.             if(server) {
  787.                 return server.emojis.array();
  788.             }
  789.             break;
  790.         case 4:
  791.             return Bot.bot.guilds.array();
  792.             break;
  793.         case 5:
  794.             if(msg && msg.mentions && msg.mentions.members) {
  795.                 return msg.mentions.members.first().roles.array();
  796.             }
  797.             break;
  798.         case 6:
  799.             if(msg && msg.member) {
  800.                 return msg.member.roles.array();
  801.             }
  802.             break;
  803.         case 7:
  804.             return cache.temp[varName];
  805.             break;
  806.         case 8:
  807.             if(server && this.server[server.id]) {
  808.                 return this.server[server.id][varName];
  809.             }
  810.             break;
  811.         case 9:
  812.             return this.global[varName];
  813.             break;
  814.         default:
  815.             break;
  816.     }
  817.     return false;
  818. };
  819.  
  820. Actions.getVariable = function(type, varName, cache) {
  821.     const server = cache.server;
  822.     switch(type) {
  823.         case 1:
  824.             return cache.temp[varName];
  825.             break;
  826.         case 2:
  827.             if(server && this.server[server.id]) {
  828.                 return this.server[server.id][varName];
  829.             }
  830.             break;
  831.         case 3:
  832.             return this.global[varName];
  833.             break;
  834.         default:
  835.             break;
  836.     }
  837.     return false;
  838. };
  839.  
  840. Actions.storeValue = function(value, type, varName, cache) {
  841.     const server = cache.server;
  842.     switch(type) {
  843.         case 1:
  844.             cache.temp[varName] = value;
  845.             break;
  846.         case 2:
  847.             if(server) {
  848.                 if(!this.server[server.id]) this.server[server.id] = {};
  849.                 this.server[server.id][varName] = value;
  850.             }
  851.             break;
  852.         case 3:
  853.             this.global[varName] = value;
  854.             break;
  855.         default:
  856.             break;
  857.     }
  858. };
  859.  
  860. Actions.executeResults = function(result, data, cache) {
  861.     if(result) {
  862.         const type = parseInt(data.iftrue);
  863.         switch(type) {
  864.             case 0:
  865.                 this.callNextAction(cache);
  866.                 break;
  867.             case 2:
  868.                 const val = parseInt(this.evalMessage(data.iftrueVal, cache));
  869.                 const index = Math.max(val - 1, 0);
  870.                 if(cache.actions[index]) {
  871.                     cache.index = index - 1;
  872.                     this.callNextAction(cache);
  873.                 }
  874.                 break;
  875.             case 3:
  876.                 const amnt = parseInt(this.evalMessage(data.iftrueVal, cache));
  877.                 const index2 = cache.index + amnt + 1;
  878.                 if(cache.actions[index2]) {
  879.                     cache.index = index2 - 1;
  880.                     this.callNextAction(cache);
  881.                 }
  882.                 break;
  883.             default:
  884.                 break;
  885.         }
  886.     } else {
  887.         const type = parseInt(data.iffalse);
  888.         switch(type) {
  889.             case 0:
  890.                 this.callNextAction(cache);
  891.                 break;
  892.             case 2:
  893.                 const val = parseInt(this.evalMessage(data.iffalseVal, cache));
  894.                 const index = Math.max(val - 1, 0);
  895.                 if(cache.actions[index]) {
  896.                     cache.index = index - 1;
  897.                     this.callNextAction(cache);
  898.                 }
  899.                 break;
  900.             case 3:
  901.                 const amnt = parseInt(this.evalMessage(data.iffalseVal, cache));
  902.                 const index2 = cache.index + amnt + 1;
  903.                 if(cache.actions[index2]) {
  904.                     cache.index = index2 - 1;
  905.                     this.callNextAction(cache);
  906.                 }
  907.                 break;
  908.             default:
  909.                 break;
  910.         }
  911.     }
  912. };
  913.  
  914. //---------------------------------------------------------------------
  915. // Events
  916. // Handles the various events that occur.
  917. //---------------------------------------------------------------------
  918.  
  919. const Events = DBM.Events = {};
  920.  
  921. let $evts = null;
  922.  
  923. Events.data = [
  924.     [],[],[],[],['guildCreate', 0, 0, 1],['guildDelete', 0, 0, 1],['guildMemberAdd', 1, 0, 2],['guildMemberRemove', 1, 0, 2],['channelCreate', 1, 0, 2, true, 'arg1.type !== \'text\''],['channelDelete', 1, 0, 2, true, 'arg1.type !== \'text\''],['roleCreate', 1, 0, 2],['roleDelete', 1, 0, 2],['guildBanAdd', 3, 0, 1],['guildBanRemove', 3, 0, 1],['channelCreate', 1, 0, 2, true, 'arg1.type !== \'voice\''],['channelDelete', 1, 0, 2, true, 'arg1.type !== \'voice\''],['emojiCreate', 1, 0, 2],['emojiDelete', 1, 0, 2],['messageDelete', 1, 0, 2, true],['guildUpdate', 1, 3, 3],['guildMemberUpdate', 1, 3, 4],['presenceUpdate', 1, 3, 4],['voiceStateUpdate', 1, 3, 4],['channelUpdate', 1, 3, 4, true],['channelPinsUpdate', 1, 0, 2, true],['roleUpdate', 1, 3, 4],['messageUpdate', 1, 3, 4, true, 'arg2.content.length === 0'],['emojiUpdate', 1, 3, 4],[],[],['messageReactionRemoveAll', 1, 0, 2, true],['guildMemberAvailable', 1, 0, 2],['guildMembersChunk', 1, 0, 3],['guildMemberSpeaking', 1, 3, 2],[],[],['guildUnavailable', 1, 0, 1]
  925. ];
  926.  
  927. Events.registerEvents = function(bot) {
  928.     $evts = Bot.$evts;
  929.     for(let i = 0; i < this.data.length; i++) {
  930.         const d = this.data[i];
  931.         if(d.length > 0 && $evts[String(i)]) {
  932.             bot.on(d[0], this.callEvents.bind(this, String(i), d[1], d[2], d[3], !!d[4], d[5]));
  933.         }
  934.     }
  935.     if($evts["28"]) bot.on('messageReactionAdd', this.onReaction.bind(this, "28"));
  936.     if($evts["29"]) bot.on('messageReactionRemove', this.onReaction.bind(this, "29"));
  937.     if($evts["34"]) bot.on('typingStart', this.onTyping.bind(this, "34"));
  938.     if($evts["35"]) bot.on('typingStop', this.onTyping.bind(this, "35"));
  939. };
  940.  
  941. Events.callEvents = function(id, temp1, temp2, server, mustServe, condition, arg1, arg2) {
  942.     if(mustServe) {
  943.         if(temp1 > 0 && !arg1.guild) return;
  944.         if(temp2 > 0 && !arg2.guild) return;
  945.     }
  946.     if(condition && eval(condition)) return;
  947.     const events = $evts[id];
  948.     if(!events) return;
  949.     for(let i = 0; i < events.length; i++) {
  950.         const event = events[i];
  951.         const temp = {};
  952.         if(event.temp) temp[event.temp] = this.getObject(temp1, arg1, arg2);
  953.         if(event.temp2) temp[event.temp2] = this.getObject(temp2, arg1, arg2);
  954.         Actions.invokeEvent(event, this.getObject(server, arg1, arg2), temp);
  955.     }
  956. };
  957.  
  958. Events.getObject = function(id, arg1, arg2) {
  959.     switch(id) {
  960.         case 1: return arg1;
  961.         case 2: return arg1.guild;
  962.         case 3: return arg2;
  963.         case 4: return arg2.guild;
  964.     }
  965.     return undefined;
  966. };
  967.  
  968. Events.onInitialization = function(bot) {
  969.     const events = $evts["1"];
  970.     for(let i = 0; i < events.length; i++) {
  971.         const event = events[i];
  972.         const temp = {};
  973.         const servers = bot.guilds.array();
  974.         for(let i = 0; i < servers.length; i++) {
  975.             const server = servers[i];
  976.             if(server) {
  977.                 Actions.invokeEvent(event, server, temp);
  978.             }
  979.         }
  980.     }
  981. };
  982.  
  983. Events.setupIntervals = function(bot) {
  984.     const events = $evts["3"];
  985.     for(let i = 0; i < events.length; i++) {
  986.         const event = events[i];
  987.         const temp = {};
  988.         const time = event.temp ? parseFloat(event.temp) : 60;
  989.         bot.setInterval(function() {
  990.             const servers = bot.guilds.array();
  991.             for(let i = 0; i < servers.length; i++) {
  992.                 const server = servers[i];
  993.                 if(server) {
  994.                     Actions.invokeEvent(event, server, temp);
  995.                 }
  996.             }
  997.         }.bind(this), time * 1000);
  998.     }
  999. };
  1000.  
  1001. Events.onReaction = function(id, reaction, user) {
  1002.     const events = $evts[id];
  1003.     if(!events) return;
  1004.     if(!reaction.message || !reaction.message.guild) return;
  1005.     const server = reaction.message.guild;
  1006.     const member = server.member(user);
  1007.     if(!member) return;
  1008.     for(let i = 0; i < events.length; i++) {
  1009.         const event = events[i];
  1010.         const temp = {};
  1011.         if(event.temp) temp[event.temp] = reaction;
  1012.         if(event.temp2) temp[event.temp2] = member;
  1013.         Actions.invokeEvent(event, server, temp);
  1014.     }
  1015. };
  1016.  
  1017. Events.onTyping = function(id, channel, user) {
  1018.     const events = $evts[id];
  1019.     if(!events) return;
  1020.     if(!channel.guild) return;
  1021.     const server = channel.guild;
  1022.     const member = server.member(user);
  1023.     if(!member) return;
  1024.     for(let i = 0; i < events.length; i++) {
  1025.         const event = events[i];
  1026.         const temp = {};
  1027.         if(event.temp) temp[event.temp] = channel;
  1028.         if(event.temp2) temp[event.temp2] = member;
  1029.         Actions.invokeEvent(event, server, temp);
  1030.     }
  1031. };
  1032.  
  1033. Events.onError = function(text, text2, cache) {
  1034.     const events = $evts["37"];
  1035.     if(!events) return;
  1036.     for(let i = 0; i < events.length; i++) {
  1037.         const event = events[i];
  1038.         const temp = {};
  1039.         if(event.temp) temp[event.temp] = text;
  1040.         if(event.temp2) temp[event.temp2] = text2;
  1041.         Actions.invokeEvent(event, cache.server, temp);
  1042.     }
  1043. };
  1044.  
  1045. //---------------------------------------------------------------------
  1046. // Images
  1047. // Contains functions for image management.
  1048. //---------------------------------------------------------------------
  1049.  
  1050. const JIMP = require('jimp');
  1051.  
  1052. const Images = DBM.Images = {};
  1053.  
  1054. Images.getImage = function(url) {
  1055.     if(!url.startsWith('http')) url = Actions.getLocalFile(url);
  1056.     return JIMP.read(url); 
  1057. };
  1058.  
  1059. Images.getFont = function(url) {
  1060.     return JIMP.loadFont(Actions.getLocalFile(url));   
  1061. };
  1062.  
  1063. Images.createBuffer = function(image) {
  1064.     return new Promise(function(resolve, reject) {
  1065.         image.getBuffer(JIMP.MIME_PNG, function(err, buffer) {
  1066.             if(err) {
  1067.                 reject(err);
  1068.             } else {
  1069.                 resolve(buffer);
  1070.             }
  1071.         });
  1072.     });
  1073. };
  1074.  
  1075. Images.drawImageOnImage = function(img1, img2, x, y) {
  1076.     for(let i = 0; i < img2.bitmap.width; i++) {
  1077.         for(let j = 0; j < img2.bitmap.height; j++) {
  1078.             const pos = (i * (img2.bitmap.width * 4)) + (j * 4);
  1079.             const pos2 = ((i + y) * (img1.bitmap.width * 4)) + ((j + x) * 4);
  1080.             const target = img1.bitmap.data;
  1081.             const source = img2.bitmap.data;
  1082.             for(let k = 0; k < 4; k++) {
  1083.                 target[pos2 + k] = source[pos + k];
  1084.             }
  1085.         }
  1086.     }
  1087. };
  1088.  
  1089. //---------------------------------------------------------------------
  1090. // Files
  1091. // Contains functions for file management.
  1092. //---------------------------------------------------------------------
  1093.  
  1094. const Files = DBM.Files = {};
  1095.  
  1096. Files.data = {};
  1097. Files.writers = {};
  1098. Files.crypto = require('crypto');
  1099. Files.dataFiles = [
  1100.     'commands.json',
  1101.     'events.json',
  1102.     'settings.json',
  1103.     'players.json',
  1104.     'servers.json',
  1105.     'serverVars.json',
  1106.     'globalVars.json'
  1107. ];
  1108.  
  1109. Files.startBot = function() {
  1110.     const fs = require('fs');
  1111.     const path = require('path');
  1112.     if(process.env['IsDiscordBotMakerTest'] === 'true') {
  1113.         Actions.location = process.env['ActionsDirectory'];
  1114.         this.initBotTest();
  1115.     } else if(process.argv.length >= 3 && fs.existsSync(process.argv[2])) {
  1116.         Actions.location = process.argv[2];
  1117.     } else {
  1118.         Actions.location = path.join(process.cwd(), 'actions')
  1119.     }
  1120.     if(typeof Actions.location === 'string' && fs.existsSync(Actions.location)) {
  1121.         Actions.initMods();
  1122.         this.readData(Bot.init.bind(Bot));
  1123.     } else {
  1124.         console.error('Please copy the "Actions" folder from the Discord Bot Maker directory to this bot\'s directory: \n' + Actions.location);
  1125.     }
  1126. };
  1127.  
  1128. Files.initBotTest = function(content) {
  1129.     this._console_log = console.log;
  1130.     console.log = function() {
  1131.         process.send(String(arguments[0]));
  1132.         Files._console_log.apply(this, arguments);
  1133.     };
  1134.  
  1135.     this._console_error = console.error;
  1136.     console.error = function() {
  1137.         process.send(String(arguments[0]));
  1138.         Files._console_error.apply(this, arguments);
  1139.     };
  1140. };
  1141.  
  1142. Files.readData = function(callback) {
  1143.     const fs = require('fs');
  1144.     const path = require('path');
  1145.     let max = this.dataFiles.length;
  1146.     let cur = 0;
  1147.     for(let i = 0; i < max; i++) {
  1148.         const filePath = path.join(process.cwd(), 'data', this.dataFiles[i]);
  1149.         if(!fs.existsSync(filePath)) continue;
  1150.         fs.readFile(filePath, function(error, content) {
  1151.             const filename = this.dataFiles[i].slice(0, -5);
  1152.             let data;
  1153.             try {
  1154.                 if(typeof content !== 'string' && content.toString) content = content.toString();
  1155.                 data = JSON.parse(this.decrypt(content));
  1156.             } catch(e) {
  1157.                 console.error(`There was issue parsing ${this.dataFiles[i]}!`);
  1158.                 return;
  1159.             }
  1160.             this.data[filename] = data;
  1161.             if(++cur === max) {
  1162.                 callback();
  1163.             }
  1164.         }.bind(this));
  1165.     }
  1166. };
  1167.  
  1168. Files.saveData = function(file, callback) {
  1169.     const fs = require('fs');
  1170.     const path = require('path');
  1171.     const data = this.data[file];
  1172.     if(!this.writers[file]) {
  1173.         const fstorm = require('fstorm');
  1174.         this.writers[file] = fstorm(path.join(process.cwd(), 'data', file + '.json'))
  1175.     }
  1176.     this.writers[file].write(this.encrypt(JSON.stringify(data)), function() {
  1177.         if(callback) {
  1178.             callback();
  1179.         }
  1180.     }.bind(this));
  1181. };
  1182.  
  1183. Files.initEncryption = function() {
  1184.     try {
  1185.         this.password = require('discord-bot-maker');
  1186.     } catch(e) {
  1187.         this.password = '';
  1188.     }
  1189. };
  1190.  
  1191. Files.encrypt = function(text) {
  1192.     if(this.password.length === 0) return text;
  1193.     const cipher = this.crypto.createCipher('aes-128-ofb', this.password);
  1194.     let crypted = cipher.update(text, 'utf8', 'hex');
  1195.     crypted += cipher.final('hex');
  1196.     return crypted;
  1197. };
  1198.  
  1199. Files.decrypt = function(text) {
  1200.     if(this.password.length === 0) return text;
  1201.     const decipher = this.crypto.createDecipher('aes-128-ofb', this.password);
  1202.     let dec = decipher.update(text, 'hex', 'utf8');
  1203.     dec += decipher.final('utf8');
  1204.     return dec;
  1205. };
  1206.  
  1207. Files.convertItem = function(item) {
  1208.     if(Array.isArray(item)) {
  1209.         const result = [];
  1210.         const length = item.length;
  1211.         for(let i = 0; i < length; i++) {
  1212.             result[i] = this.convertItem(item[i]);
  1213.         }
  1214.         return result;
  1215.     } else if(typeof item !== 'object') {
  1216.         let result = '';
  1217.         try {
  1218.             result = JSON.stringify(item);
  1219.         } catch(e) {}
  1220.         if(result !== '{}') {
  1221.             return item;
  1222.         }
  1223.     } else if(item.convertToString) {
  1224.         return item.convertToString();
  1225.     }
  1226.     return null;
  1227. };
  1228.  
  1229. Files.saveServerVariable = function(serverID, varName, item) {
  1230.     if(!this.data.serverVars[serverID]) {
  1231.         this.data.serverVars[serverID] = {};
  1232.     }
  1233.     const strItem = this.convertItem(item);
  1234.     if(strItem !== null) {
  1235.         this.data.serverVars[serverID][varName] = strItem;
  1236.     }
  1237.     this.saveData('serverVars');
  1238. };
  1239.  
  1240. Files.restoreServerVariables = function() {
  1241.     const keys = Object.keys(this.data.serverVars);
  1242.     for(let i = 0; i < keys.length; i++) {
  1243.         const varNames = Object.keys(this.data.serverVars[keys[i]]);
  1244.         for(let j = 0; j < varNames.length; j++) {
  1245.             this.restoreVariable(this.data.serverVars[keys[i]][varNames[j]], 2, varNames[j], keys[i]);
  1246.         }
  1247.     }
  1248. };
  1249.  
  1250. Files.saveGlobalVariable = function(varName, item) {
  1251.     const strItem = this.convertItem(item);
  1252.     if(strItem !== null) {
  1253.         this.data.globalVars[varName] = strItem;
  1254.     }
  1255.     this.saveData('globalVars');
  1256. };
  1257.  
  1258. Files.restoreGlobalVariables = function() {
  1259.     const keys = Object.keys(this.data.globalVars);
  1260.     for(let i = 0; i < keys.length; i++) {
  1261.         this.restoreVariable(this.data.globalVars[keys[i]], 3, keys[i]);
  1262.     }
  1263. };
  1264.  
  1265. Files.restoreVariable = function(value, type, varName, serverId) {
  1266.     const bot = Bot.bot;
  1267.     let cache = {};
  1268.     if(serverId) {
  1269.         cache.server = {
  1270.             id: serverId
  1271.         };
  1272.     }
  1273.     if(typeof value === 'string' || Array.isArray(value)) {
  1274.         this.restoreValue(value, bot).then(function(finalValue) {
  1275.             if(finalValue) {
  1276.                 Actions.storeValue(finalValue, type, varName, cache);
  1277.             }
  1278.         }.bind(this)).catch(() => {});
  1279.     } else {
  1280.         Actions.storeValue(value, type, varName, cache);
  1281.     }
  1282. };
  1283.  
  1284. Files.restoreValue = function(value, bot) {
  1285.     return new Promise(function(resolve, reject) {
  1286.         if(typeof value === 'string') {
  1287.             if(value.startsWith('mem-')) {
  1288.                 return resolve(this.restoreMember(value, bot));
  1289.             } else if(value.startsWith('msg-')) {
  1290.                 return this.restoreMessage(value, bot).then(resolve).catch(reject);
  1291.             } else if(value.startsWith('tc-')) {
  1292.                 return resolve(this.restoreTextChannel(value, bot));
  1293.             } else if(value.startsWith('vc-')) {
  1294.                 return resolve(this.restoreVoiceChannel(value, bot));
  1295.             } else if(value.startsWith('r-')) {
  1296.                 return resolve(this.restoreRole(value, bot));
  1297.             } else if(value.startsWith('s-')) {
  1298.                 return resolve(this.restoreServer(value, bot));
  1299.             } else if(value.startsWith('e-')) {
  1300.                 return resolve(this.restoreEmoji(value, bot));
  1301.             } else if(value.startsWith('usr-')) {
  1302.                 return resolve(this.restoreUser(value, bot));
  1303.             }
  1304.             resolve(value);
  1305.         } else if(Array.isArray(value)) {
  1306.             const result = [];
  1307.             const length = value.length;
  1308.             let curr = 0;
  1309.             for(let i = 0; i < length; i++) {
  1310.                 this.restoreValue(value[i], bot).then(function(item) {
  1311.                     result[i] = item;
  1312.                     if(++curr >= length) {
  1313.                         resolve(result);
  1314.                     }
  1315.                 }).catch(function() {
  1316.                     if(++curr >= length) {
  1317.                         resolve(result);
  1318.                     }
  1319.                 });
  1320.             }
  1321.         }
  1322.     }.bind(this));
  1323. };
  1324.  
  1325. Files.restoreMember = function(value, bot) {
  1326.     const split = value.split('_');
  1327.     const memId = split[0].slice(4);
  1328.     const serverId = split[1].slice(2);
  1329.     const server = bot.guilds.get(serverId);
  1330.     if(server && server.members) {
  1331.         const member = server.members.get(memId);
  1332.         return member;
  1333.     }
  1334. };
  1335.  
  1336. Files.restoreMessage = function(value, bot) {
  1337.     const split = value.split('_');
  1338.     const msgId = split[0].slice(4);
  1339.     const channelId = split[1].slice(2);
  1340.     const channel = bot.channels.get(channelId);
  1341.     if(channel && channel.fetchMessage) {
  1342.         return channel.fetchMessage(msgId);
  1343.     }
  1344. };
  1345.  
  1346. Files.restoreTextChannel = function(value, bot) {
  1347.     const channelId = value.slice(3);
  1348.     const channel = bot.channels.get(channelId);
  1349.     return channel;
  1350. };
  1351.  
  1352. Files.restoreVoiceChannel = function(value, bot) {
  1353.     const channelId = value.slice(3);
  1354.     const channel = bot.channels.get(channelId);
  1355.     return channel;
  1356. };
  1357.  
  1358. Files.restoreRole = function(value, bot) {
  1359.     const split = value.split('_');
  1360.     const roleId = split[0].slice(2);
  1361.     const serverId = split[1].slice(2);
  1362.     const server = bot.guilds.get(serverId);
  1363.     if(server && server.roles) {
  1364.         const role = server.roles.get(roleId);
  1365.         return role;
  1366.     }
  1367. };
  1368.  
  1369. Files.restoreServer = function(value, bot) {
  1370.     const serverId = value.slice(2);
  1371.     const server = bot.guilds.get(serverId);
  1372.     return server;
  1373. };
  1374.  
  1375. Files.restoreEmoji = function(value, bot) {
  1376.     const emojiId = value.slice(2);
  1377.     const emoji = bot.emojis.get(emojiId);
  1378.     return emoji;
  1379. };
  1380.  
  1381. Files.restoreUser = function(value, bot) {
  1382.     const userId = value.slice(4);
  1383.     const user = bot.users.get(userId);
  1384.     return user;
  1385. };
  1386.  
  1387. Files.initEncryption();
  1388.  
  1389. //---------------------------------------------------------------------
  1390. // Audio
  1391. // Contains functions for voice channel stuff.
  1392. //---------------------------------------------------------------------
  1393.  
  1394. const Audio = DBM.Audio = {};
  1395.  
  1396. Audio.ytdl = null;
  1397. try {
  1398.     Audio.ytdl = require('ytdl-core');
  1399. } catch(e) {}
  1400.  
  1401. Audio.queue = [];
  1402. Audio.volumes = [];
  1403. Audio.connections = [];
  1404. Audio.dispatchers = [];
  1405.  
  1406. Audio.isConnected = function(cache) {
  1407.     if(!cache.server) return false;
  1408.     const id = cache.server.id;
  1409.     return this.connections[id];
  1410. };
  1411.  
  1412. Audio.isPlaying = function(cache) {
  1413.     if(!cache.server) return false;
  1414.     const id = cache.server.id;
  1415.     return this.dispatchers[id];
  1416. };
  1417.  
  1418. Audio.setVolume = function(volume, cache) {
  1419.     if(!cache.server) return;
  1420.     const id = cache.server.id;
  1421.     if(this.dispatchers[id]) {
  1422.         this.volumes[id] = volume;
  1423.         this.dispatchers[id].setVolumeLogarithmic(volume);
  1424.     }
  1425. };
  1426.  
  1427. Audio.connectToVoice = function(voiceChannel) {
  1428.     const promise = voiceChannel.join();
  1429.     promise.then(function(connection) {
  1430.         this.connections[voiceChannel.guild.id] = connection;
  1431.         connection.on('disconnect', function() {
  1432.             this.connections[voiceChannel.guild.id] = null;
  1433.             this.volumes[voiceChannel.guild.id] = null;
  1434.         }.bind(this));
  1435.     }.bind(this)).catch(console.error);
  1436.     return promise;
  1437. };
  1438.  
  1439. Audio.addToQueue = function(item, cache) {
  1440.     if(!cache.server) return;
  1441.     const id = cache.server.id;
  1442.     if(!this.queue[id]) this.queue[id] = [];
  1443.     this.queue[id].push(item);
  1444.     this.playNext(id);
  1445. };
  1446.  
  1447. Audio.clearQueue = function(cache) {
  1448.     if(!cache.server) return;
  1449.     const id = cache.server.id;
  1450.     this.queue[id] = [];
  1451. };
  1452.  
  1453. Audio.playNext = function(id, forceSkip) {
  1454.     if(!this.connections[id]) return;
  1455.     if(!this.dispatchers[id] || !!forceSkip) {
  1456.         if(this.queue[id].length > 0) {
  1457.             const item = this.queue[id].shift();
  1458.             this.playItem(item, id);
  1459.         } else {
  1460.             this.connections[id].disconnect();
  1461.         }
  1462.     }
  1463. };
  1464.  
  1465. Audio.playItem = function(item, id) {
  1466.     if(!this.connections[id]) return;
  1467.     if(this.dispatchers[id]) {
  1468.         this.dispatchers[id]._forceEnd = true;
  1469.         this.dispatchers[id].end();
  1470.     }
  1471.     const type = item[0];
  1472.     let setupDispatcher = false;
  1473.     switch(type) {
  1474.         case 'file':
  1475.             setupDispatcher = this.playFile(item[2], item[1], id);
  1476.             break;
  1477.         case 'url':
  1478.             setupDispatcher = this.playUrl(item[2], item[1], id);
  1479.             break;
  1480.         case 'yt':
  1481.             setupDispatcher = this.playYt(item[2], item[1], id);
  1482.             break;
  1483.     }
  1484.     if(setupDispatcher && !this.dispatchers[id]._eventSetup) {
  1485.         this.dispatchers[id].on('end', function() {
  1486.             const isForced = this.dispatchers[id]._forceEnd;
  1487.             this.dispatchers[id] = null;
  1488.             if(!isForced) {
  1489.                 this.playNext(id);
  1490.             }
  1491.         }.bind(this));
  1492.         this.dispatchers[id]._eventSetup = true;
  1493.     }
  1494. };
  1495.  
  1496. Audio.playFile = function(url, options, id) {
  1497.     this.dispatchers[id] = this.connections[id].playFile(Actions.getLocalFile(url), options);
  1498.     return true;
  1499. };
  1500.  
  1501. Audio.playUrl = function(url, options, id) {
  1502.     this.dispatchers[id] = this.connections[id].playArbitraryInput(url, options);
  1503.     return true;
  1504. };
  1505.  
  1506. Audio.playYt = function(url, options, id) {
  1507.     if(!this.ytdl) return false;
  1508.     const stream = this.ytdl(url, {
  1509.         filter: 'audioonly'
  1510.     });
  1511.     this.dispatchers[id] = this.connections[id].playStream(stream, options);
  1512.     return true;
  1513. };
  1514.  
  1515. //---------------------------------------------------------------------
  1516. // GuildMember
  1517. //---------------------------------------------------------------------
  1518.  
  1519. const GuildMember = DiscordJS.GuildMember;
  1520.  
  1521. GuildMember.prototype.unban = function(server, reason) {
  1522.     return server.unban(this.author, reason);
  1523. };
  1524.  
  1525. GuildMember.prototype.data = function(name, defaultValue) {
  1526.     const id = this.id;
  1527.     const data = Files.data.players;
  1528.     if(data[id] === undefined) {
  1529.         if(defaultValue === undefined) {
  1530.             return null;
  1531.         } else {
  1532.             data[id] = {};
  1533.         }
  1534.     }
  1535.     if(data[id][name] === undefined && defaultValue !== undefined) {
  1536.         data[id][name] = defaultValue;
  1537.     }
  1538.     return data[id][name];
  1539. };
  1540.  
  1541. GuildMember.prototype.setData = function(name, value) {
  1542.     const id = this.id;
  1543.     const data = Files.data.players;
  1544.     if(data[id] === undefined) {
  1545.         data[id] = {};
  1546.     }
  1547.     data[id][name] = value;
  1548.     Files.saveData('players');
  1549. };
  1550.  
  1551. GuildMember.prototype.addData = function(name, value) {
  1552.     const id = this.id;
  1553.     const data = Files.data.players;
  1554.     if(data[id] === undefined) {
  1555.         data[id] = {};
  1556.     }
  1557.     if(data[id][name] === undefined) {
  1558.         this.setData(name, value);
  1559.     } else {
  1560.         this.setData(name, this.data(name) + value);
  1561.     }
  1562. };
  1563.  
  1564. GuildMember.prototype.convertToString = function() {
  1565.     return `mem-${this.id}_s-${this.guild.id}`;
  1566. };
  1567.  
  1568. //---------------------------------------------------------------------
  1569. // User
  1570. //---------------------------------------------------------------------
  1571.  
  1572. const User = DiscordJS.User;
  1573.  
  1574. User.prototype.data = GuildMember.prototype.data;
  1575. User.prototype.setData = GuildMember.prototype.setData;
  1576. User.prototype.addData = GuildMember.prototype.addData;
  1577.  
  1578. User.prototype.convertToString = function() {
  1579.     return `usr-${this.id}`;
  1580. };
  1581.  
  1582. //---------------------------------------------------------------------
  1583. // Guild
  1584. //---------------------------------------------------------------------
  1585.  
  1586. const Guild = DiscordJS.Guild;
  1587.  
  1588. Guild.prototype.getDefaultChannel = function() {
  1589.     let channel = this.channels.get(this.id);
  1590.     if(!channel) {
  1591.         this.channels.array().forEach(function(c) {
  1592.             if(c.type !== 'voice') {
  1593.                 if(!channel) {
  1594.                     channel = c;
  1595.                 } else if(channel.position > c.position) {
  1596.                     channel = c;
  1597.                 }
  1598.             }
  1599.         });
  1600.     }
  1601.     return channel;
  1602. };
  1603.  
  1604. Guild.prototype.data = function(name, defaultValue) {
  1605.     const id = this.id;
  1606.     const data = Files.data.servers;
  1607.     if(data[id] === undefined) {
  1608.         if(defaultValue === undefined) {
  1609.             return null;
  1610.         } else {
  1611.             data[id] = {};
  1612.         }
  1613.     }
  1614.     if(data[id][name] === undefined && defaultValue !== undefined) {
  1615.         data[id][name] = defaultValue;
  1616.     }
  1617.     return data[id][name];
  1618. };
  1619.  
  1620. Guild.prototype.setData = function(name, value) {
  1621.     const id = this.id;
  1622.     const data = Files.data.servers;
  1623.     if(data[id] === undefined) {
  1624.         data[id] = {};
  1625.     }
  1626.     data[id][name] = value;
  1627.     Files.saveData('servers');
  1628. };
  1629.  
  1630. Guild.prototype.addData = function(name, value) {
  1631.     const id = this.id;
  1632.     const data = Files.data.servers;
  1633.     if(data[id] === undefined) {
  1634.         data[id] = {};
  1635.     }
  1636.     if(data[id][name] === undefined) {
  1637.         this.setData(name, value);
  1638.     } else {
  1639.         this.setData(name, this.data(name) + value);
  1640.     }
  1641. };
  1642.  
  1643. Guild.prototype.convertToString = function() {
  1644.     return `s-${this.id}`;
  1645. };
  1646.  
  1647. //---------------------------------------------------------------------
  1648. // Message
  1649. //---------------------------------------------------------------------
  1650.  
  1651. DiscordJS.Message.prototype.convertToString = function() {
  1652.     return `msg-${this.id}_c-${this.channel.id}`;
  1653. };
  1654.  
  1655. //---------------------------------------------------------------------
  1656. // TextChannel
  1657. //---------------------------------------------------------------------
  1658.  
  1659. DiscordJS.TextChannel.prototype.convertToString = function() {
  1660.     return `tc-${this.id}`;
  1661. };
  1662.  
  1663. //---------------------------------------------------------------------
  1664. // VoiceChannel
  1665. //---------------------------------------------------------------------
  1666.  
  1667. DiscordJS.VoiceChannel.prototype.convertToString = function() {
  1668.     return `vc-${this.id}`;
  1669. };
  1670.  
  1671. //---------------------------------------------------------------------
  1672. // Role
  1673. //---------------------------------------------------------------------
  1674.  
  1675. DiscordJS.Role.prototype.convertToString = function() {
  1676.     return `r-${this.id}_s-${this.guild.id}`;
  1677. };
  1678.  
  1679. //---------------------------------------------------------------------
  1680. // Emoji
  1681. //---------------------------------------------------------------------
  1682.  
  1683. DiscordJS.Emoji.prototype.convertToString = function() {
  1684.     return `e-${this.id}`;
  1685. };
  1686.  
  1687. //---------------------------------------------------------------------
  1688. // Start Bot
  1689. //---------------------------------------------------------------------
  1690.  
  1691. Files.startBot();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement