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
This commit is contained in:
ITOH
2023-04-02 21:39:08 +02:00
committed by GitHub
parent 5e1e56146e
commit 7c031df875
3 changed files with 10 additions and 6 deletions

View File

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

View File

@@ -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. */

View File

@@ -173,7 +173,7 @@ describe('[rest] manager', () => {
let time: sinon.SinonFakeTimers
beforeEach(() => {
rest = createRestManager({ token: ' ' })
rest = createRestManager({ applicationId: 1n })
time = sinon.useFakeTimers()
})