Skip to content

Commit

Permalink
Rename Command#permissions -> callerPermissions
Browse files Browse the repository at this point in the history
  • Loading branch information
zajrik committed Apr 18, 2017
1 parent df8412a commit 1f3c3fe
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 58 deletions.
42 changes: 21 additions & 21 deletions src/command/Command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PermissionResolvable, Message } from 'discord.js';
import { PermissionResolvable, Permissions, Message } from 'discord.js';
import { Client } from '../client/Client';
import { MiddlewareFunction } from '../types/MiddlewareFunction';
import { CommandInfo } from '../types/CommandInfo';
Expand All @@ -22,7 +22,7 @@ export class Command<T extends Client>
public guildOnly: boolean;
public hidden: boolean;
public argOpts: ArgOpts;
public permissions: PermissionResolvable[];
public callerPermissions: PermissionResolvable[];
public roles: string[];
public ownerOnly: boolean;
public overloads: string;
Expand Down Expand Up @@ -104,10 +104,12 @@ export class Command<T extends Client>

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

Expand Down Expand Up @@ -167,46 +169,44 @@ export class Command<T extends Client>
*/
public register(): void
{
const name: string = this.constructor.name;

// Set defaults if not present
if (!this.aliases) this.aliases = [];
if (!this.group) this.group = 'base';
if (!this.guildOnly) this.guildOnly = false;
if (!this.hidden) this.hidden = false;
if (!this.argOpts) this.argOpts = {};
if (!this.argOpts.separator) this.argOpts.separator = ' ';
if (!this.permissions) this.permissions = [];
if (!this.callerPermissions) this.callerPermissions = [];
if (!this.roles) this.roles = [];
if (!this.ownerOnly) this.ownerOnly = false;

// Make necessary asserts
if (!this.name) throw new Error(`You must provide a name for command: ${name}`);
if (!this.description) throw new Error(`You must provide a description for command: ${name}`);
if (!this.usage) throw new Error(`You must provide usage information for command: ${name}`);
if (!this.group) throw new Error(`You must provide a group for command: ${name}`);
if (this.aliases && !Array.isArray(this.aliases)) throw new Error(`Aliases for command ${name} must be an array`);
if (this.permissions && !Array.isArray(this.permissions)) throw new Error(`Permissions for command ${name} must be an array`);
if (this.permissions && this.permissions.length > 0)
for (const [index, perm] of this.permissions.entries())
if (!this.name) throw new Error(`A command is missing a name`);
if (!this.description) throw new Error(`You must provide a description for command: ${this.name}`);
if (!this.usage) throw new Error(`You must provide usage information for command: ${this.name}`);
if (!this.group) throw new Error(`You must provide a group for command: ${this.name}`);
if (this.aliases && !Array.isArray(this.aliases)) throw new Error(`Aliases for command "${this.name}" must be an array`);
if (this.callerPermissions && !Array.isArray(this.callerPermissions)) throw new Error(`callerPermissions for Command "${this.name}" must be an array`);
if (this.callerPermissions && this.callerPermissions.length > 0)
for (const [index, perm] of this.callerPermissions.entries())
{
try
{
Permissions.resolve(perm);
}
catch (err)
{
throw new Error(`Command#${name} permission "${this.permissions[index]}" at ${name}.permissions[${index}] is not a valid permission.\n\n${err}`);
throw new Error(`Command "${this.name}" caller permission "${this.callerPermissions[index]}" at "${this.name}".callerPermissions[${index}] is not a valid permission.\n\n${err}`);
}
}
if (this.roles && !Array.isArray(this.roles)) throw new Error(`Roles for command ${name} must be an array`);
if (this.roles && !Array.isArray(this.roles)) throw new Error(`Roles for command ${this.name} must be an array`);
if (this.overloads && this.group !== 'base') throw new Error('Commands may only overload commands in group "base"');

// Default guildOnly to true if permissions/roles are given
if (this.permissions.length > 0 || this.roles.length > 0) this.guildOnly = true;
if (this.callerPermissions.length > 0 || this.roles.length > 0) this.guildOnly = true;

if (!this.action) throw new Error(`Command#${name}.action: expected Function, got: ${typeof this.action}`);
if (!(this.action instanceof Function)) throw new Error(`Command#${name}.action: expected Function, got: ${typeof this.action}`);
if (!this.action) throw new Error(`Command "${this.name}".action: expected Function, got: ${typeof this.action}`);
if (!(this.action instanceof Function)) throw new Error(`Command "${this.name}".action: expected Function, got: ${typeof this.action}`);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/command/CommandDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ export function argOpts(value: ArgOpts): ClassDecorator
}

/**
* Set `permissions` metadata
* Set `callerPermissions` metadata
* @param {...external:PermissionResolvable} values Values to set
* @returns {ClassDecorator}
*/
export function permissions(...values: PermissionResolvable[]): ClassDecorator
export function callerPermissions(...values: PermissionResolvable[]): ClassDecorator
{
return _setMetaData('permissions', values);
return _setMetaData('callerPermissions', values);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/command/CommandDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class CommandDispatcher<T extends Client>
*/
private checkPermissions(command: Command<T>, message: Message, dm: boolean): PermissionResolvable[]
{
return this._client.selfbot || dm ? [] : command.permissions.filter(a =>
return this._client.selfbot || dm ? [] : command.callerPermissions.filter(a =>
!(<TextChannel> message.channel).permissionsFor(message.author).has(a));
}

Expand Down
32 changes: 16 additions & 16 deletions src/command/CommandLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ export class CommandLoader<T extends Client>
const commandLocation: string = fileName.replace('.js', '');
delete require.cache[require.resolve(commandLocation)];

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

if (this._client.disableBase.includes(<BaseCommandName> _command.name)) continue;
_command._classloc = commandLocation;
if (this._client.disableBase.includes(<BaseCommandName> command.name)) continue;
command._classloc = commandLocation;

if (_command.overloads)
if (command.overloads)
{
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);
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.logger.info('CommandLoader',
`Command '${_command.name}' loaded, overloading command '${_command.overloads}'.`);
`Command '${command.name}' loaded, overloading command '${command.overloads}'.`);
}
else
{
this._client.commands.register(_command, _command.name);
this._client.commands.register(command, command.name);
loadedCommands++;
this.logger.info('CommandLoader', `Command '${_command.name}' loaded.`);
this.logger.info('CommandLoader', `Command '${command.name}' loaded.`);
}
}
this.logger.info('CommandLoader',
Expand All @@ -73,10 +73,10 @@ 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);
_command._classloc = commandLocation;
this._client.commands.register(_command, _command.name, true);
this.logger.info('CommandLoader', `Command '${_command.name}' reloaded.`);
const command: Command<T> = new loadedCommandClass(this._client);
command._classloc = commandLocation;
this._client.commands.register(command, command.name, true);
this.logger.info('CommandLoader', `Command '${command.name}' reloaded.`);
return true;
}

Expand Down
14 changes: 7 additions & 7 deletions src/command/CommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma

/**
* Returns a Promise resolving with a collection of all commands usable
* by the user in the guild text channel the provided message is in.
* by the caller in the guild text channel the provided message is in.
* Needs to be async due to having to access guild settings to check
* for disabled groups
* @param {Client} client YAMDBF Client instance
Expand All @@ -75,13 +75,13 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
(<TextChannel> message.channel).permissionsFor(message.author).has(a);

const byPermissions: (c: V) => boolean = c =>
c.permissions.length > 0 ? c.permissions.filter(currentPermissions).length > 0 : true;
c.callerPermissions.length > 0 ? c.callerPermissions.filter(currentPermissions).length > 0 : true;

const byRoles: (c: V) => boolean = c =>
!(c.roles.length > 0 && message.member.roles.filter(role => c.roles.includes(role.name)).size === 0);

const byOwnerOnly: (c: V) => boolean = c =>
((<any> client.config).owner.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly;
(client.config.owner.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly;

const disabledGroups: string[] = await message.guild.storage.settings.get('disabledGroups') || [];
for (const [name, command] of this.filter(byPermissions).filter(byRoles).filter(byOwnerOnly).entries())
Expand All @@ -91,28 +91,28 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
}

/**
* Returns all commands usable by the user within the DM channel the provided
* Returns all commands usable by the caller within the DM channel the provided
* message is in
* @param {Client} client YAMDBF Client instance
* @param {external:Message} message - Discord.js Message object
* @returns {external:Collection<string, Command>}
*/
public filterDMUsable(client: T, message: Message): Collection<K, V>
{
return this.filter(c => !c.guildOnly && (((<any> client.config).owner
return this.filter(c => !c.guildOnly && ((client.config.owner
.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly));
}

/**
* Returns all commands that can have their help looked up by the user
* Returns all commands that can have their help looked up by the caller
* in the DM channel the message is in
* @param {Client} client YAMDBF Client instance
* @param {external:Message} message Discord.js Message object
* @returns {external:Collection<string, Command>}
*/
public filterDMHelp(client: T, message: Message): Collection<K, V>
{
return this.filter(c => ((<any> client.config).owner
return this.filter(c => (client.config.owner
.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly);
}
}
2 changes: 1 addition & 1 deletion src/command/base/SetPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class extends Command<Client>
aliases: ['prefix'],
usage: '<prefix>setprefix [prefix]',
extraHelp: 'Prefixes may be 1-10 characters in length and may not include backslashes or backticks. Set the prefix to "noprefix" to allow commands to be called without a prefix.',
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/blacklist/Blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class extends Command<Client>
aliases: ['bl'],
usage: '<prefix>blacklist <user>, [\'global\']',
extraHelp: 'If global, this will block the user from calling commands in ANY server and DMs',
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/blacklist/Whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class extends Command<Client>
description: 'Remove a user from the command blacklist',
aliases: ['wl'],
usage: '<prefix>whitelist <user>, [\'global\']',
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/groupcontrol/ClearLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class extends Command<Client>
name: 'clearlimit',
description: 'Clear role restrictions from a command',
usage: '<prefix>clearlimit <command>',
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/groupcontrol/DisableGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class extends Command<Client>
aliases: ['disable', 'dg'],
usage: '<prefix>disablegroup <group>',
extraHelp: 'Disables a command group so that all of the commands in the group cannot be used on this server.',
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/groupcontrol/EnableGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class extends Command<Client>
aliases: ['enable', 'eg'],
usage: '<prefix>enablegroup <group>',
extraHelp: 'Enables a command group so that all of the commands in the group can be used on this server.',
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/groupcontrol/Limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class extends Command<Client>
usage: '<prefix>limit <command>, <role names, ...>',
extraHelp: 'The comma after the command name -- before the role names list -- is necessary.',
argOpts: { separator: ',' },
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/base/groupcontrol/ListGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class extends Command<Client>
aliases: ['lg'],
usage: '<prefix>listgroups',
extraHelp: `A '*' denotes a disabled group when listing all command groups.`,
permissions: ['ADMINISTRATOR']
callerPermissions: ['ADMINISTRATOR']
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/CommandInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @property {boolean} [guildOnly=false] See: {@link Command#guildOnly}
* @property {boolean} [hidden=false] See: {@link Command#hidden}
* @property {ArgOpts} [argOpts] See: {@link Command#argOpts}, {@link ArgOpts}
* @property {PermissionResolvable[]} [permissions=[]] See: {@link Command#permissions}
* @property {PermissionResolvable[]} [callerPermissions=[]] See: {@link Command#callerPermissions}
* @property {string[]} [roles=[]] See: {@link Command#roles}
* @property {boolean} [ownerOnly=false] See: {@link Command#ownerOnly}
* @property {string} [overloads=null] See: {@link Command#overloads}
Expand All @@ -31,7 +31,7 @@ export type CommandInfo = {
guildOnly?: boolean;
hidden?: boolean;
argOpts?: ArgOpts;
permissions?: PermissionResolvable[];
callerPermissions?: PermissionResolvable[];
roles?: string[];
ownerOnly?: boolean;
overloads?: BaseCommandName;
Expand Down

0 comments on commit 1f3c3fe

Please sign in to comment.