Files
discordeno/packages/utils/src/token.ts
T
Fleny 27c261fee2 formatter: Use semicolons (#4686)
I prefer semicolors, they also help avoiding certain pitfalls in JavaScript/TypeScript, such as the following code sample:
```js
const xyz = "test"
(something.else as string) = "another"
```
This results in a TypeError: "test" is not a function, this is because js thinks we are trying to call the string "test" as a function.
To fix this it requires a `;` somewhere before the `(`, such as `;(something ... ` which in my opinion is ugly and less clean overall.
2026-01-17 21:54:15 +01:00

22 lines
953 B
TypeScript

const validTokenPrefixes = ['Bot', 'Bearer'];
/** Removes the Bot/Bearer before the token. */
export function removeTokenPrefix(token?: string, type: 'GATEWAY' | 'REST' = 'REST'): string {
// If no token is provided, throw an error
if (token === undefined) {
throw new Error(`The ${type} was not given a token. Please provide a token and try again.`);
}
const splittedToken = token.split(' ');
// If the token does not have a prefix just return token
if (splittedToken.length < 2 || !validTokenPrefixes.includes(splittedToken[0])) return token;
// Remove the prefix and return only the token.
return splittedToken.splice(1).join(' ');
}
/** Get the bot id from the bot token. WARNING: Discord staff has mentioned this may not be stable forever. Use at your own risk. However, note for over 5 years this has never broken. */
export function getBotIdFromToken(token: string): bigint {
return BigInt(atob(token.split('.')[0]));
}