Update before publish to npm

Dev version
This commit is contained in:
March 7th 2022-04-01 20:09:32 +07:00
parent d233d0e83e
commit 50516bf89d
6 changed files with 60 additions and 19 deletions

View File

@ -200,6 +200,7 @@ const Messages = {
APPLICATION_ID_INVALID: "The application isn't BOT", APPLICATION_ID_INVALID: "The application isn't BOT",
INVALID_NITRO: 'Invalid Nitro Code', INVALID_NITRO: 'Invalid Nitro Code',
MESSAGE_ID_NOT_FOUND: 'Message ID not found', 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); for (const [name, message] of Object.entries(Messages)) register(name, message);

View File

@ -127,7 +127,7 @@ class MessagePayload {
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;
@ -197,11 +197,22 @@ class MessagePayload {
let webembeds = this.options.embeds.filter(e => e instanceof WebEmbed); let webembeds = this.options.embeds.filter(e => e instanceof WebEmbed);
this.options.embeds = 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) { while (webembeds.length) {
const embed = webembeds.shift(); const embed = webembeds.shift();
const data = await embed.toMessage(); const data = await embed.toMessage();
content +=`\n${data}` 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 = { this.data = {

View File

@ -11,6 +11,17 @@ class WebEmbed {
this._setup(data); this._setup(data);
} }
_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 * The title of this embed
* @type {?string} * @type {?string}
@ -234,17 +245,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(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)}`);
@ -262,7 +273,7 @@ class WebEmbed {
} }
if (this.author) { if (this.author) {
if (this.author.name) arrayQuery.push( if (this.author.name) arrayQuery.push(
`author_name=${this.author.name}`, `author_name=${encodeURIComponent(this.author.name)}`,
); );
if (this.author.url) arrayQuery.push( if (this.author.url) arrayQuery.push(
`author_url=${encodeURIComponent(this.author.url)}`, `author_url=${encodeURIComponent(this.author.url)}`,
@ -270,24 +281,24 @@ class WebEmbed {
} }
if (this.provider) { if (this.provider) {
if (this.provider.name) arrayQuery.push( if (this.provider.name) arrayQuery.push(
`provider_name=${this.provider.name}`, `provider_name=${encodeURIComponent(this.provider.name)}`,
); );
if (this.provider.url) arrayQuery.push( if (this.provider.url) arrayQuery.push(
`provider_url=${encodeURIComponent(this.provider.url)}`, `provider_url=${encodeURIComponent(this.provider.url)}`,
); );
} }
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;
} }
} }
} }
// 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 getShorten = async (url) => {
const APIurl = [ const APIurl = [
'https://is.gd/create.php?format=simple&url=', 'https://is.gd/create.php?format=simple&url=',

View File

@ -186,9 +186,9 @@ class Webhook {
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();

View File

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

22
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?: 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;
@ -1770,6 +1770,8 @@ export class WebEmbed {
public title: string | null; public title: string | null;
public url: string | null; public url: string | null;
public video: MessageEmbedVideo | null; public video: MessageEmbedVideo | null;
public hidden: Boolean;
public shorten: Boolean;
public setAuthor(options: EmbedAuthorData | null): this; public setAuthor(options: EmbedAuthorData | null): this;
public setColor(color: ColorResolvable): this; public setColor(color: ColorResolvable): this;
public setDescription(description: string): this; public setDescription(description: string): this;
@ -1778,7 +1780,7 @@ export class WebEmbed {
public setTitle(title: string): this; public setTitle(title: string): this;
public setURL(url: string): this; public setURL(url: string): this;
public setProvider(options: MessageEmbedProvider | null): this; public setProvider(options: MessageEmbedProvider | null): this;
public toMessage(hidden: boolean, shorten: boolean): Promise<string>; public toMessage(): Promise<string>;
} }
export class MessageFlags extends BitField<MessageFlagsString> { export class MessageFlags extends BitField<MessageFlagsString> {
@ -5152,6 +5154,22 @@ export interface MessageEditOptions {
components?: (MessageActionRow | (Required<BaseMessageComponentOptions> & MessageActionRowOptions))[]; components?: (MessageActionRow | (Required<BaseMessageComponentOptions> & MessageActionRowOptions))[];
} }
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 MessageEmbedAuthor { export interface MessageEmbedAuthor {
name: string; name: string;
url?: string; url?: string;