Advertisement
Guest User

Untitled

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