From 37f4071da66fed2f4db313bf3a1204b8d70f372b Mon Sep 17 00:00:00 2001 From: Yuzu Date: Fri, 1 Jul 2022 16:04:58 -0500 Subject: [PATCH] feat: Embed struct --- mod.ts | 1 + structures/Embed.ts | 102 ++++++++++++++++++++++++++++++++++++++++++ structures/Message.ts | 19 ++++---- 3 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 structures/Embed.ts diff --git a/mod.ts b/mod.ts index 559e08d..dc393a4 100644 --- a/mod.ts +++ b/mod.ts @@ -5,6 +5,7 @@ export * from "./structures/BaseGuild.ts"; export * from "./structures/Channel.ts"; export * from "./structures/Component.ts"; export * from "./structures/DMChannel.ts"; +export * from "./structures/Embed.ts"; export * from "./structures/Emoji.ts"; export * from "./structures/Guild.ts"; export * from "./structures/GuildChannel.ts"; diff --git a/structures/Embed.ts b/structures/Embed.ts new file mode 100644 index 0000000..2d5e624 --- /dev/null +++ b/structures/Embed.ts @@ -0,0 +1,102 @@ +import type { DiscordEmbed, EmbedTypes } from "../vendor/external.ts"; + +export interface Embed { + title?: string; + timestamp?: string; + type?: EmbedTypes; + url?: string; + color?: number; + description?: string; + author?: { + name: string; + iconURL?: string; + proxyIconURL?: string; + url?: string; + }; + footer?: { + text: string; + iconURL?: string; + proxyIconURL?: string; + }; + fields?: Array<{ + name: string; + value: string; + inline?: boolean; + }>; + thumbnail?: { + url: string; + proxyURL?: string; + width?: number; + height?: number; + }; + video?: { + url?: string; + proxyURL?: string; + width?: number; + height?: number; + }; + image?: { + url: string; + proxyURL?: string; + width?: number; + height?: number; + }; + provider?: { + url?: string; + name?: string; + } + +} + +export function embed(data: Embed): DiscordEmbed { + return { + title: data.title, + timestamp: data.timestamp, + type: data.type, + url: data.url, + color: data.color, + description: data.description, + author: { + name: data.author?.name!, + url: data.author?.url, + icon_url: data.author?.iconURL, + proxy_icon_url: data.author?.proxyIconURL, + }, + footer: data.footer || { + text: data.footer!.text, + icon_url: data.footer!.iconURL, + proxy_icon_url: data.footer!.proxyIconURL, + }, + fields: data.fields?.map((f) => { + return { + name: f.name, + value: f.value, + inline: f.inline, + }; + }), + thumbnail: data.thumbnail || { + url: data.thumbnail!.url, + proxy_url: data.thumbnail!.proxyURL, + width: data.thumbnail!.width, + height: data.thumbnail!.height, + }, + video: { + url: data.video?.url, + proxy_url: data.video?.proxyURL, + width: data.video?.width, + height: data.video?.height, + }, + image: data.image || { + url: data.image!.url, + proxy_url: data.image!.proxyURL, + width: data.image!.width, + height: data.image!.height, + }, + provider: { + url: data.provider?.url, + name: data.provider?.name, + }, + }; +} + +export default Embed; diff --git a/structures/Message.ts b/structures/Message.ts index da8b573..483f661 100644 --- a/structures/Message.ts +++ b/structures/Message.ts @@ -1,7 +1,7 @@ import type { Model } from "./Base.ts"; import type { Snowflake } from "../util/Snowflake.ts"; import type { Session } from "../session/Session.ts"; -import type { AllowedMentionsTypes, DiscordMessage, DiscordUser, FileContent } from "../vendor/external.ts"; +import type { AllowedMentionsTypes, DiscordEmbed, DiscordMessage, DiscordUser, FileContent } from "../vendor/external.ts"; import type { GetReactions } from "../util/Routes.ts"; import { MessageFlags } from "../util/shared/flags.ts"; import User from "./User.ts"; @@ -34,6 +34,7 @@ export interface CreateMessage { allowedMentions?: AllowedMentions; files?: FileContent[]; messageReference?: CreateMessageReference; + embeds?: DiscordEmbed[]; } /** @@ -110,20 +111,21 @@ export class Message implements Model { } /** Edits the current message */ - async edit({ content, allowedMentions, flags }: EditMessage): Promise { + async edit(options: EditMessage): Promise { const message = await this.session.rest.runMethod( this.session.rest, "POST", Routes.CHANNEL_MESSAGE(this.id, this.channelId), { - content, + content: options.content, allowed_mentions: { - parse: allowedMentions?.parse, - roles: allowedMentions?.roles, - users: allowedMentions?.users, - replied_user: allowedMentions?.repliedUser, + parse: options.allowedMentions?.parse, + roles: options.allowedMentions?.roles, + users: options.allowedMentions?.users, + replied_user: options.allowedMentions?.repliedUser, }, - flags, + flags: options.flags, + embeds: options.embeds, }, ); @@ -176,6 +178,7 @@ export class Message implements Model { fail_if_not_exists: options.messageReference.failIfNotExists ?? true, } : undefined, + embeds: options.embeds, }, );