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)) {
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 });
}
});
};