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 19 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 @@ -18,6 +18,12 @@ class BaseClient extends EventEmitter {
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
* @type {ClientOptions}
Expand Down
16 changes: 14 additions & 2 deletions packages/discord.js/src/util/Options.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const process = require('node:process');
const { DefaultRestOptions } = require('@discordjs/rest');
const { DefaultRestOptions, DefaultUserAgentAppendix } = 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} ${DefaultUserAgentAppendix}`.trimEnd();

/**
* 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 static 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 DefaultUserAgentAppendix
*/
userAgentAppendix: string;
/**
Expand Down
7 changes: 6 additions & 1 deletion packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type { RESTOptions } from '../REST.js';
export const DefaultUserAgent =
`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 = {
get agent() {
return new Agent({
Expand All @@ -24,7 +29,7 @@ export const DefaultRestOptions = {
rejectOnRateLimit: null,
retries: 3,
timeout: 15_000,
userAgentAppendix: `Node.js ${process.version}`,
userAgentAppendix: DefaultUserAgentAppendix,
version: APIVersion,
hashSweepInterval: 14_400_000, // 4 Hours
hashLifetime: 86_400_000, // 24 Hours
Expand Down