REST randomly forgets token set with setToken

I'm using @discordjs/rest and Fastify for HTTPS interactions. I'm initiating my REST client with:
const rest = new REST().setToken(DISCORD_TOKEN);
const rest = new REST().setToken(DISCORD_TOKEN);
The bot works fine for roughly a day before suddenly rejecting all requests with: Error: Expected token to be set for this request, but none was present https://github.com/discordjs/discord.js/blob/65d1879c0ae2d8d820323d0d835b1e5e3d071e57/packages/rest/src/lib/RequestManager.ts#L364 I can not, for the life of me, figure out why rest is consistently forgetting its token after roughly a day of operation. What am I missing?
6 Replies
Stev
Stev•2y ago
Restarting my app manually (via PM2) actually fixes the problem. But if it were to restart, it would just call this again - in which case it would have the token:
// rest.ts

import { REST } from '@discordjs/rest';

// Load required enviroment variables.
const DISCORD_TOKEN = process.env.DISCORD_TOKEN as string;

// Initiate REST.
const rest = new REST().setToken(DISCORD_TOKEN);

export default rest;
// rest.ts

import { REST } from '@discordjs/rest';

// Load required enviroment variables.
const DISCORD_TOKEN = process.env.DISCORD_TOKEN as string;

// Initiate REST.
const rest = new REST().setToken(DISCORD_TOKEN);

export default rest;
Everything that requires the REST client simply imports that ^. Works fine for roughly a day until every request rejects and I need to manually restart the PM2 process
DD
DD•2y ago
hmmm @steveloper, we do have a code path that sets your token to null to protect you from a CF ban 👀
if (status >= 400 && status < 500) {
// If we receive this status code, it means the token we had is no longer valid.
if (status === 401 && requestData.auth) {
this.manager.setToken(null!);
}
// The request will not succeed for some reason, parse the error returned from the api
const data = (await parseResponse(res)) as DiscordErrorData | OAuthErrorData;
// throw the API error
throw new DiscordAPIError(data, 'code' in data ? data.code : data.error, status, method, url, requestData);
}
if (status >= 400 && status < 500) {
// If we receive this status code, it means the token we had is no longer valid.
if (status === 401 && requestData.auth) {
this.manager.setToken(null!);
}
// The request will not succeed for some reason, parse the error returned from the api
const data = (await parseResponse(res)) as DiscordErrorData | OAuthErrorData;
// throw the API error
throw new DiscordAPIError(data, 'code' in data ? data.code : data.error, status, method, url, requestData);
}
it happens if you somehow hit a 401 sadly this doesn't fire a debug event so you can't easily confirm if this is what happens
souji
souji•2y ago
#justaddone
DD
DD•2y ago
but if you could be so kind to listen to RESTEvents.Response and see if you get a 401 about one request before the one that causes the throw that'd be lovely I'm particularly interested in the 2nd object you get in that event - the Response object, I want to see what route/method it is that causes your token to reset
Stev
Stev•2y ago
I will add that now, but it will take ~a day to occur
DD
DD•2y ago
yup yup feel free to @ me back once it happens