mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
feat(REST): append additional information to the required User Agent (#6112)
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
@@ -520,6 +520,9 @@ class Client extends BaseClient {
|
|||||||
if (typeof options.failIfNotExists !== 'boolean') {
|
if (typeof options.failIfNotExists !== 'boolean') {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'failIfNotExists', 'a 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 (
|
if (
|
||||||
typeof options.rejectOnRateLimit !== 'undefined' &&
|
typeof options.rejectOnRateLimit !== 'undefined' &&
|
||||||
!(typeof options.rejectOnRateLimit === 'function' || Array.isArray(options.rejectOnRateLimit))
|
!(typeof options.rejectOnRateLimit === 'function' || Array.isArray(options.rejectOnRateLimit))
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ class APIRequest {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
this.retries = 0;
|
this.retries = 0;
|
||||||
|
|
||||||
|
const { userAgentSuffix } = this.client.options;
|
||||||
|
this.fullUserAgent = `${UserAgent}${userAgentSuffix.length ? `, ${userAgentSuffix.join(', ')}` : ''}`;
|
||||||
|
|
||||||
let queryString = '';
|
let queryString = '';
|
||||||
if (options.query) {
|
if (options.query) {
|
||||||
const query = Object.entries(options.query)
|
const query = Object.entries(options.query)
|
||||||
@@ -33,11 +36,14 @@ class APIRequest {
|
|||||||
? this.client.options.http.api
|
? this.client.options.http.api
|
||||||
: `${this.client.options.http.api}/v${this.client.options.http.version}`;
|
: `${this.client.options.http.api}/v${this.client.options.http.version}`;
|
||||||
const url = API + this.path;
|
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.auth !== false) headers.Authorization = this.rest.getAuth();
|
||||||
if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
|
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);
|
if (this.options.headers) headers = Object.assign(headers, this.options.headers);
|
||||||
|
|
||||||
let body;
|
let body;
|
||||||
|
|||||||
@@ -62,6 +62,8 @@
|
|||||||
* {@link RateLimitError} will be thrown. Otherwise the request will be queued for later
|
* {@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 {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 {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 {PresenceData} [presence={}] Presence data to use upon login
|
||||||
* @property {IntentsResolvable} intents Intents to enable for this connection
|
* @property {IntentsResolvable} intents Intents to enable for this connection
|
||||||
* @property {WebsocketOptions} [ws] Options for the WebSocket
|
* @property {WebsocketOptions} [ws] Options for the WebSocket
|
||||||
@@ -109,6 +111,7 @@ class Options extends null {
|
|||||||
restTimeOffset: 500,
|
restTimeOffset: 500,
|
||||||
restSweepInterval: 60,
|
restSweepInterval: 60,
|
||||||
failIfNotExists: true,
|
failIfNotExists: true,
|
||||||
|
userAgentSuffix: [],
|
||||||
presence: {},
|
presence: {},
|
||||||
ws: {
|
ws: {
|
||||||
large_threshold: 50,
|
large_threshold: 50,
|
||||||
|
|||||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -2908,6 +2908,7 @@ export interface ClientOptions {
|
|||||||
restSweepInterval?: number;
|
restSweepInterval?: number;
|
||||||
retryLimit?: number;
|
retryLimit?: number;
|
||||||
failIfNotExists?: boolean;
|
failIfNotExists?: boolean;
|
||||||
|
userAgentSuffix?: string[];
|
||||||
presence?: PresenceData;
|
presence?: PresenceData;
|
||||||
intents: BitFieldResolvable<IntentsString, number>;
|
intents: BitFieldResolvable<IntentsString, number>;
|
||||||
ws?: WebSocketOptions;
|
ws?: WebSocketOptions;
|
||||||
|
|||||||
Reference in New Issue
Block a user