From 78820fa93a14b23aef067c040088450a1418266e Mon Sep 17 00:00:00 2001 From: Zack Campbell Date: Thu, 8 Jun 2017 11:53:01 -0500 Subject: [PATCH] Remove client param from Command, only pass CommandInfo to Command super --- .vscode/launch.json | 3 ++- src/command/Command.ts | 10 ++++++---- src/command/CommandLoader.ts | 8 ++++---- src/command/CommandRegistry.ts | 4 ++-- src/command/base/Eval.ts | 7 +++---- src/command/base/EvalTS.ts | 7 +++---- src/command/base/Help.ts | 11 +++++------ src/command/base/Ping.ts | 7 +++---- src/command/base/Reload.ts | 9 ++++----- src/command/base/SetPrefix.ts | 7 +++---- src/command/base/Version.ts | 7 +++---- src/command/base/blacklist/Blacklist.ts | 7 +++---- src/command/base/blacklist/Whitelist.ts | 7 +++---- src/command/base/groupcontrol/ClearLimit.ts | 9 ++++----- src/command/base/groupcontrol/DisableGroup.ts | 7 +++---- src/command/base/groupcontrol/EnableGroup.ts | 7 +++---- src/command/base/groupcontrol/Limit.ts | 9 ++++----- src/command/base/groupcontrol/ListGroups.ts | 7 +++---- test/commands/test_command.ts | 6 +++--- test/test_client.ts | 2 +- 20 files changed, 65 insertions(+), 76 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f118c64a..73f33067 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,8 @@ "program": "${workspaceRoot}/test/test_client.js", "cwd": "${workspaceRoot}/test", "outFiles": [], - "preLaunchTask": "tests" + "preLaunchTask": "tests", + "console": "integratedTerminal" }, { "type": "node", diff --git a/src/command/Command.ts b/src/command/Command.ts index 184c4205..74322326 100644 --- a/src/command/Command.ts +++ b/src/command/Command.ts @@ -12,7 +12,7 @@ import { ArgOpts } from '../types/ArgOpts'; */ export class Command { - public readonly client: T; + public client: T; public name: string; public description: string; public usage: string; @@ -32,13 +32,13 @@ export class Command public readonly _rateLimiter: RateLimiter; public readonly _middleware: MiddlewareFunction[]; - public constructor(client: T, info: CommandInfo = null) + public constructor(info: CommandInfo = null) { /** * YAMDBF Client instance * @type {Client} */ - this.client = client; + this.client = null; /** * The name of the command, used by the dispatcher @@ -179,8 +179,10 @@ export class Command * adding Commands to the CommandRegistry via {@link CommandRegistry#register} * @returns {void} */ - public register(): void + public register(client: T): void { + this.client = client; + // Set defaults if not present if (typeof this.aliases === 'undefined') this.aliases = []; if (typeof this.group === 'undefined') this.group = 'base'; diff --git a/src/command/CommandLoader.ts b/src/command/CommandLoader.ts index d8248848..cb0034b3 100644 --- a/src/command/CommandLoader.ts +++ b/src/command/CommandLoader.ts @@ -36,7 +36,7 @@ export class CommandLoader delete require.cache[require.resolve(commandLocation)]; const loadedCommandClass: any = this.getCommandClass(commandLocation); - const command: Command = new loadedCommandClass(this._client); + const command: Command = new loadedCommandClass(); if (this._client.disableBase.includes( command.name)) continue; command._classloc = commandLocation; @@ -46,13 +46,13 @@ export class CommandLoader if (!this._client.commands.has(command.overloads)) throw new Error(`Command "${command.overloads}" does not exist to be overloaded.`); this._client.commands.delete(command.overloads); - this._client.commands.register(command, command.name); + this._client.commands.register(this._client, command, command.name); this.logger.info('CommandLoader', `Command '${command.name}' loaded, overloading command '${command.overloads}'.`); } else { - this._client.commands.register(command, command.name); + this._client.commands.register(this._client, command, command.name); loadedCommands++; this.logger.info('CommandLoader', `Command '${command.name}' loaded.`); } @@ -75,7 +75,7 @@ export class CommandLoader const loadedCommandClass: any = this.getCommandClass(commandLocation); const command: Command = new loadedCommandClass(this._client); command._classloc = commandLocation; - this._client.commands.register(command, command.name, true); + this._client.commands.register(this._client, command, command.name, true); this.logger.info('CommandLoader', `Command '${command.name}' reloaded.`); return true; } diff --git a/src/command/CommandRegistry.ts b/src/command/CommandRegistry.ts index dbe525bc..123531e4 100644 --- a/src/command/CommandRegistry.ts +++ b/src/command/CommandRegistry.ts @@ -21,7 +21,7 @@ export class CommandRegistry command.name) && !reload && !(command.overloads && command.overloads !== super.get( command.overloads).name)) throw new Error(`A command with the name "${command.name}" already exists.`); @@ -36,7 +36,7 @@ export class CommandRegistry command); } diff --git a/src/command/base/Eval.ts b/src/command/base/Eval.ts index 46d4b739..20a620f1 100644 --- a/src/command/base/Eval.ts +++ b/src/command/base/Eval.ts @@ -1,15 +1,14 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Command } from '../Command'; import { inspect } from 'util'; const Discord = require('discord.js'); // tslint:disable-line const Yamdbf = require('../../index'); // tslint:disable-line -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'eval', description: 'Evaluate provided Javascript code', usage: 'eval <...code>', diff --git a/src/command/base/EvalTS.ts b/src/command/base/EvalTS.ts index d322acf8..472b9f75 100644 --- a/src/command/base/EvalTS.ts +++ b/src/command/base/EvalTS.ts @@ -1,4 +1,3 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Command } from '../Command'; import { inspect } from 'util'; @@ -10,11 +9,11 @@ let ts: any; try { ts = require('typescript'); } catch (err) {} -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'eval:ts', description: 'Evaluate provided Typescript code', usage: 'eval:ts <...code>', diff --git a/src/command/base/Help.ts b/src/command/base/Help.ts index 2b13082d..2ecfd482 100644 --- a/src/command/base/Help.ts +++ b/src/command/base/Help.ts @@ -1,14 +1,13 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Util } from '../../util/Util'; import { Command } from '../Command'; import { Collection, RichEmbed } from 'discord.js'; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'help', description: 'Provides information on bot commands', usage: `help [command]`, @@ -22,7 +21,7 @@ export default class extends Command const dm: boolean = message.channel.type !== 'text'; const mentionName: string = `@${this.client.user.tag}`; - let command: Command; + let command: Command; let output: string = ''; let embed: RichEmbed = new RichEmbed(); @@ -32,7 +31,7 @@ export default class extends Command const postText: string = `\`\`\`Use \`help \` ${this.client.selfbot ? '' : `or \`${ mentionName} help \` `}for more info\n\n`; - const usableCommands: Collection> = this.client.commands + const usableCommands: Collection = this.client.commands .filter(c => !(!this.client.isOwner(message.author) && c.ownerOnly)) .filter(c => !c.hidden); diff --git a/src/command/base/Ping.ts b/src/command/base/Ping.ts index 054b45f9..0053fffc 100644 --- a/src/command/base/Ping.ts +++ b/src/command/base/Ping.ts @@ -1,12 +1,11 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Command } from '../Command'; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'ping', description: 'Pong!', usage: 'ping' diff --git a/src/command/base/Reload.ts b/src/command/base/Reload.ts index 908ef731..3f711c26 100644 --- a/src/command/base/Reload.ts +++ b/src/command/base/Reload.ts @@ -1,13 +1,12 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Command } from '../Command'; import now = require('performance-now'); -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'reload', description: 'Reload a command or all commands', usage: 'reload [command]', @@ -19,7 +18,7 @@ export default class extends Command public action(message: Message, [commandName]: [string]): Promise { const start: number = now(); - const command: Command = this.client.commands.findByNameOrAlias(commandName); + const command: Command = this.client.commands.findByNameOrAlias(commandName); if (commandName && !command) return this.respond(message, `Command "${commandName}" could not be found.`); diff --git a/src/command/base/SetPrefix.ts b/src/command/base/SetPrefix.ts index 14ba57c9..6603af99 100644 --- a/src/command/base/SetPrefix.ts +++ b/src/command/base/SetPrefix.ts @@ -1,12 +1,11 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Command } from '../Command'; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'setprefix', description: 'Set or check command prefix', aliases: ['prefix'], diff --git a/src/command/base/Version.ts b/src/command/base/Version.ts index 7f9ac19e..c0c9eea1 100644 --- a/src/command/base/Version.ts +++ b/src/command/base/Version.ts @@ -1,12 +1,11 @@ -import { Client } from '../../client/Client'; import { Message } from '../../types/Message'; import { Command } from '../Command'; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'version', description: 'Get the version of the bot', usage: `version` diff --git a/src/command/base/blacklist/Blacklist.ts b/src/command/base/blacklist/Blacklist.ts index aa35bc2a..d695551c 100644 --- a/src/command/base/blacklist/Blacklist.ts +++ b/src/command/base/blacklist/Blacklist.ts @@ -1,4 +1,3 @@ -import { Client } from '../../../client/Client'; import { Message } from '../../../types/Message'; import { Command } from '../../Command'; import { Middleware } from '../../middleware/Middleware'; @@ -6,11 +5,11 @@ import { User, GuildMember } from 'discord.js'; import * as CommandDecorators from '../../CommandDecorators'; const { using } = CommandDecorators; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'blacklist', description: 'Blacklist a user from calling commands', aliases: ['bl'], diff --git a/src/command/base/blacklist/Whitelist.ts b/src/command/base/blacklist/Whitelist.ts index 6ec85035..fc6e4ceb 100644 --- a/src/command/base/blacklist/Whitelist.ts +++ b/src/command/base/blacklist/Whitelist.ts @@ -1,4 +1,3 @@ -import { Client } from '../../../client/Client'; import { Message } from '../../../types/Message'; import { Command } from '../../Command'; import { Middleware } from '../../middleware/Middleware'; @@ -6,11 +5,11 @@ import { User } from 'discord.js'; import * as CommandDecorators from '../../CommandDecorators'; const { using } = CommandDecorators; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'whitelist', description: 'Remove a user from the command blacklist', aliases: ['wl'], diff --git a/src/command/base/groupcontrol/ClearLimit.ts b/src/command/base/groupcontrol/ClearLimit.ts index 639b609e..0e533f9e 100644 --- a/src/command/base/groupcontrol/ClearLimit.ts +++ b/src/command/base/groupcontrol/ClearLimit.ts @@ -1,4 +1,3 @@ -import { Client } from '../../../client/Client'; import { Message } from '../../../types/Message'; import { Util } from '../../../util/Util'; import { Command } from '../../Command'; @@ -7,11 +6,11 @@ import { GuildStorage } from '../../../types/GuildStorage'; import * as CommandDecorators from '../../CommandDecorators'; const { using } = CommandDecorators; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'clearlimit', description: 'Clear role restrictions from a command', usage: 'clearlimit ', @@ -22,7 +21,7 @@ export default class extends Command @using(Middleware.expect({ '': 'String' })) public async action(message: Message, [commandName]: [string]): Promise { - let command: Command = this.client.commands.find(c => Util.normalize(c.name) === Util.normalize(commandName)); + let command: Command = this.client.commands.find(c => Util.normalize(c.name) === Util.normalize(commandName)); if (!command) return this.respond(message, `Failed to find a command with the name \`${commandName}\``); const storage: GuildStorage = message.guild.storage; diff --git a/src/command/base/groupcontrol/DisableGroup.ts b/src/command/base/groupcontrol/DisableGroup.ts index 3128f9d7..a0632094 100644 --- a/src/command/base/groupcontrol/DisableGroup.ts +++ b/src/command/base/groupcontrol/DisableGroup.ts @@ -1,15 +1,14 @@ -import { Client } from '../../../client/Client'; import { Message } from '../../../types/Message'; import { Command } from '../../Command'; import { Middleware } from '../../middleware/Middleware'; import * as CommandDecorators from '../../CommandDecorators'; const { using } = CommandDecorators; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'disablegroup', description: 'Disable a command group', aliases: ['disable', 'dg'], diff --git a/src/command/base/groupcontrol/EnableGroup.ts b/src/command/base/groupcontrol/EnableGroup.ts index da952dc6..21249c53 100644 --- a/src/command/base/groupcontrol/EnableGroup.ts +++ b/src/command/base/groupcontrol/EnableGroup.ts @@ -1,15 +1,14 @@ -import { Client } from '../../../client/Client'; import { Message } from '../../../types/Message'; import { Command } from '../../Command'; import { Middleware } from '../../middleware/Middleware'; import * as CommandDecorators from '../../CommandDecorators'; const { using } = CommandDecorators; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'enablegroup', description: 'Enable a command group', aliases: ['enable', 'eg'], diff --git a/src/command/base/groupcontrol/Limit.ts b/src/command/base/groupcontrol/Limit.ts index ea36e545..d655b4ed 100644 --- a/src/command/base/groupcontrol/Limit.ts +++ b/src/command/base/groupcontrol/Limit.ts @@ -1,4 +1,3 @@ -import { Client } from '../../../client/Client'; import { GuildStorage } from '../../../types/GuildStorage'; import { Message } from '../../../types/Message'; import { Util } from '../../../util/Util'; @@ -8,11 +7,11 @@ import { Role } from 'discord.js'; import * as CommandDecorators from '../../CommandDecorators'; const { using } = CommandDecorators; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'limit', description: 'Limit a command to the provided roles', usage: 'limit , ', @@ -25,7 +24,7 @@ export default class extends Command @using(Middleware.expect({ '': 'String' })) public async action(message: Message, [commandName, ...roleNames]: [string, string]): Promise { - const command: Command = this.client.commands.find(c => Util.normalize(commandName) === Util.normalize(c.name)); + const command: Command = this.client.commands.find(c => Util.normalize(commandName) === Util.normalize(c.name)); if (!command) return this.respond(message, `Failed to find a command with the name \`${commandName}\``); if (command.group === 'base') this.respond(message, `Cannot limit base commands.`); diff --git a/src/command/base/groupcontrol/ListGroups.ts b/src/command/base/groupcontrol/ListGroups.ts index 440c9621..631cd95e 100644 --- a/src/command/base/groupcontrol/ListGroups.ts +++ b/src/command/base/groupcontrol/ListGroups.ts @@ -1,12 +1,11 @@ -import { Client } from '../../../client/Client'; import { Message } from '../../../types/Message'; import { Command } from '../../Command'; -export default class extends Command +export default class extends Command { - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'listgroups', description: 'List all command groups and their status', aliases: ['lg'], diff --git a/test/commands/test_command.ts b/test/commands/test_command.ts index 59d1f94c..d05cc363 100644 --- a/test/commands/test_command.ts +++ b/test/commands/test_command.ts @@ -8,16 +8,16 @@ import * as util from 'util'; export default class extends Command { @logger private readonly logger: Logger; - public constructor(client: Client) + public constructor() { - super(client, { + super({ name: 'test', description: 'test command', usage: 'test ' }); } - @using((message, args) => [message, args.map(a => a.toUpperCase())]) + // @using((message, args) => [message, args.map(a => a.toUpperCase())]) @using(Middleware.resolve({ '': 'Duration' })) // @using(Middleware.expect({ '': ['foo', 'bar', 'baz'] })) public action(message: Message, args: string[]): void diff --git a/test/test_client.ts b/test/test_client.ts index cb61fc17..8ec0aff8 100644 --- a/test/test_client.ts +++ b/test/test_client.ts @@ -25,7 +25,7 @@ class Test extends Client public constructor() { super({ - name: 'test', + name: 'tests', token: config.token, owner: config.owner, commandsDir: './commands',