refactor(bot, gateway, rest)!: Improve the interface of the createBot() function. (#3422)

* misc: Make the token a required property on the rest manager.

* refactor: Make unnecessarily required properties optional.

* misc: Improve interface for `createBot()` and allow passing in `transformers`/`handlers`.

* fix: Test made redundant by changes still being included.

* fix: Missing non-null assertions.

* fix: Benchmarks failing.

* misc: Remove `cache.requestMembers.pending` as an exposed option.

* style: Switch back to interface approach.
This commit is contained in:
Dorian Oszczęda
2024-03-07 22:39:48 -06:00
committed by GitHub
parent 46c93385c1
commit 05a20c3bf8
7 changed files with 38 additions and 30 deletions
+1 -6
View File
@@ -76,12 +76,7 @@ export const RATE_LIMIT_LIMIT_HEADER = 'x-ratelimit-limit'
export const RATE_LIMIT_SCOPE_HEADER = 'x-ratelimit-scope'
export function createRestManager(options: CreateRestManagerOptions): RestManager {
const applicationId = options.applicationId ? BigInt(options.applicationId) : options.token ? getBotIdFromToken(options.token) : undefined
if (!applicationId) {
throw new Error(
'`applicationId` was not provided and was not able to extract the id from the bots token. Please explicitly pass `applicationId` to the rest manager.',
)
}
const applicationId = options.applicationId ? BigInt(options.applicationId) : getBotIdFromToken(options.token)
const baseUrl = options.proxy?.baseUrl ?? DISCORD_API_URL
+2 -2
View File
@@ -134,7 +134,7 @@ import type { RestRoutes } from './typings/routes.js'
export interface CreateRestManagerOptions {
/** The bot token which will be used to make requests. */
token?: string
token: string
/**
* For old bots that have a different bot id and application id.
* @default bot id from token
@@ -170,7 +170,7 @@ export interface CreateRestManagerOptions {
export interface RestManager {
/** The bot token which will be used to make requests. */
token?: string
token: string
/** The application id. Normally this is not required for recent bots but old bot's application id is sometimes different from the bot id so it is required for those bots. */
applicationId: bigint
/** The api version to use when making requests. Only the latest supported version will be tested. */
+1 -5
View File
@@ -38,10 +38,6 @@ describe('[rest] manager', () => {
expect(rest.baseUrl).to.be.equal(options.proxy.baseUrl)
})
it('With a falsy token', () => {
expect(() => createRestManager({ token: '' })).throws()
})
it('With an application id', () => {
const subrest = createRestManager({ ...options, applicationId: '130136895395987456' })
expect(subrest.applicationId).to.be.equal(130136895395987456n)
@@ -173,7 +169,7 @@ describe('[rest] manager', () => {
let time: sinon.SinonFakeTimers
beforeEach(() => {
rest = createRestManager({ applicationId: 1n })
rest = createRestManager({ token: '1', applicationId: 1n })
time = sinon.useFakeTimers()
})