fix: bot id optional on createBot

This commit is contained in:
Skillz4Killz
2022-05-18 16:57:50 +00:00
committed by GitHub
parent 4c67ea3482
commit bc2a7ade24
2 changed files with 8 additions and 3 deletions

6
bot.ts
View File

@@ -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<EventHandlers>;

View File

@@ -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]));
}