Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as Discord from "discord.js";
- import * as configFile from "./config";
- import { IBotCommand } from "./api";
- const client: Discord.Client = new Discord.Client();
- let commands: IBotCommand[] = [];
- loadCommands(`${__dirname}/commands`)
- client.on("ready", () => {
- //check if bot is online.
- console.log("Miku is now online!!");
- })
- client.on("message", msg => {
- //ignore bot-sent messages
- if(msg.author.bot) { return; }
- //ignore no prefix messages
- if(!msg.content.startsWith(configFile.config.prefix)) {return; }
- // msg.channel.send(`${msg.author.username} talked to me.`);
- handleCommand(msg);
- })
- async function handleCommand(msg: Discord.Message){
- //Split the string into the command and all the args
- let command = msg.content.split("")[0].replace(configFile.config.prefix, "");
- let args = msg.content.split("").slice(1);
- //Looping through all of the bot's loaded commands
- for(const commandClass of commands){
- //test to execute code but ready for possible errors
- try{
- //Check if our command class is correct
- if(!commandClass.isThisCommand(command)){
- //Next iteration if false
- continue;
- }
- //pause execution while running the command's code
- await commandClass.runCommand(args, msg, client);
- }
- catch(exception){
- //if error, copy the exception
- console.log(exception);
- }
- }
- }
- function loadCommands(commandsPath: string){
- //Exit if there are 0 commands
- if(!configFile.config || (configFile.config.commands as string[]).length === 0){return;}
- //Loop through commands in config
- for(const commandName of configFile.config.commands as string[]){
- const commandsClass = require(`${commandsPath}/${commandName}`).default;
- const command = new commandsClass() as IBotCommand;
- commands.push(command);
- }
- }
- client.login(configFile.config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement