Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const path = require('path');
  4. const fse = require('fs-extra');
  5. const Discord = require('discord.js');
  6. const stripIndents = require('common-tags').stripIndents;
  7. const chalk = require('chalk');
  8. const Managers = require('./managers');
  9.  
  10. const bot = global.bot = exports.client = new Discord.Client();
  11.  
  12. bot.managers = {};
  13.  
  14. const logger = bot.logger = new Managers.Logger(bot);
  15. logger.inject();
  16.  
  17. bot.managers.dynamicImports = global.dynamicImports = new Managers.DynamicImports(bot, __dirname);
  18. bot.managers.dynamicImports.init();
  19.  
  20. const configManager = bot.managers.config = new Managers.Config(bot, __dirname, bot.managers.dynamicImports);
  21.  
  22. bot.config = global.config = configManager.load();
  23.  
  24. const pluginManager = bot.plugins = bot.managers.pluginManager = new Managers.Plugins(bot);
  25. pluginManager.loadPlugins();
  26.  
  27. bot.storage = new Managers.Storage();
  28.  
  29. bot.managers.notifications = new Managers.Notifications(bot);
  30.  
  31. const commands = bot.commands = new Managers.CommandManager(bot);
  32. const stats = bot.managers.stats = new Managers.Stats(bot);
  33.  
  34. bot.deleted = new Discord.Collection();
  35.  
  36. bot.setInterval(() => {
  37. bot.deleted.clear();
  38. }, 7200000);
  39.  
  40. const settings = global.settings = {
  41. dataFolder: path.resolve(__dirname, '..', 'data'),
  42. configsFolder: path.resolve(__dirname, '..', 'data', 'configs')
  43. };
  44.  
  45. if (!fse.existsSync(settings.dataFolder)) fse.mkdirSync(settings.dataFolder);
  46. if (!fse.existsSync(settings.configsFolder)) fse.mkdirSync(settings.configsFolder);
  47.  
  48. Managers.Migrator.migrate(bot, __dirname);
  49.  
  50. let loaded = false;
  51.  
  52. bot.utils = global.utils = require('./utils');
  53.  
  54. bot.on("ready", () => {bot.user.setPresence({game: {name "music", type:2}}); })
  55.  
  56. bot.on('ready', () => {
  57. if (bot.user.bot) {
  58. logger.severe(`SharpBot is a selfbot, but you entered a bot token. Please follow the instructions at ${chalk.green('https://github.com/RayzrDev/SharpBot#getting-your-user-token')} and re-enter your token by running ${chalk.green('yarn run config')}.`);
  59. process.exit(666);
  60. }
  61.  
  62. // =======================================================
  63. // === Until we know how to fix this, just make people ===
  64. // === use the //status command to make the bot invis. ===
  65. // =======================================================
  66. // bot.user.setStatus('invisible');
  67.  
  68. // Fix mobile notifications
  69. bot.user.setAFK(true);
  70.  
  71. commands.loadCommands();
  72.  
  73. (title => {
  74. process.title = title;
  75. process.stdout.write(`\u001B]0;${title}\u0007`);
  76. })(`SharpBot - ${bot.user.username}`);
  77.  
  78. logger.info(stripIndents`Stats:
  79. - User: ${bot.user.username}#${bot.user.discriminator} <ID: ${bot.user.id}>
  80. - Users: ${bot.users.filter(user => !user.bot).size}
  81. - Bots: ${bot.users.filter(user => user.bot).size}
  82. - Channels: ${bot.channels.size}
  83. - Guilds: ${bot.guilds.size}`
  84. );
  85.  
  86. stats.set('start-time', process.hrtime());
  87.  
  88. delete bot.user.email;
  89. delete bot.user.verified;
  90.  
  91. logger.info('Bot loaded');
  92.  
  93. loaded = true;
  94. });
  95.  
  96. bot.on('message', msg => {
  97. stats.increment(`messages-${bot.user.id === msg.author.id ? 'sent' : 'received'}`);
  98. if (msg.isMentioned(bot.user)) {
  99. stats.increment('mentions');
  100. console.log(`[MENTION] ${msg.author.username} | ${msg.guild ? msg.guild.name : '(DM)'} | #${msg.channel.name || 'N/A'}:\n${msg.cleanContent}`);
  101. }
  102.  
  103. if (msg.author.id !== bot.user.id) return;
  104.  
  105. if (msg.guild && bot.config.blacklistedServers && bot.config.blacklistedServers.indexOf(msg.guild.id.toString()) > -1) {
  106. return;
  107. }
  108.  
  109. return bot.commands.handleCommand(msg, msg.content);
  110. });
  111.  
  112. bot.on('messageDelete', (msg) => {
  113. bot.deleted.set(msg.author.id, msg);
  114. });
  115.  
  116. process.on('exit', () => {
  117. bot.storage.saveAll();
  118. loaded && bot.destroy();
  119. });
  120.  
  121. bot.on('error', console.error);
  122. bot.on('warn', console.warn);
  123. bot.on('disconnect', event => {
  124. if (event.code === 1000) {
  125. logger.info('Disconnected from Discord cleanly');
  126. } else if (event.code === 4004) {
  127. // Force the user to reconfigure if their token is invalid
  128. logger.severe(`Failed to authenticate with Discord. Please follow the instructions at ${chalk.green('https://github.com/RayzrDev/SharpBot#getting-your-user-token')} and re-enter your token by running ${chalk.green('yarn run config')}.`);
  129. process.exit(666);
  130. } else {
  131. logger.warn(`Disconnected from Discord with code ${event.code}`);
  132. }
  133. });
  134.  
  135. process.on('uncaughtException', (err) => {
  136. let errorMsg = (err ? err.stack || err : '').toString().replace(new RegExp(`${__dirname}\/`, 'g'), './');
  137. logger.severe(errorMsg);
  138. });
  139.  
  140. process.on('unhandledRejection', err => {
  141. logger.severe('Uncaught Promise error: \n' + err.stack);
  142. });
  143.  
  144. bot.config && bot.login(bot.config.botToken);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement