This commit is contained in:
Niko Storni 2018-12-19 11:33:39 -05:00
parent 6391620374
commit 006c2e4912

View file

@ -9,36 +9,29 @@ exports.lbrylink = async function(bot, msg, suffix) {
if (inPrivate(msg)) { if (inPrivate(msg)) {
return; return;
} }
if (msg.content.includes('lbry://')) {
try { const urlOccurrences = (msg.content.match(/lbry:\/\//g) || []).length;
// Extract the URL(s). if (urlOccurrences > 0) {
const urls = msg.content //convert all mentions to a plain string (because lbry://@Nikooo777 gets parsed as lbry://@<123123123> instead)
.replace(new RegExp('(lbry:\\/\\/)', 'g'), 'https://open.lbry.io/') let mentions = msg.mentions.users;
.match(/\bhttps?:\/\/\S+/gi) mentions.forEach(m => {
.filter(url => url !== 'https://open.lbry.io/'); msg.content = msg.content.replace(new RegExp(m.toString(), 'g'), `@${m.username}`);
const cleanURLs = []; });
for (const i in urls) {
// Check if Username Was Supplied //compile a list of URLs
const user = urls[i].match('<@.*>'); let urls = msg.content.match(/lbry:\/\/\S+/g);
if (user) {
const { username } = msg.mentions.users.get(user[0].slice(2, -1)); //clean URLS from any prepended or appended extra chars
urls[i] = urls[i].replace(user[0], `@${username}`); for (let i = 0; i < urls.length; i++) {
urls[i] = urls[i].replace(/^lbry:\/\//g, 'https://open.lbry.io/').replace(/\W+$/g, '');
} }
// Push clean URLs to the array.
cleanURLs.push(urls[i]);
}
if (cleanURLs.length < 1) return;
const linkEmbed = new RichEmbed(); const linkEmbed = new RichEmbed();
linkEmbed linkEmbed
.setAuthor('LBRY Linker') .setAuthor('LBRY Linker')
.setDescription("I see you tried to post a LBRY URL, here's a friendly hyperlink to share and for others to access your content with a single click:") .setDescription("I see you tried to post a LBRY URL, here's a friendly hyperlink to share and for others to access your content with a single click:")
.setColor(7976557); .setColor(7976557);
cleanURLs.forEach(url => linkEmbed.addField('Open with LBRY:', url, true)); urls.forEach(url => linkEmbed.addField('Open with LBRY:', url, true));
return msg.channel.send({ embed: linkEmbed }); return msg.channel.send({ embed: linkEmbed });
} catch (e) {
console.log(e);
msg.channel.send('Something went wrong when trying to run the lbrylinker, contact a moderator.');
}
} }
}); });
}; };