feat: update WebEmbed

This commit is contained in:
Elysia 2023-10-31 22:31:34 +07:00
parent ca9e50c49a
commit d9e1b82fd2
2 changed files with 45 additions and 39 deletions

View File

@ -319,50 +319,55 @@ class WebEmbed {
return this; return this;
} }
toString() {
const url = new URL(baseURL);
url.searchParams.set('image_type', this.imageType);
if (this.title) {
url.searchParams.set('title', this.title);
}
if (this.description) {
url.searchParams.set('description', this.description);
}
if (this.url) {
url.searchParams.set('url', this.url);
}
if (this.color) {
url.searchParams.set('color', `#${this.color.toString(16)}`);
}
if (this.image?.url) {
url.searchParams.set('image', this.image.url);
}
if (this.video?.url) {
url.searchParams.set('video', this.video.url);
}
if (this.author) {
if (this.author.name) {
url.searchParams.set('author_name', this.author.name);
}
if (this.author.url) {
url.searchParams.set('author_url', this.author.url);
}
}
if (this.provider) {
if (this.provider.name) {
url.searchParams.set('provider_name', this.provider.name);
}
if (this.provider.url) {
url.searchParams.set('provider_url', this.provider.url);
}
}
if (this.thumbnail?.url) {
url.searchParams.set('image', this.thumbnail.url);
}
return url.toString();
}
/** /**
* 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)
* @returns {string} Message Content * @returns {string} Message Content
*/ */
async toMessage() { async toMessage() {
const arrayQuery = [`image_type=${this.imageType}`]; const fullURL = this.toString();
if (this.title) {
arrayQuery.push(`title=${encodeURIComponent(this.title)}`);
}
if (this.description) {
arrayQuery.push(`description=${encodeURIComponent(this.description)}`);
}
if (this.url) {
arrayQuery.push(`url=${encodeURIComponent(this.url)}`);
}
if (this.color) {
arrayQuery.push(`color=${encodeURIComponent(`#${this.color.toString(16)}`)}`);
}
if (this.image?.url) {
arrayQuery.push(`image=${encodeURIComponent(this.image.url)}`);
}
if (this.video?.url) {
arrayQuery.push(`video=${encodeURIComponent(this.video.url)}`);
}
if (this.author) {
if (this.author.name) {
arrayQuery.push(`author_name=${encodeURIComponent(this.author.name)}`);
}
if (this.author.url) {
arrayQuery.push(`author_url=${encodeURIComponent(this.author.url)}`);
}
}
if (this.provider) {
if (this.provider.name) {
arrayQuery.push(`provider_name=${encodeURIComponent(this.provider.name)}`);
}
if (this.provider.url) {
arrayQuery.push(`provider_url=${encodeURIComponent(this.provider.url)}`);
}
}
if (this.thumbnail?.url) {
arrayQuery.push(`image=${encodeURIComponent(this.thumbnail.url)}`);
}
const fullURL = `${this.baseURL}${arrayQuery.join('&')}`;
if (this.shorten) { if (this.shorten) {
const url = await this.constructor.getShorten(fullURL); const url = await this.constructor.getShorten(fullURL);
if (!url) console.log('Cannot shorten URL in WebEmbed'); if (!url) console.log('Cannot shorten URL in WebEmbed');

1
typings/index.d.ts vendored
View File

@ -2337,6 +2337,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 toString(): string;
public toMessage(): Promise<string>; public toMessage(): Promise<string>;
} }