diff --git a/src/Bot.ts b/src/Bot.ts new file mode 100644 index 0000000..a00f17e --- /dev/null +++ b/src/Bot.ts @@ -0,0 +1,38 @@ +//IMPORTS +import { Client, ClientOptions } from 'discord.js'; +import { CommandsManager } from './Commands'; + +//TYPE DEFINITIONS +interface ConfigurationOptions { + prefix: string, + accentColor: string +} +interface ConstructorOptions { + name: string, + prefix: string, + helpCommand?: boolean, + clientOptions?: ClientOptions, + token?: string +} + +//MAIN CLASS +class Bot { + name: string; + client: Client; + commands: CommandsManager; + config: ConfigurationOptions; + + constructor(options: ConstructorOptions) { + this.name = options.name; + this.client = new Client(options.clientOptions); + this.commands = new CommandsManager(); + this.config = { + prefix: options.prefix, + accentColor: "#000" + } + } +} + +//EXPORTS +export default Bot; +export { Bot }; \ No newline at end of file diff --git a/src/Commands.ts b/src/Commands.ts new file mode 100644 index 0000000..a12ca9a --- /dev/null +++ b/src/Commands.ts @@ -0,0 +1,95 @@ +import { Permissions, PermissionResolvable } from 'discord.js'; + +interface CommandBuilder { + name: string, + aliases?: string[] | string, + keywords?: string[] | string, + description?: string, + usage?: string, + permissions: PermissionResolvable +} + +class Command { + name: string; + aliases: string[]; + keywords: string[]; + description: string; + usage: string; + permissions: Permissions; + + constructor(options: CommandBuilder) { + this.name = options.name.split(' ').join('_'); + this.aliases = []; + if(options.aliases) { + this.aliases = ProcessPhrase(options.aliases); + } + this.keywords = []; + if(options.keywords) { + this.keywords = ProcessPhrase(options.keywords); + } + this.description = options.description || "No description"; + this.usage = options.usage || "-"; + this.permissions = new Permissions(options.permissions); + } +} +class CommandsManager { + list: Command[]; + + constructor() { + this.list = []; + } + get(name: string) : Command | undefined { + let command : Command | undefined = undefined; + this.list.map((c) => { + if(c.name == name) { + command = c; + } + }); + return command; + } + add(command: Command) : boolean { + try { + this.list.map((c) => { + if(c.name == command.name) { + throw new Error(`Command with name "${c.name}" already exists!`); + } + command.aliases.map((a) => { + if(c.aliases.indexOf(a) != -1) { + console.warn(`WARN! Alias with name "${a}" already exists in "${c.name}" command. It will be removed from the "${command.name}" command`); + const iToRemove = command.aliases.indexOf(a); + command.aliases.splice(iToRemove, 1); + } + }); + command.keywords.map((k) => { + if(c.keywords.indexOf(k) != -1) { + console.warn(`WARN! Keyword "${k}" already exists in "${c.name}" command. It will be removed from the "${command.name}" command`); + const iToRemove = command.keywords.indexOf(k); + command.keywords.splice(iToRemove, 1); + } + }); + }); + this.list.push(command); + return true; + } + catch(e) { + console.error(`ERROR! ${e.toString()}`); + return false; + } + } +} + +function ProcessPhrase(phrase: string | string[]) : string[] { + if(Array.isArray(phrase)) { + const buff = phrase.map((p) => { + return p.split(' ').join('_'); + }); + return buff; + } + else { + const buff = []; + buff.push(phrase.split(' ').join('_')); + return buff; + } +} + +export { Command, CommandsManager } \ No newline at end of file diff --git a/src/DiscordCommands.ts b/src/DiscordCommands.ts deleted file mode 100644 index 4195d9c..0000000 --- a/src/DiscordCommands.ts +++ /dev/null @@ -1,20 +0,0 @@ -//IMPORTS -import { Client, ClientOptions } from 'discord.js'; - -//TYPE DEFINITIONS -interface ConstructorOptions { - name: string, - prefix: string, - helpCommand?: boolean, - clientOptions?: ClientOptions -} - -//MAIN CLASS -class Bot { - name: string; - client: Client; - constructor(options: ConstructorOptions) { - this.name = options.name; - this.client = new Client(options.clientOptions); - } -} \ No newline at end of file