diff --git a/mod.ts b/mod.ts index f7d725a..d5cbe11 100644 --- a/mod.ts +++ b/mod.ts @@ -33,6 +33,8 @@ export * from "./structures/guilds/BaseGuild.ts"; export * from "./structures/guilds/Guild.ts"; export * from "./structures/guilds/InviteGuild.ts"; +export * from "./structures/builders/EmbedBuilder.ts"; + export * from "./structures/interactions/Interaction.ts"; export * from "./session/Session.ts"; diff --git a/structures/Message.ts b/structures/Message.ts index 3d734d8..f620ccd 100644 --- a/structures/Message.ts +++ b/structures/Message.ts @@ -14,7 +14,7 @@ import { MessageFlags } from "../util/shared/flags.ts"; import User from "./User.ts"; import Member from "./Member.ts"; import Attachment from "./Attachment.ts"; -import BaseComponent from "./components/Component.ts"; +import ComponentFactory from "./components/ComponentFactory.ts"; import * as Routes from "../util/Routes.ts"; /** @@ -83,7 +83,7 @@ export class Message implements Model { this.member = new Member(session, { ...data.member, user: data.author }, data.guild_id); } - this.components = data.components?.map((component) => BaseComponent.from(session, component)); + this.components = data.components?.map((component) => ComponentFactory.from(session, component)); } readonly session: Session; diff --git a/structures/builders/EmbedBuilder.ts b/structures/builders/EmbedBuilder.ts new file mode 100644 index 0000000..2359c43 --- /dev/null +++ b/structures/builders/EmbedBuilder.ts @@ -0,0 +1,113 @@ +import type { + DiscordEmbedField, + DiscordEmbed, + DiscordEmbedProvider +} from '../../vendor/external.ts'; + +export interface EmbedFooter { + text: string; + iconUrl?: string; + proxyIconUrl?: string +} + +export interface EmbedAuthor { + name: string + text?: string; + url?: string; + iconUrl?: string; + proxyIconUrl?: string +} + +export interface EmbedVideo { + height?: number; + proxyUrl?: string; + url?: string; + width?: number; +} + +export class EmbedBuilder { + #data: DiscordEmbed + constructor(data: DiscordEmbed = {}) { + this.#data = data; + if (!this.#data.fields) this.#data.fields = [] + } + + setAuthor(author: EmbedAuthor) { + this.#data.author = { + name: author.name, + icon_url: author.iconUrl, + proxy_icon_url: author.proxyIconUrl, + url: author.url + }; + return this; + } + + setColor(color: number) { + this.#data.color = color; + return this; + } + + setDescription(description: string) { + this.#data.description = description; + return this; + } + + addField(field: DiscordEmbedField) { + this.#data.fields!.push(field); + return this; + } + + setFooter(footer: EmbedFooter) { + this.#data.footer = { + text: footer.text, + icon_url: footer.iconUrl, + proxy_icon_url: footer.proxyIconUrl, + }; + return this; + } + + setImage(image: string) { + this.#data.image = { url: image }; + return this; + } + + setProvider(provider: DiscordEmbedProvider) { + this.#data.provider = provider; + return this; + } + + setThumbnail(thumbnail: string) { + this.#data.thumbnail = { url: thumbnail }; + return this; + } + + setTimestamp(timestamp: string | Date) { + this.#data.timestamp = timestamp instanceof Date ? timestamp.toISOString() : timestamp; + return this; + } + + setTitle(title: string, url?: string) { + this.#data.title = title; + if (url) this.setUrl(url); + return this; + } + + setUrl(url: string) { + this.#data.url = url; + return this; + } + + setVideo(video: EmbedVideo) { + this.#data.video = { + height: video.height, + proxy_url: video.proxyUrl, + url: video.url, + width: video.width + }; + return this; + } + + toJSON(): DiscordEmbed { + return this.#data; + } +}