Skip to content

Commit

Permalink
feat(ClientOptions): add support for setting an initial presence (#2320)
Browse files Browse the repository at this point in the history
* docs/feat(WebsocketOptions): Parse ws options presence

Allow the `presence` property in `WebsocketOptions` to be used the same way
as `ClientUser#setPresence`.

* Move presence options to top level
  • Loading branch information
Danktuary authored and SpaceEEC committed Feb 4, 2018
1 parent 4336317 commit 87e5a45
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/client/ClientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class ClientManager {
this.client.emit(Events.DEBUG, `Authenticated using token ${token}`);
this.client.token = token;
const timeout = this.client.setTimeout(() => reject(new Error('WS_CONNECTION_TIMEOUT')), 1000 * 300);
this.client.api.gateway.get().then(res => {
this.client.api.gateway.get().then(async res => {
if (this.client.options.presence != null) { // eslint-disable-line eqeqeq
this.client.options.ws.presence = await this.client.presences._parse(this.client.options.presence);
}
const gateway = `${res.url}/`;
this.client.emit(Events.DEBUG, `Using gateway ${gateway}`);
this.client.ws.connect(gateway);
Expand Down
13 changes: 9 additions & 4 deletions src/stores/ClientPresenceStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ class ClientPresenceStore extends PresenceStore {
});
}

async setClientPresence({ status, since, afk, activity }) { // eslint-disable-line complexity
async setClientPresence(presence) {
const packet = await this._parse(presence);
this.clientPresence.patch(packet);
this.client.ws.send({ op: OPCodes.STATUS_UPDATE, d: packet });
return this.clientPresence;
}

async _parse({ status, since, afk, activity }) { // eslint-disable-line complexity
const applicationID = activity && (activity.application ? activity.application.id || activity.application : null);
let assets = new Collection();
if (activity) {
Expand Down Expand Up @@ -66,9 +73,7 @@ class ClientPresenceStore extends PresenceStore {
packet.game.type : ActivityTypes.indexOf(packet.game.type);
}

this.clientPresence.patch(packet);
this.client.ws.send({ op: OPCodes.STATUS_UPDATE, d: packet });
return this.clientPresence;
return packet;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const browser = exports.browser = typeof window !== 'undefined';
* corresponding websocket events
* @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST
* requests (higher values will reduce rate-limiting errors on bad connections)
* @property {PresenceData} [presence] Presence data to use upon login
* @property {WSEventType[]} [disabledEvents] An array of disabled websocket events. Events in this array will not be
* processed, potentially resulting in performance improvements for larger bots. Only disable events you are
* 100% certain you don't need, as many are important, but not obviously so. The safest one to disable with the
Expand All @@ -47,6 +48,7 @@ exports.DefaultOptions = {
restWsBridgeTimeout: 5000,
disabledEvents: [],
restTimeOffset: 500,
presence: {},

/**
* WebSocket options (these are left as snake_case to match the API)
Expand Down

0 comments on commit 87e5a45

Please sign in to comment.