From 8cb912730b94d1cbad7018f015438ac436266e30 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 8 May 2022 11:34:33 -0400 Subject: [PATCH] feat: remove token prefix util (#2191) * feat: remove token prefix util * fix: token substring * fix: speed up unit tests --- bot.ts | 3 +- gateway/gatewayManager.ts | 3 +- rest/restManager.ts | 3 +- testss/channels.test.ts | 44 +++++++++++++++++++++ testss/channels/deleteWithReason.test.ts | 23 ----------- testss/channels/deleteWithoutReason.test.ts | 24 ----------- testss/utils.test.ts | 16 ++++++++ util/token.ts | 9 +++++ 8 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 testss/channels.test.ts delete mode 100644 testss/channels/deleteWithReason.test.ts delete mode 100644 testss/channels/deleteWithoutReason.test.ts create mode 100644 testss/utils.test.ts create mode 100644 util/token.ts diff --git a/bot.ts b/bot.ts index be98f4975..1b0305edf 100644 --- a/bot.ts +++ b/bot.ts @@ -134,12 +134,13 @@ 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"; export function createBot(options: CreateBotOptions): Bot { const bot = { id: options.botId, applicationId: options.applicationId || options.botId, - token: options.token, + token: removeTokenPrefix(options.token), events: createEventHandlers(options.events), intents: options.intents.reduce( (bits, next) => (bits |= GatewayIntents[next]), diff --git a/gateway/gatewayManager.ts b/gateway/gatewayManager.ts index 83d70a31d..cc5312010 100644 --- a/gateway/gatewayManager.ts +++ b/gateway/gatewayManager.ts @@ -24,6 +24,7 @@ import { GatewayIntents } from "../types/shared.ts"; import { StatusUpdate } from "../helpers/misc/editBotStatus.ts"; import { DiscordGatewayPayload } from "../types/discord.ts"; import { calculateMaxShards } from "./calculateMaxShards.ts"; +import { removeTokenPrefix } from "../util/token.ts"; /** Create a new Gateway Manager. * @@ -53,7 +54,7 @@ export function createGatewayManager( maxWorkers: options.maxWorkers ?? 4, firstShardId: options.firstShardId ?? 0, lastShardId: options.lastShardId ?? options.maxShards ?? options.shardsRecommended ?? 1, - token: options.token ?? "", + token: removeTokenPrefix(options.token, "GATEWAY"), compress: options.compress ?? false, $os: options.$os ?? "linux", $browser: options.$browser ?? "Discordeno", diff --git a/rest/restManager.ts b/rest/restManager.ts index 77b932d53..10c1d6ca9 100644 --- a/rest/restManager.ts +++ b/rest/restManager.ts @@ -11,6 +11,7 @@ import { runMethod } from "./runMethod.ts"; import { simplifyUrl } from "./simplifyUrl.ts"; import { baseEndpoints } from "../util/constants.ts"; import { API_VERSION } from "../util/constants.ts"; +import { removeTokenPrefix } from "../util/token.ts"; export function createRestManager(options: CreateRestManagerOptions) { const version = options.version || API_VERSION; @@ -34,7 +35,7 @@ export function createRestManager(options: CreateRestManagerOptions) { invalidRequestFrozenAt: 0, invalidRequestErrorStatuses: [401, 403, 429], version, - token: options.token, + token: removeTokenPrefix(options.token), maxRetryCount: options.maxRetryCount || 10, secretKey: options.secretKey || "discordeno_best_lib_ever", customUrl: options.customUrl || "", diff --git a/testss/channels.test.ts b/testss/channels.test.ts new file mode 100644 index 000000000..d67d1a006 --- /dev/null +++ b/testss/channels.test.ts @@ -0,0 +1,44 @@ +import { assertEquals, assertExists } from "./deps.ts"; +import { loadBot } from "./mod.ts"; +import { CACHED_COMMUNITY_GUILD_ID } from "./utils.ts"; + +Deno.test({ + name: "[channel] delete a channel without a reason", + async fn(t) { + const bot = loadBot(); + // Create a channel to delete + const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { + name: "delete-channel", + }); + + // Make sure the channel was created + assertExists(channel.id); + + // Delete the channel now without a reason + await bot.helpers.deleteChannel(channel.id); + + // Check if channel still exists + const exists = await bot.helpers.getChannel(channel.id); + assertEquals(exists, undefined); + }, +}); + +Deno.test({ + name: "[channel] delete a channel with a reason", + async fn(t) { + const bot = loadBot(); + const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { + name: "delete-channel", + }); + + // Make sure the channel was created + assertExists(channel.id); + + // Delete the channel now with a reason + await bot.helpers.deleteChannel(channel.id, "with a reason"); + + // Check if channel still exists + const exists = await bot.helpers.getChannel(channel.id); + assertEquals(exists, undefined); + }, +}); diff --git a/testss/channels/deleteWithReason.test.ts b/testss/channels/deleteWithReason.test.ts deleted file mode 100644 index d9abc8936..000000000 --- a/testss/channels/deleteWithReason.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { assertEquals, assertExists } from "../deps.ts"; -import { loadBot } from "../mod.ts"; -import { CACHED_COMMUNITY_GUILD_ID, delayUntil } from "../utils.ts"; - -Deno.test({ - name: "[channel] delete a channel with a reason", - async fn(t) { - const bot = loadBot(); - const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { - name: "delete-channel", - }); - - // Make sure the channel was created - assertExists(channel.id); - - // Delete the channel now with a reason - await bot.helpers.deleteChannel(channel.id, "with a reason"); - - // Check if channel still exists - const exists = await bot.helpers.getChannel(channel.id); - assertEquals(exists, undefined); - }, -}); diff --git a/testss/channels/deleteWithoutReason.test.ts b/testss/channels/deleteWithoutReason.test.ts deleted file mode 100644 index 9295dfa4b..000000000 --- a/testss/channels/deleteWithoutReason.test.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { assertEquals, assertExists } from "../deps.ts"; -import { loadBot } from "../mod.ts"; -import { CACHED_COMMUNITY_GUILD_ID } from "../utils.ts"; - -Deno.test({ - name: "[channel] delete a channel without a reason", - async fn(t) { - const bot = loadBot(); - // Create a channel to delete - const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { - name: "delete-channel", - }); - - // Make sure the channel was created - assertExists(channel.id); - - // Delete the channel now without a reason - await bot.helpers.deleteChannel(channel.id); - - // Check if channel still exists - const exists = await bot.helpers.getChannel(channel.id); - assertEquals(exists, undefined); - }, -}); diff --git a/testss/utils.test.ts b/testss/utils.test.ts new file mode 100644 index 000000000..e9878b8a7 --- /dev/null +++ b/testss/utils.test.ts @@ -0,0 +1,16 @@ +import { removeTokenPrefix } from "../util/token.ts"; +import { assertEquals } from "./deps.ts"; + +Deno.test({ + name: "[token] Remove token prefix when Bot is prefixed.", + async fn(t) { + assertEquals("discordeno is best lib", removeTokenPrefix("Bot discordeno is best lib")); + }, +}); + +Deno.test({ + name: "[token] Remove token prefix when Bot is NOT prefixed.", + async fn(t) { + assertEquals("discordeno is best lib", removeTokenPrefix("discordeno is best lib")); + }, +}); diff --git a/util/token.ts b/util/token.ts new file mode 100644 index 000000000..23053a063 --- /dev/null +++ b/util/token.ts @@ -0,0 +1,9 @@ +/** Removes the Bot before the token. */ +export function removeTokenPrefix(token?: string, type: "GATEWAY" | "REST" = "REST"): string { + // If no token is provided, throw an error + if (!token) throw new Error(`The ${type} was not given a token. Please provide a token and try again.`); + // If the token does not have a prefix just return token + if (!token.startsWith("Bot ")) return token; + // Remove the prefix and return only the token. + return token.substring(token.indexOf(" ") + 1); +}