Advertisement
Guest User

Untitled

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