Advertisement
Guest User

code do bot

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