diff --git a/bot/modules/lbrylink.js b/bot/modules/lbrylink.js index 83d080b..ed3dff3 100644 --- a/bot/modules/lbrylink.js +++ b/bot/modules/lbrylink.js @@ -9,36 +9,29 @@ exports.lbrylink = async function(bot, msg, suffix) { if (inPrivate(msg)) { return; } - if (msg.content.includes('lbry://')) { - try { - // Extract the URL(s). - const urls = msg.content - .replace(new RegExp('(lbry:\\/\\/)', 'g'), 'https://open.lbry.io/') - .match(/\bhttps?:\/\/\S+/gi) - .filter(url => url !== 'https://open.lbry.io/'); - const cleanURLs = []; - for (const i in urls) { - // Check if Username Was Supplied - const user = urls[i].match('<@.*>'); - if (user) { - const { username } = msg.mentions.users.get(user[0].slice(2, -1)); - urls[i] = urls[i].replace(user[0], `@${username}`); - } - // Push clean URLs to the array. - cleanURLs.push(urls[i]); - } - if (cleanURLs.length < 1) return; - const linkEmbed = new RichEmbed(); - linkEmbed - .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:") - .setColor(7976557); - cleanURLs.forEach(url => linkEmbed.addField('Open with LBRY:', url, true)); - 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.'); + + const urlOccurrences = (msg.content.match(/lbry:\/\//g) || []).length; + if (urlOccurrences > 0) { + //convert all mentions to a plain string (because lbry://@Nikooo777 gets parsed as lbry://@<123123123> instead) + let mentions = msg.mentions.users; + mentions.forEach(m => { + msg.content = msg.content.replace(new RegExp(m.toString(), 'g'), `@${m.username}`); + }); + + //compile a list of URLs + let urls = msg.content.match(/lbry:\/\/\S+/g); + + //clean URLS from any prepended or appended extra chars + for (let i = 0; i < urls.length; i++) { + urls[i] = urls[i].replace(/^lbry:\/\//g, 'https://open.lbry.io/').replace(/\W+$/g, ''); } + const linkEmbed = new RichEmbed(); + linkEmbed + .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:") + .setColor(7976557); + urls.forEach(url => linkEmbed.addField('Open with LBRY:', url, true)); + return msg.channel.send({ embed: linkEmbed }); } }); };