feat: remove token prefix util (#2191)

* feat: remove token prefix util

* fix: token substring

* fix: speed up unit tests
This commit is contained in:
Skillz4Killz
2022-05-08 11:34:33 -04:00
committed by GitHub
parent 602940e636
commit 8cb912730b
8 changed files with 75 additions and 50 deletions
+2 -1
View File
@@ -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]),
+2 -1
View File
@@ -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",
+2 -1
View File
@@ -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 || "",
+44
View File
@@ -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);
},
});
-23
View File
@@ -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);
},
});
@@ -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);
},
});
+16
View File
@@ -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"));
},
});
+9
View File
@@ -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);
}