Skip to content

Commit

Permalink
Remove YAMDBFOptions/Client#config, add YAMDBFOptions/Client#owner
Browse files Browse the repository at this point in the history
This is optional, and can be a single string or an array of strings. These strings are expected to be User IDs but there is no consequence if they are not. If no owner/s are given, ownerOnly flagged commands will be unusable by anyone.
  • Loading branch information
zajrik committed May 13, 2017
1 parent 28f9fdd commit 1945eaa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
28 changes: 14 additions & 14 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Client extends Discord.Client
@logger private readonly _logger: Logger;
public readonly name: string;
public readonly commandsDir: string;
public readonly owner: string | string[];
public readonly statusText: string;
public readonly readyText: string;
public readonly unknownCommandError: boolean;
Expand All @@ -56,7 +57,6 @@ export class Client extends Discord.Client
public readonly pause: boolean;
public readonly version: string;
public readonly disableBase: BaseCommandName[];
public readonly config: any;
public readonly provider: StorageProviderConstructor;
public readonly _middleware: MiddlewareFunction[];
public readonly _rateLimiter: RateLimiter;
Expand Down Expand Up @@ -84,6 +84,16 @@ export class Client extends Discord.Client
*/
this.name = options.name || 'botname';

/**
* The owner/owners of the bot, represented as an array of IDs.
* These IDs determine who is allowed to use commands flagged as
* `ownerOnly`
* @type {string[]}
*/
this.owner = options.owner instanceof Array ?
options.owner : typeof options.owner !== 'undefined' ?
[options.owner] : [];

/**
* Directory to find command class files. Optional
* if client is passive.<br>
Expand Down Expand Up @@ -151,14 +161,6 @@ export class Client extends Discord.Client
*/
this.version = options.version || '0.0.0';

/**
* Object containing token and owner ids
* @type {Object}
* @property {string} token - Discord login token for the client
* @property {string[]} owner - Array of owner id strings
*/
this.config = options.config || null;

/**
* Array of base command names to skip when loading commands. Base commands
* may only be disabled by name, not by alias
Expand Down Expand Up @@ -203,9 +205,7 @@ export class Client extends Discord.Client
// Make some asserts
if (!this._token) throw new Error('A token must be provided for the client');
if (!this.commandsDir && !this.passive) throw new Error('A directory from which to load commands must be provided via commandsDir');
if (!this.config) throw new Error('A config containing containing `token` string and `owner` ID string array must be provided');
if (!this.config.owner) throw new Error('Provided Client config is missing owner ID string array');
if (!(this.config.owner instanceof Array)) throw new TypeError('Client config `owner` field must be an array of user ID strings.');
if (!(this.owner instanceof Array)) throw new TypeError('Client config `owner` field must be an array of user ID strings.');

// Load commands
if (!this.passive) this.loadCommand('all');
Expand Down Expand Up @@ -260,7 +260,7 @@ export class Client extends Discord.Client
//#endregion

/**
* Logs the Client in and registers some event handlers
* Starts the login process, culminating in the `clientReady` event
* @returns {Client}
*/
public start(): this
Expand Down Expand Up @@ -289,7 +289,7 @@ export class Client extends Discord.Client
*/
public isOwner(user: User): boolean
{
return this.config.owner.includes(user.id);
return this.owner.includes(user.id);
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/command/CommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
!(c.roles.length > 0 && message.member.roles.filter(role => c.roles.includes(role.name)).size === 0);

const byOwnerOnly: (c: V) => boolean = c =>
(client.config.owner.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly;
(client.isOwner(message.author) && 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 @@ -99,8 +99,8 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
*/
public filterDMUsable(client: T, message: Message): Collection<K, V>
{
return this.filter(c => !c.guildOnly && ((client.config.owner
.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly));
return this.filter(c => !c.guildOnly &&
((client.isOwner(message.author) && c.ownerOnly) || !c.ownerOnly));
}

/**
Expand All @@ -112,7 +112,6 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
*/
public filterDMHelp(client: T, message: Message): Collection<K, V>
{
return this.filter(c => (client.config.owner
.includes(message.author.id) && c.ownerOnly) || !c.ownerOnly);
return this.filter(c => (client.isOwner(message.author) && c.ownerOnly) || !c.ownerOnly);
}
}
4 changes: 2 additions & 2 deletions src/types/YAMDBFOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* passed to a Client on construction
* @property {string} [name='botname'] See: {@link Client#name}
* @property {string} token See: {@link Client#token}
* @property {string | string[]} [owner] See: {@link Client#owner}
* @property {string} [provider] See: {@link Client#provider}
* @property {string} [commandsDir] See: {@link Client#commandsDir}
* @property {string} [statusText=null] See: {@link Client#statusText}
Expand All @@ -12,7 +13,6 @@
* @property {boolean} [passive=false] See: {@link Client#passive}
* @property {boolean} [pause=false] See: {@link Client#pause}
* @property {string} [version='0.0.0'] See: {@link Client#version}
* @property {Object} config See: {@link Client#config}
* @property {string[]} [disableBase=[]] See: {@link Client#disableBase}
* @property {string} [ratelimit] Sets a global rate limit on command calls for every user
* @property {LogLevel} [logLevel] Sets the logging level for the logger. Defaults to `LogLevel.LOG`
Expand All @@ -25,6 +25,7 @@ import { LogLevel } from './LogLevel';
export type YAMDBFOptions = {
name: string;
token: string;
owner?: string | string[];
provider?: StorageProviderConstructor;
commandsDir?: string;
statusText?: string;
Expand All @@ -36,6 +37,5 @@ export type YAMDBFOptions = {
version?: string;
disableBase?: BaseCommandName[];
ratelimit?: string;
config: any;
logLevel?: LogLevel;
};
2 changes: 1 addition & 1 deletion test/test_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Test extends Client
super({
name: 'test',
token: config.token,
config: config,
owner: config.owner,
commandsDir: './commands',
pause: true,
logLevel: LogLevel.DEBUG
Expand Down

0 comments on commit 1945eaa

Please sign in to comment.