diff --git a/src/client/Client.ts b/src/client/Client.ts index 395c4ede..ea3f046d 100644 --- a/src/client/Client.ts +++ b/src/client/Client.ts @@ -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; @@ -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; @@ -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.
@@ -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 @@ -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'); @@ -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 @@ -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); } /** diff --git a/src/command/CommandRegistry.ts b/src/command/CommandRegistry.ts index 184719bd..dbe525bc 100644 --- a/src/command/CommandRegistry.ts +++ b/src/command/CommandRegistry.ts @@ -81,7 +81,7 @@ export class CommandRegistry 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()) @@ -99,8 +99,8 @@ export class CommandRegistry { - 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)); } /** @@ -112,7 +112,6 @@ export class CommandRegistry { - 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); } } diff --git a/src/types/YAMDBFOptions.ts b/src/types/YAMDBFOptions.ts index a1a49ff9..f9a25f05 100644 --- a/src/types/YAMDBFOptions.ts +++ b/src/types/YAMDBFOptions.ts @@ -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} @@ -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` @@ -25,6 +25,7 @@ import { LogLevel } from './LogLevel'; export type YAMDBFOptions = { name: string; token: string; + owner?: string | string[]; provider?: StorageProviderConstructor; commandsDir?: string; statusText?: string; @@ -36,6 +37,5 @@ export type YAMDBFOptions = { version?: string; disableBase?: BaseCommandName[]; ratelimit?: string; - config: any; logLevel?: LogLevel; }; diff --git a/test/test_client.ts b/test/test_client.ts index c32f2d9f..e9c424df 100644 --- a/test/test_client.ts +++ b/test/test_client.ts @@ -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