Skip to content

Commit

Permalink
feat: implement userAgentSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
QSmally committed Jul 13, 2021
1 parent 98c6078 commit b00a768
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/rest/APIRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class APIRequest {
this.options = options;
this.retries = 0;

const userAgentSuffix = this.client.options.userAgentSuffix;
this.fullUserAgent = `${UserAgent}${userAgentSuffix.length && `, ${userAgentSuffix.join(", ")}`}`;

let queryString = '';
if (options.query) {
const query = Object.entries(options.query)
Expand All @@ -33,11 +36,14 @@ class APIRequest {
? this.client.options.http.api
: `${this.client.options.http.api}/v${this.client.options.http.version}`;
const url = API + this.path;
let headers = { ...this.client.options.http.headers };

let headers = {
...this.client.options.http.headers,
'User-Agent': this.fullUserAgent
};

if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
headers['User-Agent'] = UserAgent;
if (this.options.headers) headers = Object.assign(headers, this.options.headers);

let body;
Expand Down
3 changes: 3 additions & 0 deletions src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
* {@link RateLimitError} will be thrown. Otherwise the request will be queued for later
* @property {number} [retryLimit=1] How many times to retry on 5XX errors (Infinity for indefinite amount of retries)
* @property {boolean} [failIfNotExists=true] Default value for {@link ReplyMessageOptions#failIfNotExists}
* @property {string[]} [userAgentSuffix] An array of additional bot info to be appended to the end of the required
* [User Agent](https://discord.com/developers/docs/reference#user-agent) header
* @property {PresenceData} [presence={}] Presence data to use upon login
* @property {IntentsResolvable} intents Intents to enable for this connection
* @property {WebsocketOptions} [ws] Options for the WebSocket
Expand Down Expand Up @@ -110,6 +112,7 @@ class Options extends null {
restTimeOffset: 500,
restSweepInterval: 60,
failIfNotExists: true,
userAgentSuffix: [],
presence: {},
ws: {
large_threshold: 50,
Expand Down

0 comments on commit b00a768

Please sign in to comment.