From 7c031df875c536b489c05e1ff2b5057da7a65751 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sun, 2 Apr 2023 21:39:08 +0200 Subject: [PATCH] refactor(rest)!: remove falsy token check (#2949) * refactor(rest): remove falsy token check This is in favour for interaction only bots, which still need rest but cannot provide a valid bot token. If you want to use rest for your normal bot it should be your own responsibility to pass a valid token. Further more `applicationId` is now a required property since we cannot extract the id from the token anymore. * forgot to commit that * fix error * make appid optional again * app id throw error if undefined * fix test --- packages/rest/src/manager.ts | 10 +++++++--- packages/rest/src/types.ts | 4 ++-- packages/rest/tests/unit/manager.spec.ts | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index de0c24578..7f3f90e89 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -69,12 +69,16 @@ import type { CreateRequestBodyOptions, CreateRestManagerOptions, RestManager, S const version = '19.0.0-alpha.1' export function createRestManager(options: CreateRestManagerOptions): RestManager { - // Falsy token string check - if (!options.token) throw new Error('You must provide a valid token.') + 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 rest: RestManager = { token: options.token, - applicationId: options.applicationId ? BigInt(options.applicationId) : getBotIdFromToken(options.token), + applicationId, version: options.version ?? 10, baseUrl: options.proxy?.baseUrl ?? 'https://discord.com/api', maxRetryCount: Infinity, diff --git a/packages/rest/src/types.ts b/packages/rest/src/types.ts index e96514ee4..6510fab2c 100644 --- a/packages/rest/src/types.ts +++ b/packages/rest/src/types.ts @@ -99,7 +99,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 @@ -125,7 +125,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. */ diff --git a/packages/rest/tests/unit/manager.spec.ts b/packages/rest/tests/unit/manager.spec.ts index 1c85f4b37..d55f4670c 100644 --- a/packages/rest/tests/unit/manager.spec.ts +++ b/packages/rest/tests/unit/manager.spec.ts @@ -173,7 +173,7 @@ describe('[rest] manager', () => { let time: sinon.SinonFakeTimers beforeEach(() => { - rest = createRestManager({ token: ' ' }) + rest = createRestManager({ applicationId: 1n }) time = sinon.useFakeTimers() })