Advertisement
HernooX

Untitled

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