Update before publish to npm
Dev version
This commit is contained in:
parent
d233d0e83e
commit
50516bf89d
@ -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);
|
||||
|
@ -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 = {
|
||||
|
@ -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=',
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
22
typings/index.d.ts
vendored
22
typings/index.d.ts
vendored
@ -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<string>;
|
||||
public toMessage(): Promise<string>;
|
||||
}
|
||||
|
||||
export class MessageFlags extends BitField<MessageFlagsString> {
|
||||
@ -5152,6 +5154,22 @@ export interface MessageEditOptions {
|
||||
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 {
|
||||
name: string;
|
||||
url?: string;
|
||||
|
Loading…
Reference in New Issue
Block a user