thaldrin/utils/shortlinks.js

55 lines
2.0 KiB
JavaScript

const ShortLinkReg = /(?:\s|^)(gh|gl|gd|owo|sg|tw|teknik|bb|yt|bc|bcu|sc|aur|bot|sw|tw|npm|xkcd)\/([a-zA-Z0-9-_.#/!]*)/g;
const ShortLinks = {
gh: "https://github.com/$link$",
gl: "https://gitlab.com/$link$",
yt: "https://youtu.be/$link$",
tw: "https://twitter.com/$link$",
npm: "https://npmjs.com/package/$link$",
tw: "https://twitch.tv/$link$",
gd: "https://gitdab.com/$link$",
owo: "https://owo.codes/$link$",
sg: "https://git.supernets.org/$link$",
teknik: "https://git.teknik.io/$link$",
bb: "https://bitbucket.org/$link$",
bc: "https://$link$.bandcamp.com/",
bcu: "https://bandcamp.com/$link$",
sc: "https://soundcloud.com/$link$",
aur: "https://aur.archlinux.org/packages/$link$",
sw: "https://steamcommunity.com/sharedfiles/filedetails/?id=$link$",
bot: "<https://discordapp.com/oauth2/authorize?client_id=$link$&scope=bot>",
xkcd: "https://xkcd.com/$link$"
};
module.exports = function Shortlinks(enabled, msg) {
if (enabled) {
let res = msg.content.match(ShortLinkReg);
if (!res) return;
res = res.map(x => (x.startsWith(" ") ? x.substring(1) : x));
let links = [];
for (const m in res) {
for (const x in ShortLinks) {
let url = res[m];
if (!url.startsWith(x)) continue;
url = url.replace(x + "/", "");
if (x == "gh" || x == "gl" || x == "gd") {
url = url.replace("#", "/issues/");
}
if (x == "gl") {
url = url.replace("!", "/merge_requests/");
} else if (x == "gd") {
url = url.replace("!", "/pulls/");
} else if (x == "gh") {
url = url.replace("!", "/pull/");
}
url = ShortLinks[x].replace("$link$", url);
links.push(url);
}
}
msg.channel.send(links.join("\n"));
};
}