mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-04 18:00:08 +00:00
Merge branch 'main' of https://github.com/discordeno/discordeno into main
This commit is contained in:
@@ -27,21 +27,23 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
|
||||
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
|
||||
}
|
||||
|
||||
await Promise.all(payload.mentions.map(async (mention) => {
|
||||
// Cache the member if its a valid member
|
||||
if (mention.member && guild) {
|
||||
const discordenoMember = await structures.createDiscordenoMember(
|
||||
{ ...mention.member, user: mention } as DiscordGuildMemberWithUser,
|
||||
guild.id,
|
||||
);
|
||||
if (payload.mentions) {
|
||||
await Promise.all(payload.mentions.map(async (mention) => {
|
||||
// Cache the member if its a valid member
|
||||
if (mention.member && guild) {
|
||||
const discordenoMember = await structures.createDiscordenoMember(
|
||||
{ ...mention.member, user: mention } as DiscordGuildMemberWithUser,
|
||||
guild.id,
|
||||
);
|
||||
|
||||
return cacheHandlers.set(
|
||||
"members",
|
||||
mention.id,
|
||||
discordenoMember,
|
||||
);
|
||||
}
|
||||
}));
|
||||
return cacheHandlers.set(
|
||||
"members",
|
||||
mention.id,
|
||||
discordenoMember,
|
||||
);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
const message = await structures.createDiscordenoMessage(
|
||||
data.d as DiscordMessage,
|
||||
|
||||
@@ -10,6 +10,7 @@ import { PermissionStrings } from "../../types/permissions/permission_strings.ts
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
||||
import { camelKeysToSnakeCase } from "../../util/utils.ts";
|
||||
import { validateLength } from "../../util/validate_length.ts";
|
||||
|
||||
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
|
||||
export async function sendMessage(
|
||||
@@ -48,7 +49,7 @@ export async function sendMessage(
|
||||
}
|
||||
|
||||
// Use ... for content length due to unicode characters and js .length handling
|
||||
if (content.content && [...content.content].length > 2000) {
|
||||
if (content.content && !validateLength(content.content, { max: 2000 })) {
|
||||
throw new Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,15 +9,15 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
|
||||
"loop",
|
||||
`Running forEach loop in editBotStatus function.`,
|
||||
);
|
||||
shard.ws.send(
|
||||
JSON.stringify({
|
||||
op: DiscordGatewayOpcodes.StatusUpdate,
|
||||
d: {
|
||||
since: null,
|
||||
afk: false,
|
||||
...data,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
shard.queue.push({
|
||||
op: DiscordGatewayOpcodes.StatusUpdate,
|
||||
d: {
|
||||
since: null,
|
||||
afk: false,
|
||||
...data,
|
||||
},
|
||||
});
|
||||
ws.processQueue(shard.id);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { DiscordWebhook } from "../../types/webhooks/webhook.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
||||
import { snakeKeysToCamelCase, urlToBase64 } from "../../util/utils.ts";
|
||||
import { validateLength } from "../../util/validate_length.ts";
|
||||
|
||||
/**
|
||||
* Create a new webhook. Requires the MANAGE_WEBHOOKS permission. Returns a webhook object on success. Webhook names follow our naming restrictions that can be found in our Usernames and Nicknames documentation, with the following additional stipulations:
|
||||
@@ -21,9 +22,7 @@ export async function createWebhook(
|
||||
if (
|
||||
// Specific usernames that discord does not allow
|
||||
options.name === "clyde" ||
|
||||
// Character limit checks. [...] checks are because of js unicode length handling
|
||||
[...options.name].length < 2 ||
|
||||
[...options.name].length > 32
|
||||
!validateLength(options.name, { min: 2, max: 32 })
|
||||
) {
|
||||
throw new Error(Errors.INVALID_WEBHOOK_NAME);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { DiscordImageFormat } from "../types/misc/image_format.ts";
|
||||
import { DiscordImageSize } from "../types/misc/image_size.ts";
|
||||
import { EditGlobalApplicationCommand } from "../types/mod.ts";
|
||||
import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts";
|
||||
import { validateLength } from "./validate_length.ts";
|
||||
|
||||
export async function urlToBase64(url: string) {
|
||||
const buffer = await fetch(url).then((res) => res.arrayBuffer());
|
||||
@@ -53,7 +54,9 @@ function snakeToCamelCase(text: string) {
|
||||
|
||||
function isConvertableObject(obj: unknown) {
|
||||
return (
|
||||
obj === Object(obj) && !Array.isArray(obj) && typeof obj !== "function" &&
|
||||
obj === Object(obj) &&
|
||||
!Array.isArray(obj) &&
|
||||
typeof obj !== "function" &&
|
||||
!(obj instanceof Blob)
|
||||
);
|
||||
}
|
||||
@@ -122,7 +125,7 @@ function validateSlashOptionChoices(
|
||||
"loop",
|
||||
`Running for of loop in validateSlashOptionChoices function.`,
|
||||
);
|
||||
if ([...choice.name].length < 1 || [...choice.name].length > 100) {
|
||||
if (!validateLength(choice.name, { min: 1, max: 100 })) {
|
||||
throw new Error(Errors.INVALID_SLASH_OPTIONS_CHOICES);
|
||||
}
|
||||
|
||||
@@ -156,10 +159,8 @@ function validateSlashOptions(options: ApplicationCommandOption[]) {
|
||||
}
|
||||
|
||||
if (
|
||||
[...option.name].length < 1 ||
|
||||
[...option.name].length > 32 ||
|
||||
[...option.description].length < 1 ||
|
||||
[...option.description].length > 100
|
||||
!validateLength(option.name, { min: 1, max: 32 }) ||
|
||||
!validateLength(option.description, { min: 1, max: 100 })
|
||||
) {
|
||||
throw new Error(Errors.INVALID_SLASH_OPTIONS_CHOICES);
|
||||
}
|
||||
@@ -188,8 +189,7 @@ export function validateSlashCommands(
|
||||
|
||||
if (
|
||||
(command.description &&
|
||||
([...command.description].length < 1 ||
|
||||
[...command.description].length > 100)) ||
|
||||
!validateLength(command.description, { min: 1, max: 100 })) ||
|
||||
(create && !command.description)
|
||||
) {
|
||||
throw new Error(Errors.INVALID_SLASH_DESCRIPTION);
|
||||
|
||||
14
src/util/validate_length.ts
Normal file
14
src/util/validate_length.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/** Validates the length of a string in JS. Certain characters in JS can have multiple numbers in length in unicode and discords api is in python which treats length differently. */
|
||||
export function validateLength(
|
||||
text: string,
|
||||
options: { max?: number; min?: number },
|
||||
) {
|
||||
const length = [...text].length;
|
||||
|
||||
// Text is too long
|
||||
if (options.max && length > options.max) return false;
|
||||
// Text is too short
|
||||
if (options.min && length < options.min) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -15,10 +15,7 @@ export function heartbeat(shardId: number, interval: number) {
|
||||
shard.heartbeat.interval = interval;
|
||||
|
||||
shard.heartbeat.intervalId = setInterval(() => {
|
||||
ws.log(
|
||||
"DEBUG",
|
||||
`Running setInterval in heartbeat file.`,
|
||||
);
|
||||
ws.log("DEBUG", `Running setInterval in heartbeat file.`);
|
||||
const currentShard = ws.shards.get(shardId);
|
||||
if (!currentShard) return;
|
||||
|
||||
@@ -34,11 +31,11 @@ export function heartbeat(shardId: number, interval: number) {
|
||||
return clearInterval(currentShard.heartbeat.intervalId);
|
||||
}
|
||||
|
||||
currentShard.ws.send(
|
||||
JSON.stringify({
|
||||
op: DiscordGatewayOpcodes.Heartbeat,
|
||||
d: currentShard.previousSequenceNumber,
|
||||
}),
|
||||
);
|
||||
currentShard.queue.unshift({
|
||||
op: DiscordGatewayOpcodes.Heartbeat,
|
||||
d: currentShard.previousSequenceNumber,
|
||||
});
|
||||
|
||||
ws.processQueue(currentShard.id);
|
||||
}, interval);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import { ws } from "./ws.ts";
|
||||
|
||||
export async function identify(shardId: number, maxShards: number) {
|
||||
ws.log("IDENTIFYING", { shardId, maxShards });
|
||||
console.log("IDENTIFYING", { shardId, maxShards });
|
||||
// CREATE A SHARD
|
||||
const socket = await ws.createShard(shardId);
|
||||
|
||||
@@ -25,15 +24,16 @@ export async function identify(shardId: number, maxShards: number) {
|
||||
interval: 0,
|
||||
intervalId: 0,
|
||||
},
|
||||
queue: [],
|
||||
processingQueue: false,
|
||||
});
|
||||
|
||||
socket.onopen = () => {
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
op: DiscordGatewayOpcodes.Identify,
|
||||
d: { ...ws.identifyPayload, shard: [shardId, maxShards] },
|
||||
}),
|
||||
);
|
||||
ws.shards.get(shardId)?.queue.unshift({
|
||||
op: DiscordGatewayOpcodes.Identify,
|
||||
d: { ...ws.identifyPayload, shard: [shardId, maxShards] },
|
||||
});
|
||||
ws.processQueue(shardId);
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
36
src/ws/process_queue.ts
Normal file
36
src/ws/process_queue.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { delay } from "../util/utils.ts";
|
||||
import { ws } from "./ws.ts";
|
||||
|
||||
export async function processQueue(id: number) {
|
||||
const shard = ws.shards.get(id);
|
||||
// If no items or its already processing then exit
|
||||
if (!shard?.queue.length || shard.processingQueue) return;
|
||||
|
||||
shard.processingQueue = true;
|
||||
|
||||
let counter = 0;
|
||||
|
||||
while (shard.queue.length) {
|
||||
if (shard.ws.readyState !== WebSocket.OPEN) {
|
||||
shard.processingQueue = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Send a request that is next in line
|
||||
const request = shard.queue.shift();
|
||||
|
||||
shard.ws.send(JSON.stringify(request));
|
||||
|
||||
// Counter is useful for preventing 120/m requests.
|
||||
counter++;
|
||||
|
||||
// Handle if the requests have been maxed
|
||||
if (counter >= 120) {
|
||||
await delay(60000);
|
||||
counter = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
shard.processingQueue = false;
|
||||
}
|
||||
@@ -38,19 +38,21 @@ export async function resume(shardId: number) {
|
||||
interval: 0,
|
||||
intervalId: 0,
|
||||
},
|
||||
queue: oldShard?.queue || [],
|
||||
processingQueue: false,
|
||||
});
|
||||
|
||||
// Resume on open
|
||||
socket.onopen = () => {
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
op: DiscordGatewayOpcodes.Resume,
|
||||
d: {
|
||||
token: ws.identifyPayload.token,
|
||||
session_id: sessionId,
|
||||
seq: previousSequenceNumber,
|
||||
},
|
||||
}),
|
||||
);
|
||||
ws.shards.get(shardId)?.queue.unshift({
|
||||
op: DiscordGatewayOpcodes.Resume,
|
||||
d: {
|
||||
token: ws.identifyPayload.token,
|
||||
session_id: sessionId,
|
||||
seq: previousSequenceNumber,
|
||||
},
|
||||
});
|
||||
|
||||
ws.processQueue(shardId);
|
||||
};
|
||||
}
|
||||
|
||||
18
src/ws/ws.ts
18
src/ws/ws.ts
@@ -1,3 +1,4 @@
|
||||
import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts";
|
||||
import { Collection } from "../util/collection.ts";
|
||||
import { cleanupLoadingShards } from "./cleanup_loading_shards.ts";
|
||||
import { createShard } from "./create_shard.ts";
|
||||
@@ -9,7 +10,9 @@ import { identify } from "./identify.ts";
|
||||
import { resharder } from "./resharder.ts";
|
||||
import { spawnShards } from "./spawn_shards.ts";
|
||||
import { startGateway } from "./start_gateway.ts";
|
||||
import { processQueue } from "./process_queue.ts";
|
||||
import { tellClusterToIdentify } from "./tell_cluster_to_identify.ts";
|
||||
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
||||
|
||||
// CONTROLLER LIKE INTERFACE FOR WS HANDLING
|
||||
export const ws = {
|
||||
@@ -103,6 +106,8 @@ export const ws = {
|
||||
cleanupLoadingShards,
|
||||
/** Handles the message events from websocket */
|
||||
handleOnMessage,
|
||||
/** Handles processing queue of requests send to this shard */
|
||||
processQueue,
|
||||
};
|
||||
|
||||
export interface DiscordenoShard {
|
||||
@@ -136,4 +141,17 @@ export interface DiscordenoShard {
|
||||
/** The id of the interval, useful for stopping the interval if ws closed. */
|
||||
intervalId: number;
|
||||
};
|
||||
/** The items/requestst that are in queue to be sent to this shard websocket. */
|
||||
queue: WebSocketRequest[];
|
||||
/** Whether or not the queue for this shard is being processed. */
|
||||
processingQueue: boolean;
|
||||
}
|
||||
|
||||
export interface WebSocketRequest {
|
||||
op: DiscordGatewayOpcodes;
|
||||
d: unknown;
|
||||
// guildId: string;
|
||||
// shardId: number;
|
||||
// nonce?: string;
|
||||
// options?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||
channelOverwriteHasPermission(
|
||||
channel.guildId,
|
||||
botId,
|
||||
cache.channels.get(channel.id)?.permissionOverwrites,
|
||||
cache.channels.get(channel.id)?.permissionOverwrites || [],
|
||||
options.permissionOverwrites ? options.permissionOverwrites[0].allow : [],
|
||||
),
|
||||
true,
|
||||
|
||||
@@ -38,7 +38,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||
|
||||
if (
|
||||
options.permissionOverwrites &&
|
||||
channel.permissionOverwrites.length !== options.permissionOverwrites.length
|
||||
channel.permissionOverwrites?.length !== options.permissionOverwrites.length
|
||||
) {
|
||||
throw new Error(
|
||||
"The channel was supposed to have a permissionOverwrites but it does not appear to be the same permissionOverwrites.",
|
||||
|
||||
@@ -25,7 +25,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||
|
||||
if (
|
||||
options.permissionOverwrites &&
|
||||
channel.permissionOverwrites.length !== options.permissionOverwrites.length
|
||||
channel.permissionOverwrites?.length !== options.permissionOverwrites.length
|
||||
) {
|
||||
throw new Error(
|
||||
"The channel was supposed to have a permissionOverwrites but it does not appear to be the same permissionOverwrites.",
|
||||
|
||||
@@ -47,7 +47,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||
channelOverwriteHasPermission(
|
||||
channel.guildId,
|
||||
botId,
|
||||
cache.channels.get(channel.id)?.permissionOverwrites,
|
||||
cache.channels.get(channel.id)?.permissionOverwrites || [],
|
||||
["VIEW_CHANNEL", "ADD_REACTIONS"],
|
||||
),
|
||||
);
|
||||
@@ -56,7 +56,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||
channelOverwriteHasPermission(
|
||||
channel.guildId,
|
||||
botId,
|
||||
cache.channels.get(channel.id)?.permissionOverwrites,
|
||||
cache.channels.get(channel.id)?.permissionOverwrites || [],
|
||||
["VIEW_CHANNEL", "ADD_REACTIONS"],
|
||||
),
|
||||
true,
|
||||
|
||||
@@ -22,11 +22,11 @@ Deno.test({
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
true,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -21,18 +21,18 @@ async function ifItFailsBlameWolf(reason?: string) {
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
);
|
||||
|
||||
await deleteEmoji(tempData.guildId, emoji.id!, reason);
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => !cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
() => !cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -23,22 +23,22 @@ Deno.test({
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
);
|
||||
|
||||
await editEmoji(tempData.guildId, emoji.id, {
|
||||
await editEmoji(tempData.guildId, emoji.id!, {
|
||||
name: "blamewolf_infinite",
|
||||
});
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() =>
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.get(emoji.id)?.name ===
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.get(emoji.id!)?.name ===
|
||||
"blamewolf_infinite",
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.get(emoji.id)?.name,
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.get(emoji.id!)?.name,
|
||||
"blamewolf_infinite",
|
||||
);
|
||||
},
|
||||
|
||||
@@ -23,20 +23,20 @@ Deno.test({
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
);
|
||||
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.delete(emoji.id);
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.delete(emoji.id!);
|
||||
|
||||
await getEmoji(tempData.guildId, emoji.id!);
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id),
|
||||
cache.guilds.get(tempData.guildId)?.emojis?.has(emoji.id!),
|
||||
true,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -13,10 +13,13 @@ Deno.test({
|
||||
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.guilds.get(tempData.guildId)?.emojis?.size > 0,
|
||||
() => (cache.guilds.get(tempData.guildId)?.emojis?.size || 0) > 0,
|
||||
);
|
||||
|
||||
assertEquals(cache.guilds.get(tempData.guildId)?.emojis?.size > 0, true);
|
||||
assertEquals(
|
||||
(cache.guilds.get(tempData.guildId)?.emojis?.size || 0) > 0,
|
||||
true,
|
||||
);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
|
||||
@@ -24,7 +24,7 @@ async function ifItFailsBlameWolf(
|
||||
},
|
||||
};
|
||||
} else if (content === "reply") {
|
||||
const message = await sendMessage(channel.id, "Test Message");
|
||||
const message = await sendMessage(channel!.id, "Test Message");
|
||||
assertExists(message);
|
||||
// Wait few seconds for the channel create event to arrive and cache it
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
@@ -38,7 +38,7 @@ async function ifItFailsBlameWolf(
|
||||
},
|
||||
messageReference: {
|
||||
messageId: message.id,
|
||||
channelId: channel.id,
|
||||
channelId: channel?.id,
|
||||
guildId: tempData.guildId,
|
||||
failIfNotExists: true,
|
||||
},
|
||||
@@ -46,26 +46,26 @@ async function ifItFailsBlameWolf(
|
||||
}
|
||||
|
||||
const message = type === "raw"
|
||||
? await sendMessage(channel.id, messageContent)
|
||||
: await channel.send(messageContent);
|
||||
? await sendMessage(channel!.id, messageContent)
|
||||
: await channel?.send(messageContent);
|
||||
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message!.id));
|
||||
|
||||
if (!cache.messages.has(message.id)) {
|
||||
if (!cache.messages.has(message!.id)) {
|
||||
throw new Error("The message seemed to be sent but it was not cached.");
|
||||
}
|
||||
|
||||
if (content === "string") {
|
||||
assertEquals(cache.messages.get(message.id)?.content, messageContent);
|
||||
assertEquals(cache.messages.get(message!.id)?.content, messageContent);
|
||||
} else if (content === "embed") {
|
||||
assertEquals(cache.messages.get(message.id)?.embeds?.length, 1);
|
||||
assertEquals(cache.messages.get(message!.id)?.embeds?.length, 1);
|
||||
} else {
|
||||
assertEquals(
|
||||
cache.messages.get(message.id)?.messageReference.messageId,
|
||||
cache.messages.get(message!.id)?.messageReference?.messageId,
|
||||
secondMessageId,
|
||||
);
|
||||
}
|
||||
@@ -125,20 +125,20 @@ Deno.test({
|
||||
const channel = cache.channels.get(tempData.channelId);
|
||||
assertExists(channel);
|
||||
|
||||
const message = await sendMessage(channel.id, "Test Message");
|
||||
const message = await sendMessage(channel!.id, "Test Message");
|
||||
assertExists(message);
|
||||
// Wait few seconds for the channel create event to arrive and cache it
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message!.id));
|
||||
|
||||
const reply = await message.reply("Welcome!");
|
||||
await delayUntil(10000, () => cache.messages.has(reply.id));
|
||||
await delayUntil(10000, () => cache.messages.has(reply!.id));
|
||||
|
||||
if (!cache.messages.has(reply.id)) {
|
||||
if (!cache.messages.has(reply!.id)) {
|
||||
throw new Error("The message seemed to be sent but it was not cached.");
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
cache.messages.get(reply.id)?.messageReference.messageId,
|
||||
cache.messages.get(reply.id)?.messageReference?.messageId,
|
||||
message.id,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -43,7 +43,7 @@ Deno.test({
|
||||
limit: 2,
|
||||
});
|
||||
// Check if getMessages has worked
|
||||
assertEquals(fetchedMessages.length, 2);
|
||||
assertEquals(fetchedMessages?.length, 2);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// First complete non-api reliant testing.
|
||||
// Don't waste api rate limits if a early test fails.
|
||||
import "./util/utils.ts";
|
||||
import "./util/validate_length.ts";
|
||||
|
||||
// API TESTING BELOW
|
||||
|
||||
|
||||
@@ -26,20 +26,20 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||
if (type === "raw") {
|
||||
await addRole(tempData.guildId, botId, role.id, reason);
|
||||
} else {
|
||||
await cache.members.get(botId).addRole(tempData.guildId, role.id, reason);
|
||||
await cache.members.get(botId)!.addRole(tempData.guildId, role.id, reason);
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed
|
||||
await delayUntil(
|
||||
10000,
|
||||
() =>
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId)!.roles.includes(
|
||||
role.id,
|
||||
),
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId)!.roles.includes(
|
||||
role.id,
|
||||
),
|
||||
true,
|
||||
|
||||
@@ -27,14 +27,14 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||
if (type === "raw") {
|
||||
await addRole(tempData.guildId, botId, role.id, reason);
|
||||
} else {
|
||||
await cache.members.get(botId).addRole(tempData.guildId, role.id, reason);
|
||||
await cache.members.get(botId)!.addRole(tempData.guildId, role.id, reason);
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed
|
||||
await delayUntil(
|
||||
10000,
|
||||
() =>
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId)!.roles.includes(
|
||||
role.id,
|
||||
),
|
||||
);
|
||||
@@ -42,7 +42,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||
if (type === "raw") {
|
||||
await removeRole(tempData.guildId, botId, role.id, reason);
|
||||
} else {
|
||||
await cache.members.get(botId).removeRole(
|
||||
await cache.members.get(botId)!.removeRole(
|
||||
tempData.guildId,
|
||||
role.id,
|
||||
reason,
|
||||
@@ -53,13 +53,13 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||
await delayUntil(
|
||||
10000,
|
||||
() =>
|
||||
!cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(
|
||||
!cache.members.get(botId)?.guilds.get(tempData.guildId)!.roles.includes(
|
||||
role.id,
|
||||
),
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(
|
||||
cache.members.get(botId)?.guilds.get(tempData.guildId)!.roles.includes(
|
||||
role.id,
|
||||
),
|
||||
false,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export async function delayUntil(
|
||||
maxMs: number,
|
||||
isReady: () => boolean,
|
||||
isReady: () => boolean | undefined,
|
||||
timeoutTime = 100,
|
||||
): Promise<void> {
|
||||
const maxTime = Date.now() + maxMs;
|
||||
|
||||
44
tests/util/validate_length.ts
Normal file
44
tests/util/validate_length.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { validateLength } from "../../src/util/validate_length.ts";
|
||||
import { assertEquals } from "../deps.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] Validate length is too low",
|
||||
fn() {
|
||||
assertEquals(validateLength("test", { min: 5 }), false);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] Validate length is too high",
|
||||
fn() {
|
||||
assertEquals(validateLength("test", { max: 3 }), false);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] Validate length is NOT just right in between.",
|
||||
fn() {
|
||||
assertEquals(validateLength("test", { min: 5, max: 3 }), false);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] Validate length is NOT too low",
|
||||
fn() {
|
||||
assertEquals(validateLength("test", { min: 3 }), true);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] Validate length is NOT too high",
|
||||
fn() {
|
||||
assertEquals(validateLength("test", { max: 5 }), true);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] Validate length is just right in between.",
|
||||
fn() {
|
||||
assertEquals(validateLength("test", { min: 3, max: 6 }), true);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user