Skip to content

Commit

Permalink
Update Command#_respond -> respond, simplify Command doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zajrik committed Mar 30, 2017
1 parent 9c67a2a commit 5470a51
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 76 deletions.
71 changes: 20 additions & 51 deletions src/lib/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,133 +35,105 @@ export class Command<T extends Bot>
public constructor(bot: T, info: CommandInfo = null)
{
/**
* Bot instance
* @memberof Command
* @type {Bot}
* @name bot
* @instance
* YAMDBF Client instance
* @name Command#client
* @type {Client}
*/
this.bot = bot;

/**
* The name of the command, used by the dispatcher
* to determine the command being executed
* @memberof Command
* @name Command#name
* @type {string}
* @name name
* @instance
*/

/**
* A brief description of the command, displayed
* in the commands list via the Help command
* @memberof Command
* @name Command#description
* @type {string}
* @name description
* @instance
*/

/**
* An example of command usage. The token '&lt;prefix&gt;' will
* be replaced by the guild-specific command prefix in the Help command when
* 'help &lt;command&gt;' is called
* @memberof Command
* @name Command#usage
* @type {string}
* @name usage
* @instance
*/

/**
* Extra information about the command to be displayed
* by the Help command when 'help &lt;command&gt;' is called
* @memberof Command
* @name Command#extraHelp
* @type {string}
* @name extraHelp
* @instance
*/

/**
* The command group that the command belongs to. Allows commands to be
* grouped for disabling. The group 'base' cannot be disabled.
* @memberof Command
* @name Command#group
* @type {string}
* @name group
* @instance
*/

/**
* Aliases the command can be called by other than its name
* @memberof Command
* @name Command#aliases
* @type {string[]}
* @name aliases
* @instance
*/

/**
* Whether or not a command can only be used within a
* guild text channel
* @memberof Command
* @name Command#guildOnly
* @type {boolean}
* @name guildOnly
* @instance
*/

/**
* Whether or not the command is to be hidden from the
* commands list via the default help command
* @memberof Command
* @name Command#hidden
* @type {boolean}
* @name hidden
* @instance
*/

/**
* Options for how arguments should be parsed. See: {@link ArgOpts}
* @memberof Command
* @name Command#argOpts
* @type {ArgOpts}
* @name argOpts
* @instance
*/

/**
* Array of permissions required by the command
* caller to be able to execute the command in the guild the command is called in.
* <br><br>
* If any permissions are provided the command's `guildOnly` property will be automatically set to true
* @memberof Command
* @name Command#permissions
* @type {external:PermissionResolvable[]}
* @name permissions
* @instance
*/

/**
* Array of roles required to use the command. If the command caller
* has any of the roles in the array, they will be able to use the command
* <br><br>
* If any roles are provided the command's `guildOnly` property will be automatically set to true
* @memberof Command
* @name Command#roles
* @type {string[]}
* @name roles
* @instance
*/

/**
* Whether or not the command can be used by the bot owner(s).
* @memberof Command
* @name Command#ownerOnly
* @type {boolean}
* @name ownerOnly
* @instance
* @see [Bot#config.owner]{@link Bot#config}
*/

/**
* The name of a base command to overload. Commands may only overload
* base commands so the {@link Command#group} must be set to 'base' in
* order to overload. Commands may only be overloaded by name, not by alias
* @memberof Command
* @name Command#overloads
* @type {string}
* @name overloads
* @instance
*/

// Middleware function storage for the Command instance
Expand All @@ -179,8 +151,7 @@ export class Command<T extends Bot>
* are what command actions will be passed by the {@link CommandDispatcher} whenever
* a command is called. Be sure to receive these in proper order when writing
* new commands
* @memberof Command
* @instance
* @method Command#action
* @param {external:Message} message Discord.js message object
* @param {any[]} args An array containing the args parsed from the command calling message.<br>
* Will contain strings unless middleware is used to transform the args
Expand All @@ -193,8 +164,7 @@ export class Command<T extends Bot>
/**
* Assert {@link Command#action} is typeof Function, finishing the
* command creation process.<br>Called by {@link CommandRegistry#register}
* @memberof Command
* @instance
* @method Command#register
*/
public register(): void
{
Expand Down Expand Up @@ -264,8 +234,7 @@ export class Command<T extends Bot>
* Note: Middleware functions should only be added to a command one time each,
* and thus should be added in the Command's constructor. Multiple middleware
* functions can be added to a command via multiple calls to this method
* @memberof Command
* @instance
* @method Command#use
* @param {MiddlewareFunction} fn Middleware function. <code>(message, args) => [message, args]</code>
* @returns {Command} This command instance
*/
Expand All @@ -281,7 +250,7 @@ export class Command<T extends Bot>
* or not the bot is a selfbot and/or a codeblock language is given
* @protected
*/
protected _respond(message: Message, response: string, code?: string): Promise<Message | Message[]>
protected respond(message: Message, response: string, code?: string): Promise<Message | Message[]>
{
if (this.bot.selfbot && !code) return message.edit(response);
if (this.bot.selfbot && code) return message.editCode(code, response);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/command/base/Eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class extends Command<Bot>
const code: string = message.content.split(this.name).slice(1).join(this.name).trim();
if (!code)
{
this._respond(message, '**ERROR:** ```xl\nNo code provided to evaluate.\n```');
this.respond(message, '**ERROR:** ```xl\nNo code provided to evaluate.\n```');
return;
}

Expand All @@ -33,27 +33,27 @@ export default class extends Command<Bot>
}
catch (err)
{
return this._respond(message,
return this.respond(message,
`**INPUT:**\n\`\`\`js\n${code}\n\`\`\`\n**ERROR:**\n\`\`\`xl\n${this._clean(err)}\n\`\`\``);
}
if (evaled instanceof Promise)
{
evaled.then(res =>
{
if (typeof res !== 'string') res = inspect(res, { depth: 0 });
this._respond(message,
this.respond(message,
`**INPUT:**\n\`\`\`js\n${code}\n\`\`\`\n**OUTPUT:**\n\`\`\`xl\n${this._clean(res)}\n\`\`\``);
})
.catch(err =>
{
this._respond(message,
this.respond(message,
`**INPUT:**\n\`\`\`js\n${code}\n\`\`\`\n**ERROR:**\n\`\`\`xl\n${this._clean(err)}\n\`\`\``);
});
}
else
{
if (typeof evaled !== 'string') evaled = inspect(evaled, { depth: 0 });
return this._respond(message,
return this.respond(message,
`**INPUT:**\n\`\`\`js\n${code}\n\`\`\`\n**OUTPUT:**\n\`\`\`xl\n${this._clean(evaled)}\n\`\`\``);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/command/base/Reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export default class extends Command<Bot>
const command: Command<Bot> = this.bot.commands.findByNameOrAlias(commandName);

if (commandName && !command)
return this._respond(message, `Command "${commandName}" could not be found.`);
return this.respond(message, `Command "${commandName}" could not be found.`);

if (command) this.bot.loadCommand(command.name);
else this.bot.loadCommand('all');

const end: number = now();
const name: string = command ? command.name : null;
const text: string = name ? ` "${name}"` : 's';
return this._respond(message, `Command${text} reloaded. (${(end - start).toFixed(3)} ms)`);
return this.respond(message, `Command${text} reloaded. (${(end - start).toFixed(3)} ms)`);
}
}
8 changes: 4 additions & 4 deletions src/lib/command/base/SetPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export default class extends Command<Bot>
public async action(message: Message, [prefix]: [string]): Promise<any>
{
if (!prefix)
return this._respond(message, `${this.bot.getPrefix(message.guild)
return this.respond(message, `${this.bot.getPrefix(message.guild)
? `Current prefix is \`${this.bot.getPrefix(message.guild)}\``
: 'There is currently no prefix.'}`);

if (prefix.length > 10)
return this._respond(message, `Prefixes may only be up to 10 chars in length.`);
return this.respond(message, `Prefixes may only be up to 10 chars in length.`);

if (/[\\`]/.test(prefix))
return this._respond(message, `Prefixes may not contain backticks or backslashes.`);
return this.respond(message, `Prefixes may not contain backticks or backslashes.`);

if (prefix === 'noprefix') prefix = '';

Expand All @@ -40,7 +40,7 @@ export default class extends Command<Bot>
await guild.settings.set('prefix', prefix);

else await this.bot.storage.guilds.get(message.guild.id).settings.set('prefix', prefix);
this._respond(message, prefix === '' ? 'Command prefix removed.'
this.respond(message, prefix === '' ? 'Command prefix removed.'
: `Command prefix set to \`${prefix}\``);
}
}
2 changes: 1 addition & 1 deletion src/lib/command/base/Version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export default class extends Command<Bot>

public action(message: Message): void
{
this._respond(message, `Current version is: **${this.bot.version}**`);
this.respond(message, `Current version is: **${this.bot.version}**`);
}
}
4 changes: 2 additions & 2 deletions src/lib/command/base/groupcontrol/ClearLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export default class extends Command<Bot>
public async action(message: Message, [commandName]: [string]): Promise<Message | Message[]>
{
let command: Command<Bot> = this.bot.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}\``);
if (!command) return this.respond(message, `Failed to find a command with the name \`${commandName}\``);

const storage: GuildStorage = message.guild.storage;
let limitedCommands: { [name: string]: string[] } = await storage.settings.get('limitedCommands') || {};
delete limitedCommands[command.name];
storage.settings.set('limitedCommands', limitedCommands);

return this._respond(message, `Successfully cleared role limits for command: \`${command.name}\``);
return this.respond(message, `Successfully cleared role limits for command: \`${command.name}\``);
}
}
6 changes: 3 additions & 3 deletions src/lib/command/base/groupcontrol/DisableGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export default class extends Command<Bot>
DISABLED: `Command group "${group}" is already disabled or is not allowed to be disabled.`
};

if (!this.bot.commands.groups.includes(group)) return this._respond(message, err.NO_EXIST);
if (!this.bot.commands.groups.includes(group)) return this.respond(message, err.NO_EXIST);
const disabledGroups: string[] = await message.guild.storage.settings.get('disabledGroups') || [];
if (group === 'base' || disabledGroups.includes(group))
return this._respond(message, err.DISABLED);
return this.respond(message, err.DISABLED);

disabledGroups.push(group);
await message.guild.storage.settings.set('disabledGroups', disabledGroups);

this._respond(message, `**Disabled command group "${group}"**`);
this.respond(message, `**Disabled command group "${group}"**`);
}
}
6 changes: 3 additions & 3 deletions src/lib/command/base/groupcontrol/EnableGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export default class extends Command<Bot>
ENABLED: `Command group ${group} is already enabled.`
};

if (!this.bot.commands.groups.includes(group)) return this._respond(message, err.NO_EXIST);
if (!this.bot.commands.groups.includes(group)) return this.respond(message, err.NO_EXIST);
const disabledGroups: string[] = await message.guild.storage.settings.get('disabledGroups') || [];
if (group === 'base' || !disabledGroups.includes(group))
return this._respond(message, err.ENABLED);
return this.respond(message, err.ENABLED);

disabledGroups.splice(disabledGroups.indexOf(group), 1);
await message.guild.storage.settings.set('disabledGroups', disabledGroups);

this._respond(message, `**Enabled command group "${group}"**`);
this.respond(message, `**Enabled command group "${group}"**`);
}
}
8 changes: 4 additions & 4 deletions src/lib/command/base/groupcontrol/Limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export default class extends Command<Bot>
public async action(message: Message, [commandName, ...roleNames]: [string, string]): Promise<Message | Message[]>
{
const command: Command<Bot> = this.bot.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.`);
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.`);

const storage: GuildStorage = message.guild.storage;
let limitedCommands: { [name: string]: string[] } = await storage.settings.get('limitedCommands') || {};
Expand All @@ -46,13 +46,13 @@ export default class extends Command<Bot>

if (invalidRoles.length > 0) message.channel.send(`Couldn't find role${
invalidRoles.length > 1 ? 's' : ''}: \`${invalidRoles.join('`, `')}\``);
if (roles.length === 0) return this._respond(message, `Failed to add any roles to the command.`);
if (roles.length === 0) return this.respond(message, `Failed to add any roles to the command.`);

newLimit = newLimit.concat(roles.map(role => role.id));
limitedCommands[command.name] = newLimit;
await storage.settings.set('limitedCommands', limitedCommands);

return this._respond(message, `Successfully added role${roles.length > 1 ? 's' : ''}: \`${
return this.respond(message, `Successfully added role${roles.length > 1 ? 's' : ''}: \`${
roles.map(role => role.name).join('`, `')}\` to the limiter for command: \`${command.name}\``);
}
}
2 changes: 1 addition & 1 deletion src/lib/command/base/groupcontrol/ListGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export default class extends Command<Bot>
let output: string = 'Command groups:\n';
for (const group of groups) output += `${disabledGroups.includes(group) ? '*' : ' '}${group}\n`;

this._respond(message, output, 'ldif');
this.respond(message, output, 'ldif');
}
}

0 comments on commit 5470a51

Please sign in to comment.