diff --git a/bot.ts b/bot.ts index 29a54bb84..0a7652b9f 100644 --- a/bot.ts +++ b/bot.ts @@ -137,11 +137,11 @@ import { } from "./transformers/applicationCommandOptionChoice.ts"; import { transformEmbedToDiscordEmbed } from "./transformers/reverse/embed.ts"; import { transformComponentToDiscordComponent } from "./transformers/reverse/component.ts"; -import { removeTokenPrefix } from "./util/token.ts"; +import { getBotIdFromToken, removeTokenPrefix } from "./util/token.ts"; export function createBot(options: CreateBotOptions): Bot { const bot = { - id: options.botId, + id: options.botId ?? getBotIdFromToken(options.token), applicationId: options.applicationId || options.botId, token: removeTokenPrefix(options.token), events: createEventHandlers(options.events ?? {}), @@ -314,7 +314,7 @@ export async function stopBot(bot: Bot) { export interface CreateBotOptions { token: string; - botId: bigint; + botId?: bigint; applicationId?: bigint; secretKey?: string; events?: Partial; diff --git a/util/token.ts b/util/token.ts index 23053a063..795455ec2 100644 --- a/util/token.ts +++ b/util/token.ts @@ -7,3 +7,8 @@ export function removeTokenPrefix(token?: string, type: "GATEWAY" | "REST" = "RE // Remove the prefix and return only the token. return token.substring(token.indexOf(" ") + 1); } + +/** 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) { + return BigInt(atob(token.split(".")[0])); +}