From 109c2b8abe95b3244bcd832ae25406251df850bf Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Thu, 28 Oct 2021 17:22:00 +0000 Subject: [PATCH] fix embeds --- src/helpers/messages/edit_message.ts | 50 ++++++++++++++++++- src/helpers/messages/send_message.ts | 51 ++++++++++++++++++- tests/local.ts | 20 -------- tests/util/loop_object.ts | 54 -------------------- tests/util/utils.ts | 75 +--------------------------- 5 files changed, 101 insertions(+), 149 deletions(-) delete mode 100644 tests/local.ts delete mode 100644 tests/util/loop_object.ts diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index 5885a93a1..5e656c1eb 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -36,7 +36,55 @@ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), { content: content.content, - embeds: content.embeds, + embeds: content.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: { parse: content.allowedMentions?.parse, roles: content.allowedMentions?.roles, diff --git a/src/helpers/messages/send_message.ts b/src/helpers/messages/send_message.ts index 7f6313c5f..08b313ba3 100644 --- a/src/helpers/messages/send_message.ts +++ b/src/helpers/messages/send_message.ts @@ -7,6 +7,7 @@ import type { Message } from "../../types/messages/message.ts"; import type { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import type { Bot } from "../../bot.ts"; import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import { Embed } from "../../types/embeds/embed.ts"; /** Send a message to the channel. Requires SEND_MESSAGES permission. */ export async function sendMessage(bot: Bot, channelId: bigint, content: string | CreateMessage) { @@ -80,7 +81,55 @@ export async function sendMessage(bot: Bot, channelId: bigint, content: string | { content: content.content, tts: content.tts, - embeds: content.embeds, + embeds: content.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: { parse: content.allowedMentions?.parse, roles: content.allowedMentions?.roles, diff --git a/tests/local.ts b/tests/local.ts deleted file mode 100644 index 52cc85fec..000000000 --- a/tests/local.ts +++ /dev/null @@ -1,20 +0,0 @@ -// // THE ORDER OF THE IMPORTS IN THIS FILE MATTER! -// // DO NOT MOVE THEM UNLESS YOU KNOW WHAT YOUR DOING! - -// import "./util/utils.ts"; -// import "./util/validate_length.ts"; -// import "./util/loop_object.ts"; - -// // Final cleanup - -// import { delay } from "../src/util/utils.ts"; -// if (import.meta.main) { -// // clear all the sweeper intervals -// for (const c of Object.values(cache)) { -// if (!(c instanceof Map)) continue; - -// console.log("Cleaned"); -// } - -// await delay(3000); -// } diff --git a/tests/util/loop_object.ts b/tests/util/loop_object.ts deleted file mode 100644 index 7e9c5e71b..000000000 --- a/tests/util/loop_object.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { loopObject } from "../../src/util/loop_object.ts"; -import { assertEquals } from "../deps.ts"; - -const objWithBigints = { - a: BigInt(0), - b: "string", - c: 123, - d: [123], - e: { - f: 123, - g: BigInt(2), - }, - h: null, - i: undefined, - j: 0, - // k: function () { - // return true; - // }, -}; - -const correctlyConverted = { - a: "0", - b: "string", - c: 123, - d: [123], - e: { - f: 123, - g: "2", - }, - h: null, - i: undefined, - j: 0, - // k: function () { - // return true; - // }, -}; - -Deno.test({ - name: "[utils] loop over an object with a handler", - fn() { - const converted = loopObject( - objWithBigints, - (value) => - typeof value === "bigint" - ? value.toString() - : Array.isArray(value) - ? value.map((v) => (typeof v === "bigint" ? v.toString() : v)) - : value, - `Running for loop in unit test function for changing bigints to strings.` - ); - - assertEquals(converted, correctlyConverted); - }, -}); diff --git a/tests/util/utils.ts b/tests/util/utils.ts index b84cae0a4..7d65308c4 100644 --- a/tests/util/utils.ts +++ b/tests/util/utils.ts @@ -1,79 +1,8 @@ import { ApplicationCommandOption } from "../../src/types/interactions/commands/application_command_option.ts"; import { ApplicationCommandOptionChoice } from "../../src/types/interactions/commands/application_command_option_choice.ts"; import { DiscordApplicationCommandOptionTypes } from "../../src/types/interactions/commands/application_command_option_types.ts"; -import { camelize, snakelize, validateSlashCommands } from "../../src/util/utils.ts"; -import { assertEquals, assertThrows } from "../deps.ts"; - -const testSnakeObject = { - // deno-lint-ignore camelcase - hello_world: "hello_world", - // deno-lint-ignore camelcase - the_universe: { - blue_planet: { - water: "is_blue", - dirt: "isDirty", - }, - moon: { - earth_moon: { - is_round: true, - }, - other_moon: { - is_round: 0, - }, - }, - arrays: ["one_two", { moo_cow: { boo: true } }], - test_the_id: "123123123123", - }, -}; - -const testCamelObject = { - helloWorld: "hello_world", - theUniverse: { - bluePlanet: { - water: "is_blue", - dirt: "isDirty", - }, - moon: { - earthMoon: { - isRound: true, - }, - otherMoon: { - isRound: 0, - }, - }, - arrays: ["one_two", { mooCow: { boo: true } }], - testTheId: "123123123123", - }, -}; - -const someOther = { - helloWorld: 1, -}; - -const someElseOther = { - // deno-lint-ignore camelcase - hello_world: 1, -}; - -Deno.test({ - name: "[utils] convert snake case keys to camel case", - fn() { - const result = camelize(testSnakeObject); - assertEquals(result, testCamelObject); - const resultTwo = camelize(someOther); - assertEquals(resultTwo, someOther); - }, -}); - -Deno.test({ - name: "[utils] convert camel case keys to snake case", - fn() { - const result = snakelize(testCamelObject); - assertEquals(result, testSnakeObject); - const resultTwo = snakelize(someElseOther); - assertEquals(resultTwo, someElseOther); - }, -}); +import { validateSlashCommands } from "../../src/util/utils.ts"; +import { assertThrows } from "../deps.ts"; Deno.test({ name: "[utils] validateSlashCommands(): application command name",