From 50516bf89df206af4776d59347a7de14cafa9a71 Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Fri, 1 Apr 2022 20:09:32 +0700 Subject: [PATCH] Update before publish to npm Dev version --- src/errors/Messages.js | 1 + src/structures/MessagePayload.js | 13 ++++++- src/structures/WebEmbed.js | 35 ++++++++++++------- src/structures/Webhook.js | 4 +-- src/structures/interfaces/TextBasedChannel.js | 4 +-- typings/index.d.ts | 22 ++++++++++-- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/errors/Messages.js b/src/errors/Messages.js index 331e265..43c91f0 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -200,6 +200,7 @@ const Messages = { APPLICATION_ID_INVALID: "The application isn't BOT", INVALID_NITRO: 'Invalid Nitro Code', MESSAGE_ID_NOT_FOUND: 'Message ID not found', + MESSAGE_EMBED_LINK_LENGTH: 'Message content with embed link length is too long', }; for (const [name, message] of Object.entries(Messages)) register(name, message); diff --git a/src/structures/MessagePayload.js b/src/structures/MessagePayload.js index b651c4b..a32dd77 100644 --- a/src/structures/MessagePayload.js +++ b/src/structures/MessagePayload.js @@ -127,7 +127,7 @@ class MessagePayload { const isInteraction = this.isInteraction; const isWebhook = this.isWebhook; - const content = this.makeContent(); + let content = this.makeContent(); const tts = Boolean(this.options.tts); let nonce; @@ -197,11 +197,22 @@ class MessagePayload { let webembeds = this.options.embeds.filter(e => e instanceof WebEmbed); this.options.embeds = this.options.embeds.filter(e => !(e instanceof WebEmbed)); + if (webembeds.length > 0) { + // add hidden embed link + content += `\n${WebEmbed.hiddenEmbed} \n`; + } while (webembeds.length) { const embed = webembeds.shift(); const data = await embed.toMessage(); content +=`\n${data}` } + // Check content + if (content.length > 2000) { + console.warn(`[WARN] Content is longer than 2000 characters.`); + } + if (content.length > 4000) { // Max length if user has nitro boost + throw new RangeError('MESSAGE_EMBED_LINK_LENGTH'); + } } this.data = { diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index e5e8296..5f079be 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -2,7 +2,7 @@ const axios = require('axios'); const baseURL = 'https://embed.benny.fun/?'; const hiddenCharter = - '||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||'; + '||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||||​||'; const { RangeError } = require('../errors'); const Util = require('../util/Util'); @@ -11,6 +11,17 @@ class WebEmbed { this._setup(data); } _setup(data) { + /** + * Shorten the link + * @type {?boolean} + */ + this.shorten = data.shorten ?? true; + + /** + * Hidden Embed link + * @type {?boolean} + */ + this.hidden = data.hidden ?? false; /** * The title of this embed * @type {?string} @@ -234,17 +245,17 @@ class WebEmbed { /** * 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 */ - async toMessage(hidden = false, shorten = true) { + async toMessage() { const arrayQuery = []; if (this.title) { - arrayQuery.push(`title=${this.title}`); + arrayQuery.push(`title=${encodeURIComponent(this.title)}`); } if (this.description) { - arrayQuery.push(`description=${this.description}`); + arrayQuery.push( + `description=${encodeURIComponent(this.description)}`, + ); } if (this.url) { arrayQuery.push(`url=${encodeURIComponent(this.url)}`); @@ -262,7 +273,7 @@ class WebEmbed { } if (this.author) { if (this.author.name) arrayQuery.push( - `author_name=${this.author.name}`, + `author_name=${encodeURIComponent(this.author.name)}`, ); if (this.author.url) arrayQuery.push( `author_url=${encodeURIComponent(this.author.url)}`, @@ -270,24 +281,24 @@ class WebEmbed { } if (this.provider) { if (this.provider.name) arrayQuery.push( - `provider_name=${this.provider.name}`, + `provider_name=${encodeURIComponent(this.provider.name)}`, ); if (this.provider.url) arrayQuery.push( `provider_url=${encodeURIComponent(this.provider.url)}`, ); } const fullURL = `${baseURL}${arrayQuery.join('&')}`; - if (shorten) { + if (this.shorten) { const url = await getShorten(fullURL); 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 { - return hidden ? `${hiddenCharter} ${fullURL}` : fullURL; + return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL; } } } - // Credit: https://www.npmjs.com/package/node-url-shortener + google :)) +// Credit: https://www.npmjs.com/package/node-url-shortener + google :)) const getShorten = async (url) => { const APIurl = [ 'https://is.gd/create.php?format=simple&url=', diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 022270a..6a75499 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -186,9 +186,9 @@ class Webhook { let messagePayload; if (options instanceof MessagePayload) { - messagePayload = options.resolveData(); + messagePayload = await options.resolveData(); } else { - messagePayload = MessagePayload.create(this, options).resolveData(); + messagePayload = await MessagePayload.create(this, options).resolveData(); } const { data, files } = await messagePayload.resolveFiles(); diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 04f748a..e160826 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -166,9 +166,9 @@ class TextBasedChannel { let messagePayload; if (options instanceof MessagePayload) { - messagePayload = options.resolveData(); + messagePayload = await options.resolveData(); } else { - messagePayload = MessagePayload.create(this, options).resolveData(); + messagePayload = await MessagePayload.create(this, options).resolveData(); } const { data, files } = await messagePayload.resolveFiles(); diff --git a/typings/index.d.ts b/typings/index.d.ts index a32d98f..394d27e 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1761,7 +1761,7 @@ export class MessageEmbed { } export class WebEmbed { - public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed); + public constructor(data?: WebEmbedOptions); public author: MessageEmbedAuthor | null; public color: number | null; public description: string | null; @@ -1770,6 +1770,8 @@ export class WebEmbed { public title: string | null; public url: string | null; public video: MessageEmbedVideo | null; + public hidden: Boolean; + public shorten: Boolean; public setAuthor(options: EmbedAuthorData | null): this; public setColor(color: ColorResolvable): this; public setDescription(description: string): this; @@ -1778,7 +1780,7 @@ export class WebEmbed { public setTitle(title: string): this; public setURL(url: string): this; public setProvider(options: MessageEmbedProvider | null): this; - public toMessage(hidden: boolean, shorten: boolean): Promise; + public toMessage(): Promise; } export class MessageFlags extends BitField { @@ -5152,6 +5154,22 @@ export interface MessageEditOptions { components?: (MessageActionRow | (Required & MessageActionRowOptions))[]; } +export interface WebEmbedOptions { + shorten?: boolean; + hidden?: boolean; + title?: string; + description?: string; + url?: string; + timestamp?: Date | number; + color?: ColorResolvable; + fields?: EmbedFieldData[]; + author?: Partial & { icon_url?: string; proxy_icon_url?: string }; + thumbnail?: Partial & { proxy_url?: string }; + image?: Partial & { proxy_url?: string }; + video?: Partial & { proxy_url?: string }; + footer?: Partial & { icon_url?: string; proxy_icon_url?: string }; +} + export interface MessageEmbedAuthor { name: string; url?: string;