Skip to content

Commit

Permalink
Privatize Client#_token
Browse files Browse the repository at this point in the history
Co-authored-by: Khaazz <[email protected]>
  • Loading branch information
abalabahaha and Khaaz committed Apr 1, 2021
1 parent 7a5ec43 commit 99f4163
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
22 changes: 13 additions & 9 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
* @prop {RequestHandler} requestHandler The request handler the client will use
* @prop {Collection<Shard>} shards Collection of shards Eris is using
* @prop {Number} startTime Timestamp of bot ready event
* @prop {String} token The bot user token
* @prop {String} token The auth token
* @prop {Collection<UnavailableGuild>} unavailableGuilds Collection of unavailable guilds the bot is in
* @prop {Number} uptime How long in milliseconds the bot has been up for
* @prop {ExtendedUser} user The bot user
Expand All @@ -77,7 +77,7 @@ const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
class Client extends EventEmitter {
/**
* Create a Client
* @arg {String} token bot token
* @arg {String} token The auth token to use. Bot tokens should be prefixed with `Bot` (e.g. `Bot MTExIHlvdSAgdHJpZWQgMTEx.O5rKAA.dQw4w9WgXcQ_wpV-gGA4PSk_bm8`). Prefix-less bot tokens are [DEPRECATED]
* @arg {Object} [options] Eris options (all options are optional)
* @arg {Object} [options.agent] [DEPRECATED] A HTTPS Agent used to proxy requests. This option has been moved under `options.rest`
* @arg {Object} [options.allowedMentions] A list of mentions to allow by default in createMessage/editMessage
Expand Down Expand Up @@ -187,13 +187,18 @@ class Client extends EventEmitter {
}
}

this.token = token;
Object.defineProperty(this, "_token", {
configurable: true,
enumerable: false,
writable: true,
value: token
});

this.requestHandler = new RequestHandler(this, this.options.rest);
delete this.options.rest;

this.ready = false;
this.bot = this.options.restMode && token ? token.startsWith("Bot ") : true;
this.bot = this._token.startsWith("Bot ");
this.startTime = 0;
this.lastConnect = 0;
this.channelGuildMap = {};
Expand Down Expand Up @@ -852,7 +857,7 @@ class Client extends EventEmitter {
code
}).then((data) => {
if(data.token) {
this.token = data.token;
this._token = data.token;
}
});
}
Expand Down Expand Up @@ -1389,7 +1394,7 @@ class Client extends EventEmitter {
code
}).then((data) => {
if(data.token) {
this.token = data.token;
this._token = data.token;
}
});
}
Expand Down Expand Up @@ -1461,8 +1466,8 @@ class Client extends EventEmitter {
* @returns {Promise<Object>} Resolves with an object containing gateway connection info
*/
getBotGateway() {
if(!this.token.startsWith("Bot ")) {
this.token = "Bot " + this.token;
if(!this._token.startsWith("Bot ")) {
this._token = "Bot " + this._token;
}
return this.requestHandler.request("GET", Endpoints.GATEWAY_BOT, true);
}
Expand Down Expand Up @@ -2498,7 +2503,6 @@ class Client extends EventEmitter {
toJSON(props = []) {
return Base.prototype.toJSON.call(this, [
"options",
"token",
"requestHandler",
"ready",
"bot",
Expand Down
7 changes: 4 additions & 3 deletions lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ class Shard extends EventEmitter {
Object.defineProperty(this, "_token", {
configurable: true,
enumerable: false,
value: this.client.token
writable: true,
value: this.client._token
});
}

Expand Down Expand Up @@ -1848,8 +1849,8 @@ class Shard extends EventEmitter {
this.client.user = this.client.users.update(new ExtendedUser(packet.d.user, this.client), this.client);
if(this.client.user.bot) {
this.client.bot = true;
if(!this.client.token.startsWith("Bot ")) {
this.client.token = "Bot " + this.client.token;
if(!this.client._token.startsWith("Bot ")) {
this.client._token = "Bot " + this.client._token;
}
} else {
this.client.bot = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class RequestHandler {

try {
if(auth) {
headers.Authorization = this._client.token;
headers.Authorization = this._client._token;
}
if(body && body.reason) { // Audit log reason sniping
headers["X-Audit-Log-Reason"] = body.reason;
Expand Down

0 comments on commit 99f4163

Please sign in to comment.