import 'reflect-metadata'; const log = console.log; import { BotCommand, BotPlugin } from 'psyduck-contracts'; import { BotOptions } from './BotOptions'; import { HelpCommand } from '../commands/Help'; import * as Discord from 'discord.js'; import Chalk from 'chalk'; import CommandHandler from '../plugins/commands/CommandHandler'; import fs from 'fs'; import LoggerPlugin from '../plugins/logger/LoggerPlugin'; class Bot { private client: Discord.Client; private commandHandler: CommandHandler; private discordToken: string; private helpCommand?: string; private plugins: Array; constructor(options: BotOptions) { this.commandHandler = new CommandHandler(); this.discordToken = options.Token; this.helpCommand = options.HelpCommand; // Initialize with included plugins this.plugins = [LoggerPlugin, this.commandHandler]; // Register included commands this.registerCommand(new HelpCommand(this.helpCommand)); this.client = new Discord.Client(); this.startClient(); } addPlugin(plugin: BotPlugin): Bot { this.plugins.push(plugin); return this; } addPlugins(...plugins: BotPlugin[]) { this.plugins.push(...plugins); } onMessage(message: Discord.Message) { this.plugins.forEach(plugin => { plugin.onMessage(message); }); } registerCommand(command: BotCommand) { if (!command) { log(Chalk.black.bgRedBright('Unable to register null command')); } else if (!command.commandPrefix || command.commandPrefix.length === 0) { log(Chalk.black.bgRedBright('Unable to register command without a prefix')); } else if (!command.commandCodes || command.commandCodes.length === 0) { log(Chalk.black.bgRedBright('Unable to register command without command codes')); } else { this.commandHandler.registerCommand(command); } } async startClient() { try { await this.client.login(this.discordToken); this.client.on('message', message => this.onMessage(message)); } catch (error) { } } } export { Bot }; export { Bot as Client };