Skip to content

Commit

Permalink
Add Command#init() support
Browse files Browse the repository at this point in the history
  • Loading branch information
zajrik committed Aug 3, 2017
1 parent fb79f9c commit 62d94f9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
19 changes: 6 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,18 @@ export class Client extends Discord.Client
{
await this._guildDataStorage.init();
await this._guildSettingStorage.init();

await this._guildStorageLoader.loadStorages(this._guildDataStorage, this._guildSettingStorage);

await this.plugins._loadPlugins();

if (!this.passive) this._dispatcher.setReady();
if (!this.passive)
{
this._logger.info('Client', 'Initializing commands...');
await this.commands._initCommands();
this._logger.info('Client', 'Commands initialized.');
this._dispatcher.setReady();
this._logger.info('Client', 'Command dispatcher ready.');
}

if (typeof this.readyText !== 'undefined')
this._logger.log('Client', this.readyText);

Expand Down
17 changes: 16 additions & 1 deletion src/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ export class Command<T extends Client = Client>
this._rateLimiter = new RateLimiter(info.ratelimit, false);
}

/**
* Can be included in a command to initlialize any resources a command
* needs at runtime that require things that are not available within
* a command's constructor like the client instance or client/guild storages.
*
* Will be called after all commands are loaded (including those from
* any loaded plugins) and after all base framework storages (client and guild)
* are ready for use.
*
* >**Note:** Can be async if needed
* @returns {Promise<void>}
*/
public async init(): Promise<void> {}

/**
* Action to be executed when the command is called. The following parameters
* are what command actions will be passed by the {@link CommandDispatcher} whenever
Expand Down Expand Up @@ -241,7 +255,8 @@ export class Command<T extends Client = Client>
// Default guildOnly to true if permissions/roles are given
if (!this.guildOnly && (this.callerPermissions.length
|| this.clientPermissions.length
|| this.roles.length)) this.guildOnly = true;
|| this.roles.length))
this.guildOnly = true;

if (!this.action) throw new TypeError(`Command "${this.name}".action: expected Function, got: ${typeof this.action}`);
if (!(this.action instanceof Function)) throw new TypeError(`Command "${this.name}".action: expected Function, got: ${typeof this.action}`);
Expand Down
10 changes: 10 additions & 0 deletions src/command/CommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export class CommandRegistry<T extends Client, K extends string, V extends Comma
}
}

/**
* Run the `init()` method of all loaded commands.
* This is an internal method and should not be used
* @private
*/
public async _initCommands(): Promise<void>
{
for (const command of this.values()) await command.init();
}

/**
* Register an external command and add it to the `<Client>.commands`
* [collection]{@link external:Collection}, erroring on duplicate
Expand Down
7 changes: 7 additions & 0 deletions test/commands/test_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export default class extends Command
});
}

public async init(): Promise<void>
{
await this.logger.debug('Command:test', await this.client.storage.get('defaultGuildSettings.prefix'));
await this.logger.debug('Command:test', await this.client.storage.guilds.first().settings.get('prefix'));
await this.logger.debug('Command:test', 'Test command initialized.');
}

// @using((message, args) => [message, args.map(a => a.toUpperCase())])
// @using(resolve(`test: Member, foo: String`))
// @using(expect(`test: Member, foo: ['foo', 'bar']`))
Expand Down

0 comments on commit 62d94f9

Please sign in to comment.