feat: static getShorten (WebEmbed)

This commit is contained in:
March 7th 2022-11-12 17:56:42 +07:00
parent 41f414b13a
commit bd48db3f8c

View File

@ -371,31 +371,31 @@ class WebEmbed {
} }
const fullURL = `${this.baseURL}${arrayQuery.join('&')}`; const fullURL = `${this.baseURL}${arrayQuery.join('&')}`;
if (this.shorten) { if (this.shorten) {
const url = await getShorten(fullURL, this); const url = await this.constructor.getShorten(fullURL, this);
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 this.hidden ? `${hiddenCharter} ${url || fullURL}` : url || fullURL;
} else { } else {
return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL; return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL;
} }
} }
}
const getShorten = async (url, embed) => { static async getShorten(url, embed) {
const APIurl = ['https://tinyurl.com/api-create.php?url=']; const APIurl = ['https://tinyurl.com/api-create.php?url='];
const shorten = `${ const shorten = `${
embed.shortenAPI && typeof embed.shortenAPI == 'string' embed.shortenAPI && typeof embed.shortenAPI == 'string'
? embed.shortenAPI ? embed.shortenAPI
: APIurl[Math.floor(Math.random() * APIurl.length)] : APIurl[Math.floor(Math.random() * APIurl.length)]
}${encodeURIComponent(url)}`; }${encodeURIComponent(url)}`;
try { try {
const res = await axios.get(`${shorten}`); const res = await axios.get(`${shorten}`);
if (typeof res.data === 'string') return res.data; if (typeof res.data === 'string') return res.data;
else if (typeof res.data === 'object') return res.data.shorten; else if (typeof res.data === 'object') return res.data.shorten;
else throw new Error('Unknown error'); else throw new Error('Unknown error');
} catch { } catch {
return undefined; return undefined;
}
} }
}; }
module.exports = WebEmbed; module.exports = WebEmbed;
module.exports.hiddenEmbed = hiddenCharter; module.exports.hiddenEmbed = hiddenCharter;