/** * This function will fetch a url to follow all redirects and compare the base url * with the RegEx to validate false and true positives. * * @param message The message that originally triggered the provided RegEx * @param banRegEx RegEx to check against * @returns @type boolean */ const linkChecker = async (message: string, banRegEx: RegExp) => { const detectedLinks = message.split(" ").map(subStr => { if (banRegEx.test(subStr)) { return subStr; } }); if (!detectedLinks || detectedLinks.length === 0) { return false; } const resolvedURLs = detectedLinks.map(async string => { if (string === undefined) { return false; } const url = string.startsWith("http://") || string.startsWith("https://") ? string : `https://${string}`; const res = await fetch(url, { headers: { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0" } }); const resolvedURL = new URL(res.url); const baseURL = resolvedURL.hostname.split(".").slice(-2).join("."); if (/^x\.com/i.test(baseURL)) { return true; } }); return (await Promise.all(resolvedURLs)).includes(true); }; export default linkChecker;