From b00a768441553d1ea13b7093b8285023f02ec3df Mon Sep 17 00:00:00 2001 From: QSmally Date: Tue, 13 Jul 2021 18:00:08 +0200 Subject: [PATCH 1/6] feat: implement `userAgentSuffix` --- src/rest/APIRequest.js | 10 ++++++++-- src/util/Options.js | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index c4487bf2a708..f80ef1f197e8 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -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) @@ -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; diff --git a/src/util/Options.js b/src/util/Options.js index 2cc3caea9ce6..85450b9f79fa 100644 --- a/src/util/Options.js +++ b/src/util/Options.js @@ -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 @@ -110,6 +112,7 @@ class Options extends null { restTimeOffset: 500, restSweepInterval: 60, failIfNotExists: true, + userAgentSuffix: [], presence: {}, ws: { large_threshold: 50, From 72473e0bf73187a0b3f5bac3ebe4ab273be68d90 Mon Sep 17 00:00:00 2001 From: QSmally Date: Tue, 13 Jul 2021 18:00:25 +0200 Subject: [PATCH 2/6] types: update types for this feature --- typings/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/typings/index.d.ts b/typings/index.d.ts index ef2c4a31143f..f8b0e189b765 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2865,6 +2865,7 @@ export interface ClientOptions { restSweepInterval?: number; retryLimit?: number; failIfNotExists?: boolean; + userAgentSuffix?: string[]; presence?: PresenceData; intents: BitFieldResolvable; ws?: WebSocketOptions; From 94408343bd0e2d2597b6c47ba8baa163367410b9 Mon Sep 17 00:00:00 2001 From: QSmally Date: Tue, 13 Jul 2021 19:09:33 +0200 Subject: [PATCH 3/6] add option validation --- src/client/Client.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/client/Client.js b/src/client/Client.js index 3079c3b34f26..1343905da98e 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -511,6 +511,9 @@ class Client extends BaseClient { if (typeof options.failIfNotExists !== 'boolean') { throw new TypeError('CLIENT_INVALID_OPTION', 'failIfNotExists', 'a boolean'); } + if (!Array.isArray(options.userAgentSuffix)) { + throw new TypeError('CLIENT_INVALID_OPTION', 'userAgentSuffix', 'an array of strings'); + } if ( typeof options.rejectOnRateLimit !== 'undefined' && !(typeof options.rejectOnRateLimit === 'function' || Array.isArray(options.rejectOnRateLimit)) From c480514e1ef95deb53788773cb545e84f8ed7a0b Mon Sep 17 00:00:00 2001 From: QSmally Date: Tue, 13 Jul 2021 21:41:39 +0200 Subject: [PATCH 4/6] fix: linting --- src/rest/APIRequest.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index f80ef1f197e8..ca378030286a 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -18,7 +18,7 @@ class APIRequest { this.retries = 0; const userAgentSuffix = this.client.options.userAgentSuffix; - this.fullUserAgent = `${UserAgent}${userAgentSuffix.length && `, ${userAgentSuffix.join(", ")}`}`; + this.fullUserAgent = `${UserAgent}${userAgentSuffix.length && `, ${userAgentSuffix.join(', ')}`}`; let queryString = ''; if (options.query) { @@ -39,7 +39,7 @@ class APIRequest { let headers = { ...this.client.options.http.headers, - 'User-Agent': this.fullUserAgent + 'User-Agent': this.fullUserAgent, }; if (this.options.auth !== false) headers.Authorization = this.rest.getAuth(); From f52064c4dc9f6140deb76fa4e5d1fb1ec6d1700a Mon Sep 17 00:00:00 2001 From: QSmally Date: Wed, 14 Jul 2021 09:42:46 +0200 Subject: [PATCH 5/6] fix: append on user agent --- src/rest/APIRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index ca378030286a..f8e92f81b338 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -18,7 +18,7 @@ class APIRequest { this.retries = 0; const userAgentSuffix = this.client.options.userAgentSuffix; - this.fullUserAgent = `${UserAgent}${userAgentSuffix.length && `, ${userAgentSuffix.join(', ')}`}`; + this.fullUserAgent = `${UserAgent}${userAgentSuffix.length ? `, ${userAgentSuffix.join(', ')}` : ''}`; let queryString = ''; if (options.query) { From 5e768ee49cf54732e2636dbeadc3f2596b632fa6 Mon Sep 17 00:00:00 2001 From: Joey Smalen <38661077+QSmally@users.noreply.github.com> Date: Wed, 14 Jul 2021 18:06:40 +0200 Subject: [PATCH 6/6] alter: destructure suffix property from `options` Co-authored-by: Vlad Frangu --- src/rest/APIRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index f8e92f81b338..f5136165634d 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -17,7 +17,7 @@ class APIRequest { this.options = options; this.retries = 0; - const userAgentSuffix = this.client.options.userAgentSuffix; + const { userAgentSuffix } = this.client.options; this.fullUserAgent = `${UserAgent}${userAgentSuffix.length ? `, ${userAgentSuffix.join(', ')}` : ''}`; let queryString = '';