Skip to content

Commit

Permalink
Remove client param from Command, only pass CommandInfo to Command super
Browse files Browse the repository at this point in the history
  • Loading branch information
zajrik committed Jun 8, 2017
1 parent d83e41d commit 78820fa
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"program": "${workspaceRoot}/test/test_client.js",
"cwd": "${workspaceRoot}/test",
"outFiles": [],
"preLaunchTask": "tests"
"preLaunchTask": "tests",
"console": "integratedTerminal"
},
{
"type": "node",
Expand Down
10 changes: 6 additions & 4 deletions src/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ArgOpts } from '../types/ArgOpts';
*/
export class Command<T extends Client = Client>
{
public readonly client: T;
public client: T;
public name: string;
public description: string;
public usage: string;
Expand All @@ -32,13 +32,13 @@ export class Command<T extends Client = Client>
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
Expand Down Expand Up @@ -179,8 +179,10 @@ export class Command<T extends Client = Client>
* 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';
Expand Down
8 changes: 4 additions & 4 deletions src/command/CommandLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CommandLoader<T extends Client>
delete require.cache[require.resolve(commandLocation)];

const loadedCommandClass: any = this.getCommandClass(commandLocation);
const command: Command<T> = new loadedCommandClass(this._client);
const command: Command<T> = new loadedCommandClass();

if (this._client.disableBase.includes(<BaseCommandName> command.name)) continue;
command._classloc = commandLocation;
Expand All @@ -46,13 +46,13 @@ export class CommandLoader<T extends Client>
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.`);
}
Expand All @@ -75,7 +75,7 @@ export class CommandLoader<T extends Client>
const loadedCommandClass: any = this.getCommandClass(commandLocation);
const command: Command<T> = 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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/CommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
* replaced in the collection
* @returns {void}
*/
public register(command: V, key: K, reload?: boolean): void
public register(client: T, command: V, key: K, reload?: boolean): void
{
if (super.has(<K> command.name) && !reload && !(command.overloads && command.overloads !== super.get(<K> command.overloads).name))
throw new Error(`A command with the name "${command.name}" already exists.`);
Expand All @@ -36,7 +36,7 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
}
}

command.register();
command.register(client);
super.set(key, <V> command);
}

Expand Down
7 changes: 3 additions & 4 deletions src/command/base/Eval.ts
Original file line number Diff line number Diff line change
@@ -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<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'eval',
description: 'Evaluate provided Javascript code',
usage: '<prefix>eval <...code>',
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/EvalTS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Client } from '../../client/Client';
import { Message } from '../../types/Message';
import { Command } from '../Command';
import { inspect } from 'util';
Expand All @@ -10,11 +9,11 @@ let ts: any;
try { ts = require('typescript'); }
catch (err) {}

export default class extends Command<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'eval:ts',
description: 'Evaluate provided Typescript code',
usage: '<prefix>eval:ts <...code>',
Expand Down
11 changes: 5 additions & 6 deletions src/command/base/Help.ts
Original file line number Diff line number Diff line change
@@ -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<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'help',
description: 'Provides information on bot commands',
usage: `<prefix>help [command]`,
Expand All @@ -22,7 +21,7 @@ export default class extends Command<Client>
const dm: boolean = message.channel.type !== 'text';
const mentionName: string = `@${this.client.user.tag}`;

let command: Command<Client>;
let command: Command;
let output: string = '';
let embed: RichEmbed = new RichEmbed();

Expand All @@ -32,7 +31,7 @@ export default class extends Command<Client>
const postText: string = `\`\`\`Use \`<prefix>help <command>\` ${this.client.selfbot ? '' : `or \`${
mentionName} help <command>\` `}for more info\n\n`;

const usableCommands: Collection<string, Command<Client>> = this.client.commands
const usableCommands: Collection<string, Command> = this.client.commands
.filter(c => !(!this.client.isOwner(message.author) && c.ownerOnly))
.filter(c => !c.hidden);

Expand Down
7 changes: 3 additions & 4 deletions src/command/base/Ping.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Client } from '../../client/Client';
import { Message } from '../../types/Message';
import { Command } from '../Command';

export default class extends Command<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'ping',
description: 'Pong!',
usage: '<prefix>ping'
Expand Down
9 changes: 4 additions & 5 deletions src/command/base/Reload.ts
Original file line number Diff line number Diff line change
@@ -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<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'reload',
description: 'Reload a command or all commands',
usage: '<prefix>reload [command]',
Expand All @@ -19,7 +18,7 @@ export default class extends Command<Client>
public action(message: Message, [commandName]: [string]): Promise<Message | Message[]>
{
const start: number = now();
const command: Command<Client> = 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.`);
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/SetPrefix.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Client } from '../../client/Client';
import { Message } from '../../types/Message';
import { Command } from '../Command';

export default class extends Command<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'setprefix',
description: 'Set or check command prefix',
aliases: ['prefix'],
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/Version.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Client } from '../../client/Client';
import { Message } from '../../types/Message';
import { Command } from '../Command';

export default class extends Command<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'version',
description: 'Get the version of the bot',
usage: `<prefix>version`
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/blacklist/Blacklist.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Client } from '../../../client/Client';
import { Message } from '../../../types/Message';
import { Command } from '../../Command';
import { Middleware } from '../../middleware/Middleware';
import { User, GuildMember } from 'discord.js';
import * as CommandDecorators from '../../CommandDecorators';
const { using } = CommandDecorators;

export default class extends Command<Client>
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'],
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/blacklist/Whitelist.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Client } from '../../../client/Client';
import { Message } from '../../../types/Message';
import { Command } from '../../Command';
import { Middleware } from '../../middleware/Middleware';
import { User } from 'discord.js';
import * as CommandDecorators from '../../CommandDecorators';
const { using } = CommandDecorators;

export default class extends Command<Client>
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'],
Expand Down
9 changes: 4 additions & 5 deletions src/command/base/groupcontrol/ClearLimit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Client } from '../../../client/Client';
import { Message } from '../../../types/Message';
import { Util } from '../../../util/Util';
import { Command } from '../../Command';
Expand All @@ -7,11 +6,11 @@ import { GuildStorage } from '../../../types/GuildStorage';
import * as CommandDecorators from '../../CommandDecorators';
const { using } = CommandDecorators;

export default class extends Command<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'clearlimit',
description: 'Clear role restrictions from a command',
usage: '<prefix>clearlimit <command>',
Expand All @@ -22,7 +21,7 @@ export default class extends Command<Client>
@using(Middleware.expect({ '<command>': 'String' }))
public async action(message: Message, [commandName]: [string]): Promise<Message | Message[]>
{
let command: Command<Client> = 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;
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/groupcontrol/DisableGroup.ts
Original file line number Diff line number Diff line change
@@ -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<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'disablegroup',
description: 'Disable a command group',
aliases: ['disable', 'dg'],
Expand Down
7 changes: 3 additions & 4 deletions src/command/base/groupcontrol/EnableGroup.ts
Original file line number Diff line number Diff line change
@@ -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<Client>
export default class extends Command
{
public constructor(client: Client)
public constructor()
{
super(client, {
super({
name: 'enablegroup',
description: 'Enable a command group',
aliases: ['enable', 'eg'],
Expand Down
9 changes: 4 additions & 5 deletions src/command/base/groupcontrol/Limit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Client } from '../../../client/Client';
import { GuildStorage } from '../../../types/GuildStorage';
import { Message } from '../../../types/Message';
import { Util } from '../../../util/Util';
Expand All @@ -8,11 +7,11 @@ import { Role } from 'discord.js';
import * as CommandDecorators from '../../CommandDecorators';
const { using } = CommandDecorators;

export default class extends Command<Client>
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: '<prefix>limit <command>, <role names, ...>',
Expand All @@ -25,7 +24,7 @@ export default class extends Command<Client>
@using(Middleware.expect({ '<command>': 'String' }))
public async action(message: Message, [commandName, ...roleNames]: [string, string]): Promise<Message | Message[]>
{
const command: Command<Client> = 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.`);

Expand Down
7 changes: 3 additions & 4 deletions src/command/base/groupcontrol/ListGroups.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Client } from '../../../client/Client';
import { Message } from '../../../types/Message';
import { Command } from '../../Command';

export default class extends Command<Client>
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'],
Expand Down
Loading

0 comments on commit 78820fa

Please sign in to comment.