mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
chore: Include discord.js in the user agent string (#9267)
* chore: apply user agent string * fix: enforce even in custom option * fix: tests * refactor: simpler way * docs: add type * Update packages/discord.js/src/client/BaseClient.js Co-authored-by: Aura Román <kyradiscord@gmail.com> * fix: prioritise `option` type check * types: `static` * feat: add runtime check * docs: update default * refactor: remove Bun * Update packages/discord.js/src/client/BaseClient.js Co-authored-by: Almeida <almeidx@pm.me> * fix: extra whitespace issues * refactor: `trimEnd()` --------- Co-authored-by: Aura Román <kyradiscord@gmail.com> Co-authored-by: Almeida <almeidx@pm.me> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -18,6 +18,12 @@ class BaseClient extends EventEmitter {
|
|||||||
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
|
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.rest?.userAgentAppendix) {
|
||||||
|
// Merging the default options when a custom user agent appendix is supplied
|
||||||
|
// Replaces the discord.js string. Enforce it.
|
||||||
|
options.rest.userAgentAppendix = `${Options.userAgentAppendix} ${options.rest.userAgentAppendix}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The options the client was instantiated with
|
* The options the client was instantiated with
|
||||||
* @type {ClientOptions}
|
* @type {ClientOptions}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const process = require('node:process');
|
const process = require('node:process');
|
||||||
const { DefaultRestOptions } = require('@discordjs/rest');
|
const { DefaultRestOptions, DefaultUserAgentAppendix } = require('@discordjs/rest');
|
||||||
const { toSnakeCase } = require('./Transformers');
|
const { toSnakeCase } = require('./Transformers');
|
||||||
|
const { version } = require('../../package.json');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Function} CacheFactory
|
* @typedef {Function} CacheFactory
|
||||||
@@ -70,6 +71,14 @@ const { toSnakeCase } = require('./Transformers');
|
|||||||
* Contains various utilities for client options.
|
* Contains various utilities for client options.
|
||||||
*/
|
*/
|
||||||
class Options extends null {
|
class Options extends null {
|
||||||
|
/**
|
||||||
|
* The default user agent appendix.
|
||||||
|
* @type {string}
|
||||||
|
* @memberof Options
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
static userAgentAppendix = `discord.js/${version} ${DefaultUserAgentAppendix}`.trimEnd();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default client options.
|
* The default client options.
|
||||||
* @returns {ClientOptions}
|
* @returns {ClientOptions}
|
||||||
@@ -94,7 +103,10 @@ class Options extends null {
|
|||||||
},
|
},
|
||||||
version: 10,
|
version: 10,
|
||||||
},
|
},
|
||||||
rest: DefaultRestOptions,
|
rest: {
|
||||||
|
...DefaultRestOptions,
|
||||||
|
userAgentAppendix: this.userAgentAppendix,
|
||||||
|
},
|
||||||
jsonTransformer: toSnakeCase,
|
jsonTransformer: toSnakeCase,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
1
packages/discord.js/typings/index.d.ts
vendored
1
packages/discord.js/typings/index.d.ts
vendored
@@ -1030,6 +1030,7 @@ export class ClientUser extends User {
|
|||||||
|
|
||||||
export class Options extends null {
|
export class Options extends null {
|
||||||
private constructor();
|
private constructor();
|
||||||
|
private static userAgentAppendix: string;
|
||||||
public static get DefaultMakeCacheSettings(): CacheWithLimitsOptions;
|
public static get DefaultMakeCacheSettings(): CacheWithLimitsOptions;
|
||||||
public static get DefaultSweeperSettings(): SweeperOptions;
|
public static get DefaultSweeperSettings(): SweeperOptions;
|
||||||
public static createDefault(): ClientOptions;
|
public static createDefault(): ClientOptions;
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ export interface RESTOptions {
|
|||||||
/**
|
/**
|
||||||
* Extra information to add to the user agent
|
* Extra information to add to the user agent
|
||||||
*
|
*
|
||||||
* @defaultValue `Node.js ${process.version}`
|
* @defaultValue DefaultUserAgentAppendix
|
||||||
*/
|
*/
|
||||||
userAgentAppendix: string;
|
userAgentAppendix: string;
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,6 +6,11 @@ import type { RESTOptions } from '../REST.js';
|
|||||||
export const DefaultUserAgent =
|
export const DefaultUserAgent =
|
||||||
`DiscordBot (https://discord.js.org, [VI]{{inject}}[/VI])` as `DiscordBot (https://discord.js.org, ${string})`;
|
`DiscordBot (https://discord.js.org, [VI]{{inject}}[/VI])` as `DiscordBot (https://discord.js.org, ${string})`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default string to append onto the user agent.
|
||||||
|
*/
|
||||||
|
export const DefaultUserAgentAppendix = process.release?.name === 'node' ? `Node.js/${process.version}` : '';
|
||||||
|
|
||||||
export const DefaultRestOptions = {
|
export const DefaultRestOptions = {
|
||||||
get agent() {
|
get agent() {
|
||||||
return new Agent({
|
return new Agent({
|
||||||
@@ -24,7 +29,7 @@ export const DefaultRestOptions = {
|
|||||||
rejectOnRateLimit: null,
|
rejectOnRateLimit: null,
|
||||||
retries: 3,
|
retries: 3,
|
||||||
timeout: 15_000,
|
timeout: 15_000,
|
||||||
userAgentAppendix: `Node.js ${process.version}`,
|
userAgentAppendix: DefaultUserAgentAppendix,
|
||||||
version: APIVersion,
|
version: APIVersion,
|
||||||
hashSweepInterval: 14_400_000, // 4 Hours
|
hashSweepInterval: 14_400_000, // 4 Hours
|
||||||
hashLifetime: 86_400_000, // 24 Hours
|
hashLifetime: 86_400_000, // 24 Hours
|
||||||
|
|||||||
Reference in New Issue
Block a user