feat: Embed struct

This commit is contained in:
Yuzu 2022-07-01 16:04:58 -05:00
parent dba2de7305
commit 37f4071da6
3 changed files with 114 additions and 8 deletions

1
mod.ts
View File

@ -5,6 +5,7 @@ export * from "./structures/BaseGuild.ts";
export * from "./structures/Channel.ts"; export * from "./structures/Channel.ts";
export * from "./structures/Component.ts"; export * from "./structures/Component.ts";
export * from "./structures/DMChannel.ts"; export * from "./structures/DMChannel.ts";
export * from "./structures/Embed.ts";
export * from "./structures/Emoji.ts"; export * from "./structures/Emoji.ts";
export * from "./structures/Guild.ts"; export * from "./structures/Guild.ts";
export * from "./structures/GuildChannel.ts"; export * from "./structures/GuildChannel.ts";

102
structures/Embed.ts Normal file
View File

@ -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;

View File

@ -1,7 +1,7 @@
import type { Model } from "./Base.ts"; import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.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 type { GetReactions } from "../util/Routes.ts";
import { MessageFlags } from "../util/shared/flags.ts"; import { MessageFlags } from "../util/shared/flags.ts";
import User from "./User.ts"; import User from "./User.ts";
@ -34,6 +34,7 @@ export interface CreateMessage {
allowedMentions?: AllowedMentions; allowedMentions?: AllowedMentions;
files?: FileContent[]; files?: FileContent[];
messageReference?: CreateMessageReference; messageReference?: CreateMessageReference;
embeds?: DiscordEmbed[];
} }
/** /**
@ -110,20 +111,21 @@ export class Message implements Model {
} }
/** Edits the current message */ /** Edits the current message */
async edit({ content, allowedMentions, flags }: EditMessage): Promise<Message> { async edit(options: EditMessage): Promise<Message> {
const message = await this.session.rest.runMethod( const message = await this.session.rest.runMethod(
this.session.rest, this.session.rest,
"POST", "POST",
Routes.CHANNEL_MESSAGE(this.id, this.channelId), Routes.CHANNEL_MESSAGE(this.id, this.channelId),
{ {
content, content: options.content,
allowed_mentions: { allowed_mentions: {
parse: allowedMentions?.parse, parse: options.allowedMentions?.parse,
roles: allowedMentions?.roles, roles: options.allowedMentions?.roles,
users: allowedMentions?.users, users: options.allowedMentions?.users,
replied_user: allowedMentions?.repliedUser, 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, fail_if_not_exists: options.messageReference.failIfNotExists ?? true,
} }
: undefined, : undefined,
embeds: options.embeds,
}, },
); );