Skip to content

chore: Include discord.js in the user agent string #9267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/discord.js/src/client/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class BaseClient extends EventEmitter {
constructor(options = {}) {
super({ captureRejections: true });

if (options.rest?.userAgentAppendix !== undefined) {
// 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}`;
}

if (typeof options !== 'object' || options === null) {
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
}
Expand Down
14 changes: 13 additions & 1 deletion packages/discord.js/src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const process = require('node:process');
const { DefaultRestOptions } = require('@discordjs/rest');
const { toSnakeCase } = require('./Transformers');
const { version } = require('../../package.json');

/**
* @typedef {Function} CacheFactory
Expand Down Expand Up @@ -70,6 +71,14 @@ const { toSnakeCase } = require('./Transformers');
* Contains various utilities for client options.
*/
class Options extends null {
/**
* The default user agent appendix.
* @type {string}
* @memberof Options
* @private
*/
static userAgentAppendix = `discord.js/${version} Node.js/${process.version}`;

/**
* The default client options.
* @returns {ClientOptions}
Expand All @@ -94,7 +103,10 @@ class Options extends null {
},
version: 10,
},
rest: DefaultRestOptions,
rest: {
...DefaultRestOptions,
userAgentAppendix: this.userAgentAppendix,
},
jsonTransformer: toSnakeCase,
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ export class ClientUser extends User {

export class Options extends null {
private constructor();
private userAgentAppendix: string;
public static get DefaultMakeCacheSettings(): CacheWithLimitsOptions;
public static get DefaultSweeperSettings(): SweeperOptions;
public static createDefault(): ClientOptions;
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/lib/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export interface RESTOptions {
/**
* Extra information to add to the user agent
*
* @defaultValue `Node.js ${process.version}`
* @defaultValue `Node.js/${process.version}`
*/
userAgentAppendix: string;
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const DefaultRestOptions = {
rejectOnRateLimit: null,
retries: 3,
timeout: 15_000,
userAgentAppendix: `Node.js ${process.version}`,
userAgentAppendix: `Node.js/${process.version}`,
version: APIVersion,
hashSweepInterval: 14_400_000, // 4 Hours
hashLifetime: 86_400_000, // 24 Hours
Expand Down