Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     //check if bot is online.
  13.     console.log("Miku is now online!!");
  14. })
  15.  
  16. client.on("message", msg => {
  17.     //ignore bot-sent messages
  18.     if(msg.author.bot) { return; }
  19.  
  20.     //ignore no prefix messages
  21.     if(!msg.content.startsWith(configFile.config.prefix)) {return; }
  22.    
  23.     // msg.channel.send(`${msg.author.username} talked to me.`);
  24.  
  25.     handleCommand(msg);
  26. })
  27.  
  28. async function handleCommand(msg: Discord.Message){
  29.     //Split the string into the command and all the args
  30.     let command = msg.content.split("")[0].replace(configFile.config.prefix, "");
  31.     let args = msg.content.split("").slice(1);
  32.  
  33.     //Looping through all of the bot's loaded commands
  34.     for(const commandClass of commands){
  35.         //test to execute code but ready for possible errors
  36.         try{
  37.             //Check if our command class is correct
  38.             if(!commandClass.isThisCommand(command)){
  39.  
  40.                 //Next iteration if false
  41.                 continue;
  42.             }
  43.            
  44.             //pause execution while running the command's code
  45.             await commandClass.runCommand(args, msg, client);
  46.         }
  47.         catch(exception){
  48.             //if error, copy the exception
  49.             console.log(exception);
  50.         }
  51.     }
  52. }
  53.  
  54. function loadCommands(commandsPath: string){
  55.     //Exit if there are 0 commands
  56.     if(!configFile.config || (configFile.config.commands as string[]).length === 0){return;}
  57.  
  58.     //Loop through commands in config
  59.     for(const commandName of configFile.config.commands as string[]){
  60.         const commandsClass = require(`${commandsPath}/${commandName}`).default;
  61.         const command = new commandsClass() as IBotCommand;
  62.         commands.push(command);
  63.     }
  64. }
  65.  
  66.  
  67. client.login(configFile.config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement