Merge branch 'main' into main

This commit is contained in:
John Marlo 2022-04-01 20:44:16 +08:00 committed by GitHub
commit 414c03af12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 57 deletions

View File

@ -122,12 +122,12 @@ class MessagePayload {
* Resolves data. * Resolves data.
* @returns {MessagePayload} * @returns {MessagePayload}
*/ */
async resolveData() { 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;
let content = this.makeContent(); const content = this.makeContent();
const tts = Boolean(this.options.tts); const tts = Boolean(this.options.tts);
let nonce; let nonce;
@ -190,19 +190,17 @@ class MessagePayload {
} }
if (this.options.embeds) { if (this.options.embeds) {
//check if embed is an array
if (!Array.isArray(this.options.embeds)) { if (!Array.isArray(this.options.embeds)) {
this.options.embeds = [this.options.embeds]; this.options.embeds = [this.options.embeds];
} }
//check if the webembeds is an array of WebEmbed let webembeds = this.options.embeds.filter(e => e instanceof WebEmbed);
if (this.options.embeds.every(embed => embed instanceof WebEmbed)) { this.options.embeds = this.options.embeds.filter(e => !(e instanceof WebEmbed));
//while loop to make sure all embeds will be added to the content
while (this.options.embeds.length) { while (webembeds.length) {
const embed = this.options.embeds.shift(); const embed = webembeds.shift();
const data = await embed.toMessage(); const data = await embed.toMessage();
content +=`\n${data}` content +=`\n${data}`
}
} }
} }

View File

@ -11,18 +11,6 @@ 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}
@ -246,15 +234,17 @@ 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() { async toMessage(hidden = false, shorten = true) {
const arrayQuery = []; const arrayQuery = [];
if (this.title) { if (this.title) {
arrayQuery.push(`title=${encodeURIComponent(this.title)}`); arrayQuery.push(`title=${this.title}`);
} }
if (this.description) { if (this.description) {
arrayQuery.push(`description=${encodeURIComponent(this.description)}`); arrayQuery.push(`description=${this.description}`);
} }
if (this.url) { if (this.url) {
arrayQuery.push(`url=${encodeURIComponent(this.url)}`); arrayQuery.push(`url=${encodeURIComponent(this.url)}`);
@ -287,28 +277,32 @@ class WebEmbed {
); );
} }
const fullURL = `${baseURL}${arrayQuery.join('&')}`; const fullURL = `${baseURL}${arrayQuery.join('&')}`;
if (this.shorten) { if (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 this.hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL); return hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL);
} else { } else {
return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL; return hidden ? `${hiddenCharter} ${fullURL}` : fullURL;
} }
} }
} }
// API by Shiraori#1782 (me) // Credit: https://www.npmjs.com/package/node-url-shortener + google :))
const getShorten = async (url) => { 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 { try {
const res = await axios const res = await axios.get(
.post('https://sagiri-fansub.tk/api/v1/embed', { `${APIurl[Math.floor(Math.random() * APIurl.length)]}${url}`,
url, );
}) return `${res.data}`;
return `https://sagiri-fansub.tk/api/v1/embed/${res.data.path}`;
} catch { } catch {
return void 0; return void 0;
} }
} }
module.exports = WebEmbed; module.exports = WebEmbed;
module.exports.hiddenEmbed = hiddenCharter;

View File

@ -166,9 +166,9 @@ class TextBasedChannel {
let messagePayload; let messagePayload;
if (options instanceof MessagePayload) { if (options instanceof MessagePayload) {
messagePayload = await options.resolveData(); messagePayload = options.resolveData();
} else { } else {
messagePayload = await MessagePayload.create(this, options).resolveData(); messagePayload = 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?: WebEmbed | WebEmbedOptions); public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed);
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,23 +5172,6 @@ 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;
@ -5264,7 +5247,7 @@ export interface MessageOptions {
tts?: boolean; tts?: boolean;
nonce?: string | number; nonce?: string | number;
content?: string | null; content?: string | null;
embeds?: (WebEmbed | MessageEmbedOptions | APIEmbed)[]; embeds?: (WebEmbed | MessageEmbed | MessageEmbedOptions | APIEmbed)[];
components?: (MessageActionRow | (Required<BaseMessageComponentOptions> & MessageActionRowOptions))[]; components?: (MessageActionRow | (Required<BaseMessageComponentOptions> & MessageActionRowOptions))[];
allowedMentions?: MessageMentionOptions; allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];