From 403f93ec27708a98936340f06271b44320eec6b0 Mon Sep 17 00:00:00 2001 From: lts372005 <87189679+lts372005@users.noreply.github.com> Date: Sat, 30 Oct 2021 10:49:49 +0700 Subject: [PATCH 01/20] [Unit Test] utils: delay --- tests/local.ts | 4 ++-- tests/util/{hasProperty.ts => utils.ts} | 17 ++++++++++++++++- .../{validateLength.ts => validate_length.ts} | 0 3 files changed, 18 insertions(+), 3 deletions(-) rename tests/util/{hasProperty.ts => utils.ts} (51%) rename tests/util/{validateLength.ts => validate_length.ts} (100%) diff --git a/tests/local.ts b/tests/local.ts index 5858743f4..bba4ce566 100644 --- a/tests/local.ts +++ b/tests/local.ts @@ -1,4 +1,4 @@ import "./local/snowflake.ts"; -import "./util/validateLength.ts"; -import "./util/hasProperty.ts"; +import "./util/validate_length.ts"; +import "./util/utils.ts"; import "./util/hash.ts"; diff --git a/tests/util/hasProperty.ts b/tests/util/utils.ts similarity index 51% rename from tests/util/hasProperty.ts rename to tests/util/utils.ts index 7cf0bd412..8ec25bee2 100644 --- a/tests/util/hasProperty.ts +++ b/tests/util/utils.ts @@ -1,5 +1,8 @@ -import { hasProperty } from "../../src/util/utils.ts"; +import { hasProperty, delay } from "../../src/util/utils.ts"; import { assertEquals } from "../deps.ts"; + +// hasProperty + const obj = { prop: "lts372005" }; Deno.test({ name: "[utils] hasProperty does HAVE property", @@ -13,3 +16,15 @@ Deno.test({ assertEquals(hasProperty(obj, "lts372005"), false); }, }); + +// delay + +Deno.test({ + name: "[utils] delay 2000 ms", + async fn() { + const before = Date.now(); + await delay(2000); + const after = Date.now(); + if (after - before < 2000) throw new Error(`delay(2000) delayed ${after - before}ms`); + }, +}); diff --git a/tests/util/validateLength.ts b/tests/util/validate_length.ts similarity index 100% rename from tests/util/validateLength.ts rename to tests/util/validate_length.ts From 150fc394437b1f35e1cb4693528f718fcf66dc56 Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 11:21:33 +0200 Subject: [PATCH 02/20] [Unit Test] messages: sendMessage --- tests/helpers/messages/sendMessage.ts | 91 +++++++++++++++++++++++++++ tests/mod.ts | 38 +++++++---- 2 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 tests/helpers/messages/sendMessage.ts diff --git a/tests/helpers/messages/sendMessage.ts b/tests/helpers/messages/sendMessage.ts new file mode 100644 index 000000000..29738fea1 --- /dev/null +++ b/tests/helpers/messages/sendMessage.ts @@ -0,0 +1,91 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import {CreateMessage} from "../../../src/types/messages/create_message.ts"; +import { + DiscordMessageComponentTypes +} from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/message_component_types.ts"; +import { + DiscordButtonStyles +} from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/button_styles.ts"; + +async function ifItFailsBlameWolf(bot: Bot, channelId: bigint, content: string | CreateMessage) { + const message = await bot.helpers.sendMessage(channelId, content); + // Assertions + assertExists(message); + // Delay the execution by to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => bot.cache.messages.has(message.id)); + // Make sure the message was created. + if (!bot.cache.messages.has(message.id)) { + throw new Error("The message seemed to be sent but it was not cached."); + } +} + +export async function sendMessageWithTextTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, "Hello World!"); +} + +export async function sendMessageWithComponents(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, { + content: "Hello World!", + components: [ + { + type: DiscordMessageComponentTypes.ActionRow, + components: [ + { + type: DiscordMessageComponentTypes.Button, + label: "Doc", + style: DiscordButtonStyles.Link, + url: `https://discordeno.mod.land/` + }, + { + type: DiscordMessageComponentTypes.Button, + label: "Server", + style: DiscordButtonStyles.Link, + url: `https://discord.gg/ddeno` + }, + ] + }, + { + type: DiscordMessageComponentTypes.ActionRow, + components: [ + { + type: DiscordMessageComponentTypes.Button, + label: "Hi", + customId: `hi`, + style: DiscordButtonStyles.Primary, + }, + ] + } + ] + }); +} + +export async function sendMessageWithEmbedsTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, { + embeds: [ + { + title: "Hello World", + description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam at cursus libero. Sed egestas nec ligula sit amet sollicitudin. Curabitur.", + color: 0x00ff00, + footer: { + text: "Discordeno Best Lib" + }, + author: { + name: "Cacahe" + } + }, + { + title: "Goodbye World", + description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean libero enim, blandit tincidunt magna non, auctor pellentesque lacus. Nulla diam.", + color: 0x0000ff, + footer: { + text: "Discordeno Best Lib" + }, + author: { + name: "Wolf" + } + } + ] + }); +} diff --git a/tests/mod.ts b/tests/mod.ts index 1a4fa26ac..6604c5cf1 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -4,6 +4,11 @@ import { assertEquals, assertExists } from "./deps.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; import { editMessageTest } from "./helpers/messages/editMessage.ts"; import { delayUntil } from "./utils.ts"; +import { + sendMessageWithComponents, + sendMessageWithEmbedsTest, + sendMessageWithTextTest +} from "./helpers/messages/sendMessage.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST import "./local.ts"; @@ -77,20 +82,29 @@ Deno.test("[Bot] - Starting Tests", async (t) => { // ALL MESSAGE RELATED TESTS THAT DEPEND ON AN EXISTING CHANNEL await t.step("Message related tests", async (t) => { - const message = await bot.helpers.sendMessage(channel.id, "Testing"); - - // Assertions - assertExists(message); - - // Delay the execution to allow MESSAGE_CREATE event to be processed - await delayUntil(10000, () => bot.cache.messages.has(message.id)); - - if (!bot.cache.messages.has(message.id)) { - throw new Error("The message seemed to be sent but it was not cached."); - } - // CONDUCT ALL TESTS RELATED TO A MESSAGE HERE await Promise.all([ + t.step({ + name: "[message] send message with text", + fn: async (t) => { + await sendMessageWithTextTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[message] send message with embeds", + fn: async (t) => { + await sendMessageWithEmbedsTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[message] send message with components", + fn: async (t) => { + await sendMessageWithComponents(bot, channel.id, t); + }, + ...sanitizeMode, + }), t.step({ name: "[message] delete message without a reason", fn: async (t) => { From 66700adbcba3db6dd8086a9d092fbba989728d46 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 09:22:16 +0000 Subject: [PATCH 03/20] change: prettier code --- tests/helpers/messages/sendMessage.ts | 44 +++++++++++++-------------- tests/mod.ts | 4 +-- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/tests/helpers/messages/sendMessage.ts b/tests/helpers/messages/sendMessage.ts index 29738fea1..47e471111 100644 --- a/tests/helpers/messages/sendMessage.ts +++ b/tests/helpers/messages/sendMessage.ts @@ -1,13 +1,9 @@ import { Bot } from "../../../src/bot.ts"; import { assertExists } from "../../deps.ts"; import { delayUntil } from "../../utils.ts"; -import {CreateMessage} from "../../../src/types/messages/create_message.ts"; -import { - DiscordMessageComponentTypes -} from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/message_component_types.ts"; -import { - DiscordButtonStyles -} from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/button_styles.ts"; +import { CreateMessage } from "../../../src/types/messages/create_message.ts"; +import { DiscordMessageComponentTypes } from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/message_component_types.ts"; +import { DiscordButtonStyles } from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/button_styles.ts"; async function ifItFailsBlameWolf(bot: Bot, channelId: bigint, content: string | CreateMessage) { const message = await bot.helpers.sendMessage(channelId, content); @@ -36,15 +32,15 @@ export async function sendMessageWithComponents(bot: Bot, channelId: bigint, t: type: DiscordMessageComponentTypes.Button, label: "Doc", style: DiscordButtonStyles.Link, - url: `https://discordeno.mod.land/` + url: `https://discordeno.mod.land/`, }, { type: DiscordMessageComponentTypes.Button, label: "Server", style: DiscordButtonStyles.Link, - url: `https://discord.gg/ddeno` + url: `https://discord.gg/ddeno`, }, - ] + ], }, { type: DiscordMessageComponentTypes.ActionRow, @@ -55,9 +51,9 @@ export async function sendMessageWithComponents(bot: Bot, channelId: bigint, t: customId: `hi`, style: DiscordButtonStyles.Primary, }, - ] - } - ] + ], + }, + ], }); } @@ -66,26 +62,28 @@ export async function sendMessageWithEmbedsTest(bot: Bot, channelId: bigint, t: embeds: [ { title: "Hello World", - description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam at cursus libero. Sed egestas nec ligula sit amet sollicitudin. Curabitur.", + description: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam at cursus libero. Sed egestas nec ligula sit amet sollicitudin. Curabitur.", color: 0x00ff00, footer: { - text: "Discordeno Best Lib" + text: "Discordeno Best Lib", }, author: { - name: "Cacahe" - } + name: "Cacahe", + }, }, { title: "Goodbye World", - description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean libero enim, blandit tincidunt magna non, auctor pellentesque lacus. Nulla diam.", + description: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean libero enim, blandit tincidunt magna non, auctor pellentesque lacus. Nulla diam.", color: 0x0000ff, footer: { - text: "Discordeno Best Lib" + text: "Discordeno Best Lib", }, author: { - name: "Wolf" - } - } - ] + name: "Wolf", + }, + }, + ], }); } diff --git a/tests/mod.ts b/tests/mod.ts index 6604c5cf1..facb9982e 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -7,7 +7,7 @@ import { delayUntil } from "./utils.ts"; import { sendMessageWithComponents, sendMessageWithEmbedsTest, - sendMessageWithTextTest + sendMessageWithTextTest, } from "./helpers/messages/sendMessage.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST @@ -125,7 +125,7 @@ Deno.test("[Bot] - Starting Tests", async (t) => { await editMessageTest(bot, channel.id, t); }, ...sanitizeMode, - }) + }), ]); }); }); From bd8659359f90eb33d3d7b56205c8fee27009f578 Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 11:22:59 +0200 Subject: [PATCH 04/20] Revert "[Unit Test] messages: editMessage" This reverts commit 1a3123011b2b7e420287410dc6e6bcdc88fdbca1. --- tests/helpers/messages/editMessage.ts | 26 -------------------------- tests/mod.ts | 8 -------- 2 files changed, 34 deletions(-) delete mode 100644 tests/helpers/messages/editMessage.ts diff --git a/tests/helpers/messages/editMessage.ts b/tests/helpers/messages/editMessage.ts deleted file mode 100644 index 0ab4f2f8b..000000000 --- a/tests/helpers/messages/editMessage.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Bot } from "../../../src/bot.ts"; -import { assertExists } from "../../deps.ts"; -import { delayUntil } from "../../utils.ts"; - -export async function editMessageTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { - const message = await bot.helpers.sendMessage(channelId, "Hello World!"); - - // Assertions - assertExists(message); - // Delay the execution by to allow MESSAGE_CREATE event to be processed - await delayUntil(10000, () => bot.cache.messages.has(message.id)); - // Make sure the message was created. - if (!bot.cache.messages.has(message.id)) { - throw new Error("The message seemed to be sent but it was not cached. Reason: ${reason}"); - } - - // Delete the message now - await bot.helpers.editMessage(channelId, message.id, "Goodbye World!"); - - // Wait to give it time for MESSAGE_UPDATE event - await delayUntil(10000, async () => (await bot.cache.messages.get(message.id))?.content === "Goodbye World!"); - // Make sure it was edited - if ((await bot.cache.messages.get(message.id))?.content !== "Goodbye World!") { - throw new Error("The message should have been edited but it was not."); - } -} diff --git a/tests/mod.ts b/tests/mod.ts index 6604c5cf1..ed10d2c9f 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -2,7 +2,6 @@ import { TOKEN } from "../configs.ts"; import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; import { assertEquals, assertExists } from "./deps.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; -import { editMessageTest } from "./helpers/messages/editMessage.ts"; import { delayUntil } from "./utils.ts"; import { sendMessageWithComponents, @@ -119,13 +118,6 @@ Deno.test("[Bot] - Starting Tests", async (t) => { }, ...sanitizeMode, }), - t.step({ - name: "[message] edit message", - fn: async (t) => { - await editMessageTest(bot, channel.id, t); - }, - ...sanitizeMode, - }) ]); }); }); From 4720fd0c77dd7921a43d052b0ab293dc27e94376 Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 11:35:34 +0200 Subject: [PATCH 05/20] [Unit Test] messages: deleteMessages --- tests/helpers/messages/deleteMessages.ts | 36 ++++++++++++++++++++++++ tests/mod.ts | 15 ++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/helpers/messages/deleteMessages.ts diff --git a/tests/helpers/messages/deleteMessages.ts b/tests/helpers/messages/deleteMessages.ts new file mode 100644 index 000000000..cc026eac0 --- /dev/null +++ b/tests/helpers/messages/deleteMessages.ts @@ -0,0 +1,36 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +async function ifItFailsBlameWolf(bot: Bot, channelId: bigint, reason?: string) { + const message = await bot.helpers.sendMessage(channelId, "Hello World!"); + const secondMessage = await bot.helpers.sendMessage(channelId, "Hello World 2!"); + + // Assertions + assertExists(message); + assertExists(secondMessage); + // Delay the execution by to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => bot.cache.messages.has(message.id) && bot.cache.messages.has(secondMessage.id)); + // Make sure the message was created. + if (!bot.cache.messages.has(message.id) || !bot.cache.messages.has(secondMessage.id)) { + throw new Error(`The message seemed to be sent but it was not cached. Reason: ${reason}`); + } + + // Delete the message now + await bot.helpers.deleteMessages(channelId, [message.id, secondMessage.id], reason); + + // Wait to give it time for MESSAGE_DELETE event + await delayUntil(10000, () => !bot.cache.messages.has(message.id) && !bot.cache.messages.has(secondMessage.id)); + // Make sure it is gone from cache + if (bot.cache.messages.has(message.id) || bot.cache.messages.has(secondMessage.id)) { + throw new Error("The messages should have been deleted but they are still in cache."); + } +} + +export async function deleteMessagesWithoutReasonTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId); +} + +export async function deleteMessagesWithReasonTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, "with a reason"); +} diff --git a/tests/mod.ts b/tests/mod.ts index 3dc66bdd6..b6b47442c 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -2,6 +2,7 @@ import { TOKEN } from "../configs.ts"; import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; import { assertEquals, assertExists } from "./deps.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; +import { deleteMessagesWithoutReasonTest, deleteMessagesWithReasonTest } from "./helpers/messages/deleteMessages.ts"; import { delayUntil } from "./utils.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST @@ -104,6 +105,20 @@ Deno.test("[Bot] - Starting Tests", async (t) => { }, ...sanitizeMode, }), + t.step({ + name: "[message] delete messages without a reason", + fn: async (t) => { + await deleteMessagesWithoutReasonTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[message] delete messages with a reason", + fn: async (t) => { + await deleteMessagesWithReasonTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), ]); }); }); From 8c853766b323bfc0e29a05110639b3c7fc87907a Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 13:51:40 +0200 Subject: [PATCH 06/20] [Unit Test] messages: getMessage --- tests/helpers/messages/getMessage.ts | 22 ++++++++++++++++++++++ tests/mod.ts | 8 ++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/helpers/messages/getMessage.ts diff --git a/tests/helpers/messages/getMessage.ts b/tests/helpers/messages/getMessage.ts new file mode 100644 index 000000000..8b4e8fff0 --- /dev/null +++ b/tests/helpers/messages/getMessage.ts @@ -0,0 +1,22 @@ +import { Bot } from "../../../src/bot.ts"; +import {assertEquals, assertExists} from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function getMessageTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + const message = await bot.helpers.sendMessage(channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => bot.cache.messages.has(message.id)); + // Make sure the message was created. + if (!bot.cache.messages.has(message.id)) { + throw new Error("The message seemed to be sent but it was not cached. Reason: ${reason}"); + } + + // Fetch the message + const fetchedMessage = await bot.helpers.getMessage(channelId, message.id); + // Check if getMessage has worked + assertEquals(fetchedMessage.id, message.id); + assertEquals(fetchedMessage.content, message.content); +} diff --git a/tests/mod.ts b/tests/mod.ts index 3dc66bdd6..36a48fe00 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -6,6 +6,7 @@ import { delayUntil } from "./utils.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST import "./local.ts"; +import {getMessageTest} from "./helpers/messages/getMessage.ts"; Deno.test("[Bot] - Starting Tests", async (t) => { // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS @@ -104,6 +105,13 @@ Deno.test("[Bot] - Starting Tests", async (t) => { }, ...sanitizeMode, }), + t.step({ + name: "[message] fetch a message", + fn: async (t) => { + await getMessageTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), ]); }); }); From 73fea75ff7bea35ad441479e530499ff429a488f Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 11:52:20 +0000 Subject: [PATCH 07/20] change: prettier code --- tests/helpers/messages/getMessage.ts | 2 +- tests/mod.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/helpers/messages/getMessage.ts b/tests/helpers/messages/getMessage.ts index 8b4e8fff0..58c037322 100644 --- a/tests/helpers/messages/getMessage.ts +++ b/tests/helpers/messages/getMessage.ts @@ -1,5 +1,5 @@ import { Bot } from "../../../src/bot.ts"; -import {assertEquals, assertExists} from "../../deps.ts"; +import { assertEquals, assertExists } from "../../deps.ts"; import { delayUntil } from "../../utils.ts"; export async function getMessageTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { diff --git a/tests/mod.ts b/tests/mod.ts index 36a48fe00..5afc7cacc 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -6,7 +6,7 @@ import { delayUntil } from "./utils.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST import "./local.ts"; -import {getMessageTest} from "./helpers/messages/getMessage.ts"; +import { getMessageTest } from "./helpers/messages/getMessage.ts"; Deno.test("[Bot] - Starting Tests", async (t) => { // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS From 96fd804628f29d47f1ef5612bfd8a21539e8d322 Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 13:56:56 +0200 Subject: [PATCH 08/20] Fix comments --- tests/helpers/messages/deleteMessages.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/helpers/messages/deleteMessages.ts b/tests/helpers/messages/deleteMessages.ts index cc026eac0..592417057 100644 --- a/tests/helpers/messages/deleteMessages.ts +++ b/tests/helpers/messages/deleteMessages.ts @@ -16,12 +16,12 @@ async function ifItFailsBlameWolf(bot: Bot, channelId: bigint, reason?: string) throw new Error(`The message seemed to be sent but it was not cached. Reason: ${reason}`); } - // Delete the message now + // Delete the messages now await bot.helpers.deleteMessages(channelId, [message.id, secondMessage.id], reason); // Wait to give it time for MESSAGE_DELETE event await delayUntil(10000, () => !bot.cache.messages.has(message.id) && !bot.cache.messages.has(secondMessage.id)); - // Make sure it is gone from cache + // Make sure they are gone from cache if (bot.cache.messages.has(message.id) || bot.cache.messages.has(secondMessage.id)) { throw new Error("The messages should have been deleted but they are still in cache."); } From 6a94fe8f71878c4a7a72e88ba618ad8c2cc07cca Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 14:04:45 +0200 Subject: [PATCH 09/20] [Unit Test] messages: getMessages --- tests/helpers/messages/getMessages.ts | 32 +++++++++++++++++++++++++++ tests/mod.ts | 8 +++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/helpers/messages/getMessages.ts diff --git a/tests/helpers/messages/getMessages.ts b/tests/helpers/messages/getMessages.ts new file mode 100644 index 000000000..da914fd8d --- /dev/null +++ b/tests/helpers/messages/getMessages.ts @@ -0,0 +1,32 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertEquals, assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function getMessagesTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + const message = await bot.helpers.sendMessage(channelId, "Hello World!"); + const secondMessage = await bot.helpers.sendMessage(channelId, "Hello World 2!"); + const thirdMessage = await bot.helpers.sendMessage(channelId, "Hello World 3!"); + + // Assertions + assertExists(message); + assertExists(secondMessage); + assertExists(thirdMessage); + // Delay the execution by to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => bot.cache.messages.has(message.id) && bot.cache.messages.has(secondMessage.id) && bot.cache.messages.has(thirdMessage.id)); + // Make sure the messages was created. + if ( + !bot.cache.messages.has(message.id) || + !bot.cache.messages.has(secondMessage.id) || + !bot.cache.messages.has(thirdMessage.id) + ) { + throw new Error("The message seemed to be sent but it was not cached."); + } + + // Fetch the messages + const fetchedMessages = await bot.helpers.getMessages(channelId, { + after: message.id, + limit: 2, + }); + // Check if getMessage has worked + assertEquals(fetchedMessages?.length, 2); +} diff --git a/tests/mod.ts b/tests/mod.ts index 3dc66bdd6..9e6c8c6bc 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -2,6 +2,7 @@ import { TOKEN } from "../configs.ts"; import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; import { assertEquals, assertExists } from "./deps.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; +import { getMessagesTest } from "./helpers/messages/getMessages.ts"; import { delayUntil } from "./utils.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST @@ -104,6 +105,13 @@ Deno.test("[Bot] - Starting Tests", async (t) => { }, ...sanitizeMode, }), + t.step({ + name: "[message] fetch messages", + fn: async (t) => { + await getMessagesTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), ]); }); }); From b20c38a56ecd0dd05f7c5ba89eb5a020d32ca5ee Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 12:05:16 +0000 Subject: [PATCH 10/20] change: prettier code --- tests/helpers/messages/getMessages.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/helpers/messages/getMessages.ts b/tests/helpers/messages/getMessages.ts index da914fd8d..187a044a3 100644 --- a/tests/helpers/messages/getMessages.ts +++ b/tests/helpers/messages/getMessages.ts @@ -12,12 +12,18 @@ export async function getMessagesTest(bot: Bot, channelId: bigint, t: Deno.TestC assertExists(secondMessage); assertExists(thirdMessage); // Delay the execution by to allow MESSAGE_CREATE event to be processed - await delayUntil(10000, () => bot.cache.messages.has(message.id) && bot.cache.messages.has(secondMessage.id) && bot.cache.messages.has(thirdMessage.id)); + await delayUntil( + 10000, + () => + bot.cache.messages.has(message.id) && + bot.cache.messages.has(secondMessage.id) && + bot.cache.messages.has(thirdMessage.id) + ); // Make sure the messages was created. if ( - !bot.cache.messages.has(message.id) || - !bot.cache.messages.has(secondMessage.id) || - !bot.cache.messages.has(thirdMessage.id) + !bot.cache.messages.has(message.id) || + !bot.cache.messages.has(secondMessage.id) || + !bot.cache.messages.has(thirdMessage.id) ) { throw new Error("The message seemed to be sent but it was not cached."); } From 1367d34982832e5d9e507353e5f8a96017871f72 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 14:50:27 +0200 Subject: [PATCH 11/20] Add missing interaction data --- src/transformers/interaction.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/transformers/interaction.ts b/src/transformers/interaction.ts index e7dc45590..260fe065d 100644 --- a/src/transformers/interaction.ts +++ b/src/transformers/interaction.ts @@ -22,6 +22,8 @@ export function transformInteraction(bot: Bot, payload: SnakeCasedPropertiesDeep message: payload.message ? bot.transformers.message(bot, payload.message) : undefined, channelId: payload.channel_id ? bot.transformers.snowflake(payload.channel_id) : undefined, member: payload.member && guildId ? bot.transformers.member(bot, payload.member, guildId, user.id) : undefined, + // TODO: CamelCase INTERACTION DATA + data: payload.data }; } From 1a3e821f7ab8151f0dbb77feb39453a1e7b1fff8 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 12:50:56 +0000 Subject: [PATCH 12/20] change: prettier code --- src/transformers/interaction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/interaction.ts b/src/transformers/interaction.ts index 260fe065d..a104aad1c 100644 --- a/src/transformers/interaction.ts +++ b/src/transformers/interaction.ts @@ -23,7 +23,7 @@ export function transformInteraction(bot: Bot, payload: SnakeCasedPropertiesDeep channelId: payload.channel_id ? bot.transformers.snowflake(payload.channel_id) : undefined, member: payload.member && guildId ? bot.transformers.member(bot, payload.member, guildId, user.id) : undefined, // TODO: CamelCase INTERACTION DATA - data: payload.data + data: payload.data, }; } From 844019c4a4a9dce44aaf5ad36fb075262ff1005b Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 14:58:14 +0200 Subject: [PATCH 13/20] Fix: sendInteractionResponse --- .../interactions/send_interaction_response.ts | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/helpers/interactions/send_interaction_response.ts b/src/helpers/interactions/send_interaction_response.ts index 65cdf4d7a..6568b4009 100644 --- a/src/helpers/interactions/send_interaction_response.ts +++ b/src/helpers/interactions/send_interaction_response.ts @@ -39,13 +39,63 @@ export async function sendInteractionResponse( return await bot.rest.runMethod(bot.rest, "post", bot.constants.endpoints.WEBHOOK(bot.applicationId, token), { content: options.data.content, tts: options.data.tts, - embeds: options.data.embeds, - allowed_mentions: { - parse: allowedMentions.parse, - roles: allowedMentions.roles, - users: allowedMentions.users, - replied_user: allowedMentions.repliedUser, - }, + embeds: options.data.embeds?.map((embed) => ({ + title: embed.title, + type: embed.type, + description: embed.description, + url: embed.url, + timestamp: embed.timestamp, + color: embed.color, + footer: embed.footer + ? { + text: embed.footer.text, + icon_url: embed.footer.iconUrl, + proxy_icon_url: embed.footer.proxyIconUrl, + } + : undefined, + image: embed.image + ? { + url: embed.image.url, + proxy_url: embed.image.proxyUrl, + height: embed.image.height, + width: embed.image.width, + } + : undefined, + thumbnail: embed.thumbnail + ? { + url: embed.thumbnail.url, + proxy_url: embed.thumbnail.proxyUrl, + height: embed.thumbnail.height, + width: embed.thumbnail.width, + } + : undefined, + video: embed.video + ? { + url: embed.video.url, + proxy_url: embed.video.proxyUrl, + height: embed.video.height, + width: embed.video.width, + } + : undefined, + provider: embed.provider, + author: embed.author + ? { + name: embed.author.name, + url: embed.author.url, + icon_url: embed.author.iconUrl, + proxy_icon_url: embed.author.proxyIconUrl, + } + : undefined, + fields: embed.fields, + })), + allowed_mentions: allowedMentions + ? { + parse: allowedMentions?.parse, + roles: allowedMentions?.roles, + users: allowedMentions?.users, + replied_user: allowedMentions?.repliedUser, + } + : undefined, file: options.data.file, // TODO: Snakelize components?? components: options.data.components, From 4e709abed8b725b5457a36119737b9c485dc563c Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 15:32:59 +0200 Subject: [PATCH 14/20] Fix sendInteractionResponse again --- src/helpers/interactions/send_interaction_response.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/interactions/send_interaction_response.ts b/src/helpers/interactions/send_interaction_response.ts index 6568b4009..78b2d29be 100644 --- a/src/helpers/interactions/send_interaction_response.ts +++ b/src/helpers/interactions/send_interaction_response.ts @@ -32,7 +32,7 @@ export async function sendInteractionResponse( options.data = { ...options.data, allowedMentions: { parse: [] } }; } - const allowedMentions: AllowedMentions = options.data?.allowedMentions || { parse: [] }; + const allowedMentions: AllowedMentions = options.data?.allowedMentions; // If its already been executed, we need to send a followup response if (bot.cache.executedSlashCommands.has(token)) { From ac6b82810301e655491a2261773fa05137592bf8 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 15:36:30 +0200 Subject: [PATCH 15/20] Update send_interaction_response.ts --- .../interactions/send_interaction_response.ts | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/helpers/interactions/send_interaction_response.ts b/src/helpers/interactions/send_interaction_response.ts index 78b2d29be..98174c8dc 100644 --- a/src/helpers/interactions/send_interaction_response.ts +++ b/src/helpers/interactions/send_interaction_response.ts @@ -117,13 +117,63 @@ export async function sendInteractionResponse( { content: options.data.content, tts: options.data.tts, - embeds: options.data.embeds, - allowed_mentions: { - parse: allowedMentions.parse, - roles: allowedMentions.roles, - users: allowedMentions.users, - replied_user: allowedMentions.repliedUser, - }, + embeds: options.data.embeds?.map((embed) => ({ + title: embed.title, + type: embed.type, + description: embed.description, + url: embed.url, + timestamp: embed.timestamp, + color: embed.color, + footer: embed.footer + ? { + text: embed.footer.text, + icon_url: embed.footer.iconUrl, + proxy_icon_url: embed.footer.proxyIconUrl, + } + : undefined, + image: embed.image + ? { + url: embed.image.url, + proxy_url: embed.image.proxyUrl, + height: embed.image.height, + width: embed.image.width, + } + : undefined, + thumbnail: embed.thumbnail + ? { + url: embed.thumbnail.url, + proxy_url: embed.thumbnail.proxyUrl, + height: embed.thumbnail.height, + width: embed.thumbnail.width, + } + : undefined, + video: embed.video + ? { + url: embed.video.url, + proxy_url: embed.video.proxyUrl, + height: embed.video.height, + width: embed.video.width, + } + : undefined, + provider: embed.provider, + author: embed.author + ? { + name: embed.author.name, + url: embed.author.url, + icon_url: embed.author.iconUrl, + proxy_icon_url: embed.author.proxyIconUrl, + } + : undefined, + fields: embed.fields, + })), + allowed_mentions: allowedMentions + ? { + parse: allowedMentions?.parse, + roles: allowedMentions?.roles, + users: allowedMentions?.users, + replied_user: allowedMentions?.repliedUser, + } + : undefined, file: options.data.file, // TODO: Snakelize components?? components: options.data.components, From 971443cdf4bbd341823d05843d091937f172488e Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 13:37:05 +0000 Subject: [PATCH 16/20] change: prettier code --- src/helpers/interactions/send_interaction_response.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/interactions/send_interaction_response.ts b/src/helpers/interactions/send_interaction_response.ts index 98174c8dc..d11a80cbe 100644 --- a/src/helpers/interactions/send_interaction_response.ts +++ b/src/helpers/interactions/send_interaction_response.ts @@ -117,7 +117,7 @@ export async function sendInteractionResponse( { content: options.data.content, tts: options.data.tts, - embeds: options.data.embeds?.map((embed) => ({ + embeds: options.data.embeds?.map((embed) => ({ title: embed.title, type: embed.type, description: embed.description, From 12ea44e838ac632e30b5327dc8346f63b415d8f8 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 15:37:43 +0200 Subject: [PATCH 17/20] Update send_interaction_response.ts --- src/helpers/interactions/send_interaction_response.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/interactions/send_interaction_response.ts b/src/helpers/interactions/send_interaction_response.ts index d11a80cbe..357518b85 100644 --- a/src/helpers/interactions/send_interaction_response.ts +++ b/src/helpers/interactions/send_interaction_response.ts @@ -32,7 +32,7 @@ export async function sendInteractionResponse( options.data = { ...options.data, allowedMentions: { parse: [] } }; } - const allowedMentions: AllowedMentions = options.data?.allowedMentions; + const allowedMentions: AllowedMentions = options.data?.allowedMentions || { parse: [] }; // If its already been executed, we need to send a followup response if (bot.cache.executedSlashCommands.has(token)) { From a539673de1d6dd16286ba57ca5e0c6d8d48ca5b9 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sat, 30 Oct 2021 15:40:58 +0200 Subject: [PATCH 18/20] Update send_interaction_response.ts --- .../interactions/send_interaction_response.ts | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/helpers/interactions/send_interaction_response.ts b/src/helpers/interactions/send_interaction_response.ts index 357518b85..9a7e8e890 100644 --- a/src/helpers/interactions/send_interaction_response.ts +++ b/src/helpers/interactions/send_interaction_response.ts @@ -88,14 +88,12 @@ export async function sendInteractionResponse( : undefined, fields: embed.fields, })), - allowed_mentions: allowedMentions - ? { - parse: allowedMentions?.parse, - roles: allowedMentions?.roles, - users: allowedMentions?.users, - replied_user: allowedMentions?.repliedUser, - } - : undefined, + allowed_mentions: { + parse: allowedMentions?.parse, + roles: allowedMentions?.roles, + users: allowedMentions?.users, + replied_user: allowedMentions?.repliedUser, + }, file: options.data.file, // TODO: Snakelize components?? components: options.data.components, @@ -166,14 +164,12 @@ export async function sendInteractionResponse( : undefined, fields: embed.fields, })), - allowed_mentions: allowedMentions - ? { - parse: allowedMentions?.parse, - roles: allowedMentions?.roles, - users: allowedMentions?.users, - replied_user: allowedMentions?.repliedUser, - } - : undefined, + allowed_mentions: { + parse: allowedMentions?.parse, + roles: allowedMentions?.roles, + users: allowedMentions?.users, + replied_user: allowedMentions?.repliedUser, + }, file: options.data.file, // TODO: Snakelize components?? components: options.data.components, From a01f1c08cd2217d76f62c43b2ca3e2458b4d8ba6 Mon Sep 17 00:00:00 2001 From: Skillz4Killz Date: Sat, 30 Oct 2021 13:42:22 +0000 Subject: [PATCH 19/20] change: prettier code --- tests/mod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mod.ts b/tests/mod.ts index 6e2b2f1e1..51a3b73d7 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -131,7 +131,7 @@ Deno.test("[Bot] - Starting Tests", async (t) => { name: "[message] delete messages with a reason", fn: async (t) => { await deleteMessagesWithReasonTest(bot, channel.id, t); - }, + }, ...sanitizeMode, }), t.step({ From 1f95dc1ab02f14a25cc3e250b16d61e3d87429f9 Mon Sep 17 00:00:00 2001 From: Skillz4Killz Date: Sat, 30 Oct 2021 13:44:12 +0000 Subject: [PATCH 20/20] change: prettier code --- tests/mod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mod.ts b/tests/mod.ts index 338579b62..a6f4a3cf9 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -146,7 +146,7 @@ Deno.test("[Bot] - Starting Tests", async (t) => { name: "[message] fetch messages", fn: async (t) => { await getMessagesTest(bot, channel.id, t); - }, + }, ...sanitizeMode, }), ]);