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", () => {
- //Let us know that the bot is online
- console.log("Boss Bot is online!")
- })
- client.on("message", msg => {
- //Ignore the message if it was sent by the bot
- if (msg.author.bot) { return; }
- //Ignore message that don't start with the prefix
- if (!msg.content.startsWith(ConfigFile.config.prefix)) { return; }
- })
- async function handleCommand(msg: Discord.Message) {
- //Split the string into the command and all of the args!
- let command = msg.content.split(" ")[0].replace(ConfigFile.config.prefix, "").toLowerCase();
- let args = msg.content.split(" ").slice(1);
- //Loop through all of our commands
- for (const CommandsClass of commands) {
- //Attempt to execute code but ready in case of a crash
- try {
- //Check if our command class is correct.
- if (!CommandsClass.isThisCommand(command)) {
- //Go to the next iteration of the loop if it is not correct.
- continue;
- }
- //Pause execution whilst we run the command's code
- await CommandsClass.runCommand(args, msg, client);
- }
- catch (exception) {
- //If there is an error then log it
- console.log(exception);
- }
- }
- }
- function loadCommands(commandsPath: string) {
- //Exit if there are no commands
- if (!ConfigFile.config || (ConfigFile.config.commands as string[]).length === 0) { return; }
- //Loop through all of the commands in our config file
- for(const commandname of ConfigFile.config.commands as string[] ){
- const commandClass = require(`${commandsPath}/${commandname}`).default;
- const command = new commandClass() as IBotCommand
- commands.push(command);
- }
- }
- client.login(ConfigFile.config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement