Merge pull request #13 from lazuee/main

Update embeds sending to channel (Before uploading to npm)
This commit is contained in:
March 7th 2022-04-01 17:34:12 +07:00 committed by GitHub
commit fd7910de95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 18 deletions

View File

@ -2,7 +2,7 @@
const { Buffer } = require('node:buffer'); const { Buffer } = require('node:buffer');
const BaseMessageComponent = require('./BaseMessageComponent'); const BaseMessageComponent = require('./BaseMessageComponent');
const MessageEmbed = require('./MessageEmbed'); const WebEmbed = require('./WebEmbed');
const { RangeError } = require('../errors'); const { RangeError } = require('../errors');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const MessageFlags = require('../util/MessageFlags'); const MessageFlags = require('../util/MessageFlags');
@ -121,12 +121,12 @@ class MessagePayload {
* Resolves data. * Resolves data.
* @returns {MessagePayload} * @returns {MessagePayload}
*/ */
resolveData() { async resolveData() {
if (this.data) return this; if (this.data) return this;
const isInteraction = this.isInteraction; const isInteraction = this.isInteraction;
const isWebhook = this.isWebhook; const isWebhook = this.isWebhook;
const content = this.makeContent(); let content = this.makeContent();
const tts = Boolean(this.options.tts); const tts = Boolean(this.options.tts);
let nonce; let nonce;
@ -188,11 +188,29 @@ class MessagePayload {
this.options.attachments = attachments; this.options.attachments = attachments;
} }
if (this.options.embeds) {
//check if embeds is an array
if (!Array.isArray(this.options.embeds)) {
this.options.embeds = [this.options.embeds];
}
//check if the embeds is an array of WebEmbed
if (!this.options.embeds.every(e => e instanceof WebEmbed)) {
throw new TypeError('WEB_EMBEDS_TYPE');
}
//while loop to make sure all embeds will be added to the content
while (this.options.embeds.length) {
const embed = this.options.embeds.shift();
const data = await embed.toMessage();
content +=`\n${data}`
}
}
this.data = { this.data = {
content, content,
tts, tts,
nonce, nonce,
embeds: this.options.embeds?.map(embed => new MessageEmbed(embed).toJSON()),
components, components,
username, username,
avatar_url: avatarURL, avatar_url: avatarURL,

View File

@ -11,6 +11,18 @@ class WebEmbed {
this._setup(data); this._setup(data);
} }
_setup(data) { _setup(data) {
/**
* Shorten the link
* @type {?boolean}
*/
this.shorten = data.shorten ?? false;
/**
* Hidden Embed link
* @type {?boolean}
*/
this.hidden = data.hidden ?? false;
/** /**
* The title of this embed * The title of this embed
* @type {?string} * @type {?string}
@ -234,17 +246,15 @@ class WebEmbed {
/** /**
* Return Message Content + Embed (if hidden, pls check content length because it has 1000+ length) * Return Message Content + Embed (if hidden, pls check content length because it has 1000+ length)
* @param {boolean} hidden Hidden Embed link
* @param {boolean} shorten Shorten link ?
* @returns {string} Message Content * @returns {string} Message Content
*/ */
async toMessage(hidden = false, shorten = true) { async toMessage() {
const arrayQuery = []; const arrayQuery = [];
if (this.title) { if (this.title) {
arrayQuery.push(`title=${this.title}`); arrayQuery.push(`title=${encodeURIComponent(this.title)}`);
} }
if (this.description) { if (this.description) {
arrayQuery.push(`description=${this.description}`); arrayQuery.push(`description=${encodeURIComponent(this.description)}`);
} }
if (this.url) { if (this.url) {
arrayQuery.push(`url=${encodeURIComponent(this.url)}`); arrayQuery.push(`url=${encodeURIComponent(this.url)}`);
@ -277,12 +287,12 @@ class WebEmbed {
); );
} }
const fullURL = `${baseURL}${arrayQuery.join('&')}`; const fullURL = `${baseURL}${arrayQuery.join('&')}`;
if (shorten) { if (this.shorten) {
const url = await getShorten(fullURL); const url = await getShorten(fullURL);
if (!url) console.log('Cannot shorten URL in WebEmbed'); if (!url) console.log('Cannot shorten URL in WebEmbed');
return hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL); return this.hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL);
} else { } else {
return hidden ? `${hiddenCharter} ${fullURL}` : fullURL; return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL;
} }
} }
} }

View File

@ -58,7 +58,7 @@ class TextBasedChannel {
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud * @property {boolean} [tts=false] Whether or not the message should be spoken aloud
* @property {string} [nonce=''] The nonce for the message * @property {string} [nonce=''] The nonce for the message
* @property {string} [content=''] The content for the message * @property {string} [content=''] The content for the message
* @property {MessageEmbed[]|APIEmbed[]} [embeds] The embeds for the message * @property {WebEmbed[]} [embeds] The embeds for the message
* (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details) * (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details)
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content * @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details) * (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details)
@ -166,9 +166,9 @@ class TextBasedChannel {
let messagePayload; let messagePayload;
if (options instanceof MessagePayload) { if (options instanceof MessagePayload) {
messagePayload = options.resolveData(); messagePayload = await options.resolveData();
} else { } else {
messagePayload = MessagePayload.create(this, options).resolveData(); messagePayload = await MessagePayload.create(this, options).resolveData();
} }
const { data, files } = await messagePayload.resolveFiles(); const { data, files } = await messagePayload.resolveFiles();

21
typings/index.d.ts vendored
View File

@ -1761,7 +1761,7 @@ export class MessageEmbed {
} }
export class WebEmbed { export class WebEmbed {
public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed); public constructor(data?: WebEmbed | WebEmbedOptions);
public author: MessageEmbedAuthor | null; public author: MessageEmbedAuthor | null;
public color: number | null; public color: number | null;
public description: string | null; public description: string | null;
@ -5172,6 +5172,23 @@ export interface MessageEmbedImage {
width?: number; width?: number;
} }
export interface WebEmbedOptions {
shorten?: boolean;
hidden?: boolean;
title?: string;
description?: string;
url?: string;
timestamp?: Date | number;
color?: ColorResolvable;
fields?: EmbedFieldData[];
author?: Partial<MessageEmbedAuthor> & { icon_url?: string; proxy_icon_url?: string };
thumbnail?: Partial<MessageEmbedThumbnail> & { proxy_url?: string };
image?: Partial<MessageEmbedImage> & { proxy_url?: string };
video?: Partial<MessageEmbedVideo> & { proxy_url?: string };
footer?: Partial<MessageEmbedFooter> & { icon_url?: string; proxy_icon_url?: string };
}
export interface MessageEmbedOptions { export interface MessageEmbedOptions {
title?: string; title?: string;
description?: string; description?: string;
@ -5247,7 +5264,7 @@ export interface MessageOptions {
tts?: boolean; tts?: boolean;
nonce?: string | number; nonce?: string | number;
content?: string | null; content?: string | null;
embeds?: (MessageEmbed | MessageEmbedOptions | APIEmbed)[]; embeds?: (WebEmbed)[];
components?: (MessageActionRow | (Required<BaseMessageComponentOptions> & MessageActionRowOptions))[]; components?: (MessageActionRow | (Required<BaseMessageComponentOptions> & MessageActionRowOptions))[];
allowedMentions?: MessageMentionOptions; allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];