Merge branch 'master' into indev-prism

This commit is contained in:
Schuyler Cebulskie
2017-02-06 01:54:31 -05:00
17 changed files with 167 additions and 120 deletions

View File

@@ -15,7 +15,7 @@ const ShardClientUtil = require('../sharding/ShardClientUtil');
const VoiceBroadcast = require('./voice/VoiceBroadcast');
/**
* The starting point for making a Discord Bot.
* The main hub for interacting with the Discord API, and the starting point for any bot.
* @extends {EventEmitter}
*/
class Client extends EventEmitter {
@@ -86,39 +86,43 @@ class Client extends EventEmitter {
this.voice = !this.browser ? new ClientVoiceManager(this) : null;
/**
* The shard helpers for the client (only if the process was spawned as a child, such as from a ShardingManager)
* The shard helpers for the client
* (only if the process was spawned as a child, such as from a {@link ShardingManager})
* @type {?ShardClientUtil}
*/
this.shard = process.send ? ShardClientUtil.singleton(this) : null;
/**
* A collection of the Client's stored users
* @type {Collection<string, User>}
* All of the {@link User} objects that have been cached at any point, mapped by their IDs
* @type {Collection<Snowflake, User>}
*/
this.users = new Collection();
/**
* A collection of the Client's stored guilds
* @type {Collection<string, Guild>}
* All of the guilds the client is currently handling, mapped by their IDs -
* as long as sharding isn't being used, this will be *every* guild the bot is a member of
* @type {Collection<Snowflake, Guild>}
*/
this.guilds = new Collection();
/**
* A collection of the Client's stored channels
* @type {Collection<string, Channel>}
* All of the {@link Channel}s that the client is currently handling, mapped by their IDs -
* as long as sharding isn't being used, this will be *every* channel in *every* guild, and all DM channels
* @type {Collection<Snowflake, Channel>}
*/
this.channels = new Collection();
/**
* A collection of presences for friends of the logged in user.
* Presences that have been received for the client user's friends, mapped by user IDs
* <warn>This is only filled when using a user account.</warn>
* @type {Collection<string, Presence>}
* @type {Collection<Snowflake, Presence>}
*/
this.presences = new Collection();
if (!this.token && 'CLIENT_TOKEN' in process.env) {
/**
* The authorization token for the logged in user/bot.
* Authorization token for the logged in user/bot
* <warn>This should be kept private at all times.</warn>
* @type {?string}
*/
this.token = process.env.CLIENT_TOKEN;
@@ -127,25 +131,26 @@ class Client extends EventEmitter {
}
/**
* The ClientUser representing the logged in Client
* User that the client is logged in as
* @type {?ClientUser}
*/
this.user = null;
/**
* The date at which the Client was regarded as being in the `READY` state.
* Time at which the client was last regarded as being in the `READY` state
* (each time the client disconnects and successfully reconnects, this will be overwritten)
* @type {?Date}
*/
this.readyAt = null;
/**
* An array of voice broadcasts
* Active voice broadcasts that have been created
* @type {VoiceBroadcast[]}
*/
this.broadcasts = [];
/**
* The previous heartbeat pings of the websocket (most recent first, limited to three elements)
* Previous heartbeat pings of the websocket (most recent first, limited to three elements)
* @type {number[]}
*/
this.pings = [];
@@ -160,7 +165,7 @@ class Client extends EventEmitter {
}
/**
* The status for the logged in Client.
* Current status of the client's connection to Discord
* @type {?number}
* @readonly
*/
@@ -169,7 +174,7 @@ class Client extends EventEmitter {
}
/**
* The uptime for the logged in Client.
* How long it has been since the client last entered the `READY` state
* @type {?number}
* @readonly
*/
@@ -178,7 +183,7 @@ class Client extends EventEmitter {
}
/**
* The average heartbeat ping of the websocket
* Average heartbeat ping of the websocket, obtained by averaging the {@link Client#pings} property
* @type {number}
* @readonly
*/
@@ -187,7 +192,7 @@ class Client extends EventEmitter {
}
/**
* Returns a collection, mapping guild ID to voice connections.
* All active voice connections that have been established, mapped by channel ID
* @type {Collection<string, VoiceConnection>}
* @readonly
*/
@@ -197,8 +202,8 @@ class Client extends EventEmitter {
}
/**
* The emojis that the client can use. Mapped by emoji ID.
* @type {Collection<string, Emoji>}
* All custom emojis that the client has access to, mapped by their IDs
* @type {Collection<Snowflake, Emoji>}
* @readonly
*/
get emojis() {
@@ -210,7 +215,7 @@ class Client extends EventEmitter {
}
/**
* The timestamp that the client was last ready at
* Timestamp of the time the client was last `READY` at
* @type {?number}
* @readonly
*/
@@ -228,8 +233,8 @@ class Client extends EventEmitter {
}
/**
* Creates a new voice broadcast
* @returns {VoiceBroadcast} the created broadcast
* Creates a voice broadcast.
* @returns {VoiceBroadcast}
*/
createVoiceBroadcast() {
const broadcast = new VoiceBroadcast(this);
@@ -238,28 +243,22 @@ class Client extends EventEmitter {
}
/**
* Logs the client in. If successful, resolves with the account's token. <warn>If you're making a bot, it's
* much better to use a bot account rather than a user account.
* Bot accounts have higher rate limits and have access to some features user accounts don't have. User bots
* that are making a lot of API requests can even be banned.</warn>
* @param {string} token The token used for the account.
* @returns {Promise<string>}
* Logs the client in, establishing a websocket connection to Discord.
* <info>Both bot and regular user accounts are supported, but it is highly recommended to use a bot account whenever
* possible. User accounts are subject to harsher ratelimits and other restrictions that don't apply to bot accounts.
* Bot accounts also have access to many features that user accounts cannot utilise. User accounts that are found to
* be abusing/overusing the API will be banned, locking you out of Discord entirely.</info>
* @param {string} token Token of the account to log in with
* @returns {Promise<string>} Token of the account used
* @example
* // log the client in using a token
* const token = 'my token';
* client.login(token);
* @example
* // log the client in using email and password
* const email = 'user@email.com';
* const password = 'supersecret123';
* client.login(email, password);
* client.login('my token');
*/
login(token) {
return this.rest.methods.login(token);
}
/**
* Destroys the client and logs out.
* Logs out, terminates the connection to Discord, and destroys the client
* @returns {Promise}
*/
destroy() {
@@ -271,10 +270,10 @@ class Client extends EventEmitter {
}
/**
* This shouldn't really be necessary to most developers as it is automatically invoked every 30 seconds, however
* if you wish to force a sync of guild data, you can use this.
* Requests a sync of guild data with Discord.
* <info>This can be done automatically every 30 seconds by enabling {@link ClientOptions#sync}.</info>
* <warn>This is only available when using a user account.</warn>
* @param {Guild[]|Collection<string, Guild>} [guilds=this.guilds] An array or collection of guilds to sync
* @param {Guild[]|Collection<Snowflake, Guild>} [guilds=this.guilds] An array or collection of guilds to sync
*/
syncGuilds(guilds = this.guilds) {
if (this.user.bot) return;
@@ -285,10 +284,10 @@ class Client extends EventEmitter {
}
/**
* Caches a user, or obtains it from the cache if it's already cached.
* Obtains a user from Discord, or the user cache if it's already available.
* <warn>This is only available when using a bot account.</warn>
* @param {string} id The ID of the user to obtain
* @param {boolean} [cache=true] Insert the user into the users cache
* @param {string} id ID of the user
* @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
* @returns {Promise<User>}
*/
fetchUser(id, cache = true) {
@@ -297,8 +296,8 @@ class Client extends EventEmitter {
}
/**
* Fetches an invite object from an invite code.
* @param {InviteResolvable} invite An invite code or URL
* Obtains an invite from Discord.
* @param {InviteResolvable} invite Invite code or URL
* @returns {Promise<Invite>}
*/
fetchInvite(invite) {
@@ -307,7 +306,7 @@ class Client extends EventEmitter {
}
/**
* Fetch a webhook by ID.
* Obtains a webhook from Discord.
* @param {string} id ID of the webhook
* @param {string} [token] Token for the webhook
* @returns {Promise<Webhook>}
@@ -317,7 +316,7 @@ class Client extends EventEmitter {
}
/**
* Fetch available voice regions
* Obtains the available voice regions from Discord.
* @returns {Collection<string, VoiceRegion>}
*/
fetchVoiceRegions() {
@@ -325,10 +324,10 @@ class Client extends EventEmitter {
}
/**
* Sweeps all channels' messages and removes the ones older than the max message lifetime.
* Sweeps all text-based channels' messages and removes the ones older than the max message lifetime.
* If the message has been edited, the time of the edit is used rather than the time of the original message.
* @param {number} [lifetime=this.options.messageCacheLifetime] Messages that are older than this (in seconds)
* will be removed from the caches. The default is based on the client's `messageCacheLifetime` option.
* will be removed from the caches. The default is based on {@link ClientOptions#messageCacheLifetime}.
* @returns {number} Amount of messages that were removed from the caches,
* or -1 if the message cache lifetime is unlimited
*/
@@ -361,7 +360,7 @@ class Client extends EventEmitter {
}
/**
* Gets the bot's OAuth2 application.
* Obtains the OAuth Application of the bot from Discord.
* <warn>This is only available when using a bot account.</warn>
* @returns {Promise<ClientOAuth2Application>}
*/
@@ -371,9 +370,10 @@ class Client extends EventEmitter {
}
/**
* Generate an invite link for your bot
* @param {PermissionResolvable[]|number} [permissions] An array of permissions to request
* @returns {Promise<string>} The invite link
* Generates a link that can be used to invite the bot to a guild.
* <warn>This is only available when using a bot account.</warn>
* @param {PermissionResolvable[]|number} [permissions] Permissions to request
* @returns {Promise<string>}
* @example
* client.generateInvite(['SEND_MESSAGES', 'MANAGE_GUILD', 'MENTION_EVERYONE'])
* .then(link => {
@@ -438,24 +438,46 @@ class Client extends EventEmitter {
this._intervals.delete(interval);
}
/**
* Adds a ping to {@link Client#pings}.
* @param {number} startTime Starting time of the ping
* @private
*/
_pong(startTime) {
this.pings.unshift(Date.now() - startTime);
if (this.pings.length > 3) this.pings.length = 3;
this.ws.lastHeartbeatAck = true;
}
/**
* Adds/updates a friend's presence in {@link Client#presences}.
* @param {string} id ID of the user
* @param {Object} presence Raw presence object from Discord
* @private
*/
_setPresence(id, presence) {
if (this.presences.get(id)) {
if (this.presences.has(id)) {
this.presences.get(id).update(presence);
return;
}
this.presences.set(id, new Presence(presence));
}
/**
* Calls `eval(script)` with the client as `this`.
* @param {string} script Script to eval
* @returns {*}
* @private
*/
_eval(script) {
return eval(script);
}
/**
* Validates client options
* @param {ClientOptions} [options=this.options] Options to validate
* @private
*/
_validateOptions(options = this.options) {
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount)) {
throw new TypeError('The shardCount option must be a number.');