From 5b0ebe175b10e7507912d531db401b11d5d1d105 Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Fri, 1 Apr 2022 18:22:36 +0700 Subject: [PATCH 1/2] Revert "Merge pull request #13 from lazuee/main" This reverts commit fd7910de95d6dbec53984bbedb2119bfdeb5bcc9, reversing changes made to 44db21c06f5053f2f2197d9fdb52b88496dadcbf. --- src/structures/MessagePayload.js | 26 +++-------------- src/structures/WebEmbed.js | 28 ++++++------------- src/structures/interfaces/TextBasedChannel.js | 6 ++-- typings/index.d.ts | 21 ++------------ 4 files changed, 18 insertions(+), 63 deletions(-) diff --git a/src/structures/MessagePayload.js b/src/structures/MessagePayload.js index 67a28db..339b8f9 100644 --- a/src/structures/MessagePayload.js +++ b/src/structures/MessagePayload.js @@ -2,7 +2,7 @@ const { Buffer } = require('node:buffer'); const BaseMessageComponent = require('./BaseMessageComponent'); -const WebEmbed = require('./WebEmbed'); +const MessageEmbed = require('./MessageEmbed'); const { RangeError } = require('../errors'); const DataResolver = require('../util/DataResolver'); const MessageFlags = require('../util/MessageFlags'); @@ -121,12 +121,12 @@ class MessagePayload { * Resolves data. * @returns {MessagePayload} */ - async resolveData() { + resolveData() { if (this.data) return this; const isInteraction = this.isInteraction; const isWebhook = this.isWebhook; - let content = this.makeContent(); + const content = this.makeContent(); const tts = Boolean(this.options.tts); let nonce; @@ -188,29 +188,11 @@ class MessagePayload { 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 = { content, tts, nonce, + embeds: this.options.embeds?.map(embed => new MessageEmbed(embed).toJSON()), components, username, avatar_url: avatarURL, diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index f71b1f9..1f71e3c 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -11,18 +11,6 @@ class WebEmbed { this._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 * @type {?string} @@ -246,15 +234,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() { + async toMessage(hidden = false, shorten = true) { const arrayQuery = []; if (this.title) { - arrayQuery.push(`title=${encodeURIComponent(this.title)}`); + arrayQuery.push(`title=${this.title}`); } if (this.description) { - arrayQuery.push(`description=${encodeURIComponent(this.description)}`); + arrayQuery.push(`description=${this.description}`); } if (this.url) { arrayQuery.push(`url=${encodeURIComponent(this.url)}`); @@ -287,12 +277,12 @@ class WebEmbed { ); } const fullURL = `${baseURL}${arrayQuery.join('&')}`; - if (this.shorten) { + if (shorten) { const url = await getShorten(fullURL); if (!url) console.log('Cannot shorten URL in WebEmbed'); - return this.hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL); + return hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL); } else { - return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL; + return hidden ? `${hiddenCharter} ${fullURL}` : fullURL; } } } @@ -311,4 +301,4 @@ const getShorten = async (url) => { } } -module.exports = WebEmbed; +module.exports = WebEmbed; \ No newline at end of file diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 747ef95..5d2daaa 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -58,7 +58,7 @@ class TextBasedChannel { * @property {boolean} [tts=false] Whether or not the message should be spoken aloud * @property {string} [nonce=''] The nonce for the message * @property {string} [content=''] The content for the message - * @property {WebEmbed[]} [embeds] The embeds for the message + * @property {MessageEmbed[]|APIEmbed[]} [embeds] The embeds for the message * (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 * (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details) @@ -166,9 +166,9 @@ class TextBasedChannel { let messagePayload; if (options instanceof MessagePayload) { - messagePayload = await options.resolveData(); + messagePayload = options.resolveData(); } else { - messagePayload = await MessagePayload.create(this, options).resolveData(); + messagePayload = MessagePayload.create(this, options).resolveData(); } const { data, files } = await messagePayload.resolveFiles(); diff --git a/typings/index.d.ts b/typings/index.d.ts index fee26d0..01ac852 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1761,7 +1761,7 @@ export class MessageEmbed { } export class WebEmbed { - public constructor(data?: WebEmbed | WebEmbedOptions); + public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed); public author: MessageEmbedAuthor | null; public color: number | null; public description: string | null; @@ -5172,23 +5172,6 @@ export interface MessageEmbedImage { width?: number; } - -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 MessageEmbedOptions { title?: string; description?: string; @@ -5264,7 +5247,7 @@ export interface MessageOptions { tts?: boolean; nonce?: string | number; content?: string | null; - embeds?: (WebEmbed)[]; + embeds?: (MessageEmbed | MessageEmbedOptions | APIEmbed)[]; components?: (MessageActionRow | (Required & MessageActionRowOptions))[]; allowedMentions?: MessageMentionOptions; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; From 95dd8ae90a43c9dd31349cdb55af4048955f048a Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Fri, 1 Apr 2022 18:26:43 +0700 Subject: [PATCH 2/2] WebEmbed shorten api --- src/structures/WebEmbed.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index 1f71e3c..e5e8296 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -287,18 +287,22 @@ class WebEmbed { } } -// API by Shiraori#1782 (me) + // Credit: https://www.npmjs.com/package/node-url-shortener + google :)) const getShorten = async (url) => { - // Please not using this API, it's hosting in Heroku, very slow + const APIurl = [ + 'https://is.gd/create.php?format=simple&url=', + 'https://tinyurl.com/api-create.php?url=', + // 'https://cdpt.in/shorten?url=', Redirects 5s :( + ]; try { - const res = await axios - .post('https://sagiri-fansub.tk/api/v1/embed', { - url, - }) - return `https://sagiri-fansub.tk/api/v1/embed/${res.data.path}`; + const res = await axios.get( + `${APIurl[Math.floor(Math.random() * APIurl.length)]}${url}`, + ); + return `${res.data}`; } catch { return void 0; } } -module.exports = WebEmbed; \ No newline at end of file +module.exports = WebEmbed; +module.exports.hiddenEmbed = hiddenCharter; \ No newline at end of file