Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. const { Client, Collection } = require("discord.js");
  2. const { promisify } = require("util");
  3. const readdir = promisify(require("fs").readdir);
  4. const Enmap = require("enmap");
  5. const klaw = require("klaw");
  6. const path = require("path");
  7. const Discord = require('discord.js');
  8. require("dotenv").config();
  9. const axios = require('axios');
  10. const config = require('./config.json');
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. class theoldtown extends Client {
  18. constructor(options) {
  19. super(options);
  20.  
  21. this.points = new Enmap({
  22. name: "points",
  23. cloneLevel: "deep",
  24. fetchAll: true,
  25. autoFetch: true
  26. });
  27.  
  28. this.config = require("./config.js");
  29.  
  30. this.commands = new Collection();
  31. this.aliases = new Collection();
  32. this.queue = new Map();
  33.  
  34. this.settings = new Enmap({
  35. name: "settings",
  36. cloneLevel: "deep",
  37. fetchAll: false,
  38. autoFetch: true
  39. });
  40.  
  41. this.warns = new Enmap({
  42. name: "warns",
  43. cloneLevel: "deep",
  44. fetchAll: true,
  45. autoFetch: true
  46.  
  47.  
  48. });
  49.  
  50. this.logger = require("./modules/Logger");
  51. this.wait = require("util").promisify(setTimeout);
  52. }
  53.  
  54. // Permission
  55.  
  56. permlevel(message) {
  57. let permlvl = 0;
  58.  
  59. const permOrder = this.config.permLevels
  60. .slice(0)
  61. .sort((p, c) => (p.level < c.level ? 1 : -1));
  62.  
  63. while (permOrder.length) {
  64. const currentLevel = permOrder.shift();
  65. if (message.guild && currentLevel.guildOnly) continue;
  66. if (currentLevel.check(message)) {
  67. permlvl = currentLevel.level;
  68. break;
  69. }
  70. }
  71. return permlvl;
  72. }
  73.  
  74. loadCommand(commandPath, commandName) {
  75. try {
  76. const props = new (require(`${commandPath}${path.sep}${commandName}`))(
  77. this
  78. );
  79. this.logger.log(`Chargement de la commande: ${props.help.name}`, "log");
  80. props.conf.location = commandPath;
  81. if (props.init) {
  82. props.init(this);
  83. }
  84. this.commands.set(props.help.name, props);
  85. props.conf.aliases.forEach(alias => {
  86. this.aliases.set(alias, props.help.name);
  87. });
  88. return false;
  89. } catch (e) {
  90. return `Impossible de charger la commande ${commandName}: ${e}`;
  91. }
  92. }
  93.  
  94. async unloadCommand(commandPath, commandName) {
  95. let command;
  96. if (this.command.has(commandName)) {
  97. command = this.commands.get(commandName);
  98. } else if (this.aliases.has(commandName)) {
  99. command = this.commands.get(this.aliases.get(commandName));
  100. }
  101. if (!command)
  102. return `La commande \`${commandName}\` ne semble pas exister. Essayez à nouveau!`;
  103.  
  104. if (command.shutdown) {
  105. await command.shutdown(this);
  106. }
  107. delete require.cache[
  108. require.resolve(`${commandPath}${path.sep}${commandName}.js`)
  109. ];
  110. return false;
  111. }
  112.  
  113. getSettings(guild) {
  114. const defaults = this.config.defaultSettings || {};
  115. const guildData = this.settings.get(guild.id) || {};
  116. const returnObject = {};
  117. Object.keys(defaults).forEach(key => {
  118. returnObject[key] = guildData[key] ? guildData[key] : defaults[key];
  119. });
  120. return returnObject;
  121. }
  122. }
  123.  
  124. const client = new theoldtown();
  125. console.log(client.config.permLevels.map(p => `${p.level}: ${p.name}`));
  126.  
  127. // Fonction d'initialisation
  128.  
  129. const init = async () => {
  130. // Récupération des commandes
  131. klaw("./commands").on("data", item => {
  132. const cmdFile = path.parse(item.path);
  133. if (!cmdFile.ext || cmdFile.ext !== ".js") return;
  134. const response = client.loadCommand(
  135. cmdFile.dir,
  136. `${cmdFile.name}${cmdFile.ext}`
  137. );
  138. if (response) client.logger.error(response);
  139. });
  140.  
  141. // Récupération des événements
  142. const evtFiles = await readdir("./events");
  143. client.logger.log(`Chargement de ${evtFiles.length} événements.`, "log");
  144. evtFiles.forEach(file => {
  145. const eventName = file.split(".")[0];
  146. client.logger.log(`Chargement de l'événement: ${eventName}`);
  147. const event = new (require(`./events/${file}`))(client);
  148. client.on(eventName, (...args) => event.run(...args));
  149. delete require.cache[require.resolve(`./events/${file}`)];
  150. });
  151.  
  152. client.levelCache = {};
  153. for (let i = 0; i < client.config.permLevels.length; i++) {
  154. const thisLevel = client.config.permLevels[i];
  155. client.levelCache[thisLevel.name] = thisLevel.level;
  156. }
  157.  
  158. client.login(config.token)
  159. client.on('error', console.error);
  160. };
  161.  
  162. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement