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:
Jiralite
2023-04-01 23:26:44 +01:00
committed by GitHub
parent 1864d37d36
commit ab3328a0e2
5 changed files with 28 additions and 4 deletions

View File

@@ -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}

View File

@@ -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
@@ -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}
@@ -94,7 +103,10 @@ class Options extends null {
},
version: 10,
},
rest: DefaultRestOptions,
rest: {
...DefaultRestOptions,
userAgentAppendix: this.userAgentAppendix,
},
jsonTransformer: toSnakeCase,
};
}