Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import * as Discord from "discord.js";
  2. import * as ConfigFile from "./config";
  3. import { IBotCommand } from "./api";
  4.  
  5. const client: Discord.Client = new Discord.Client();
  6.  
  7. let commands: IBotCommand[] = [];
  8.  
  9. loadCommands(`${__dirname}/commands`)
  10.  
  11. client.on("ready", () => {
  12.  
  13. //Let us know that the bot is online
  14. console.log("Boss Bot is online!")
  15. })
  16.  
  17. client.on("message", msg => {
  18.  
  19. //Ignore the message if it was sent by the bot
  20. if (msg.author.bot) { return; }
  21.  
  22. //Ignore message that don't start with the prefix
  23. if (!msg.content.startsWith(ConfigFile.config.prefix)) { return; }
  24.  
  25. })
  26.  
  27. async function handleCommand(msg: Discord.Message) {
  28.  
  29. //Split the string into the command and all of the args!
  30. let command = msg.content.split(" ")[0].replace(ConfigFile.config.prefix, "").toLowerCase();
  31. let args = msg.content.split(" ").slice(1);
  32.  
  33. //Loop through all of our commands
  34. for (const CommandsClass of commands) {
  35.  
  36. //Attempt to execute code but ready in case of a crash
  37. try {
  38.  
  39. //Check if our command class is correct.
  40. if (!CommandsClass.isThisCommand(command)) {
  41.  
  42. //Go to the next iteration of the loop if it is not correct.
  43. continue;
  44. }
  45.  
  46. //Pause execution whilst we run the command's code
  47. await CommandsClass.runCommand(args, msg, client);
  48. }
  49. catch (exception) {
  50.  
  51. //If there is an error then log it
  52. console.log(exception);
  53. }
  54. }
  55. }
  56.  
  57. function loadCommands(commandsPath: string) {
  58.  
  59. //Exit if there are no commands
  60. if (!ConfigFile.config || (ConfigFile.config.commands as string[]).length === 0) { return; }
  61.  
  62. //Loop through all of the commands in our config file
  63. for(const commandname of ConfigFile.config.commands as string[] ){
  64.  
  65. const commandClass = require(`${commandsPath}/${commandname}`).default;
  66.  
  67.  
  68. const command = new commandClass() as IBotCommand
  69.  
  70. commands.push(command);
  71.  
  72. }
  73. }
  74.  
  75. client.login(ConfigFile.config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement