Created
August 2, 2022 21:54
-
-
Save paradox-glitch/88dd84cf678cd4eee71676eb6f1947e3 to your computer and use it in GitHub Desktop.
Detect Links for Discord.js Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
var m_TLDs; | |
module.exports = { | |
CheckMessage: async function(a_MessageReceived) { | |
const l_MessageContent = a_MessageReceived.cleanContent.toLowerCase(); | |
const l_URLRegex = /([a-zA-Z\d]+:\/\/)?((\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/.*)?)/ig; | |
const l_DetectedURLs = l_MessageContent.match(l_URLRegex); | |
if (!l_DetectedURLs) | |
return false; | |
var l_Return = false; | |
await l_DetectedURLs.forEach(a_URL => { | |
const l_ThisTLD = a_URL.split(`.`).pop().toUpperCase(); | |
const l_JustTLD = l_ThisTLD.split(`/`).shift(); | |
if (m_TLDs.includes(l_JustTLD)) | |
l_Return = true; | |
}); | |
return l_Return; | |
}, | |
StartUp: async function() { | |
const l_Response = await fetch(`https://data.iana.org/TLD/tlds-alpha-by-domain.txt`); | |
const l_Body = await l_Response.text(); | |
const l_TLDs = l_Body.split(/\r?\n|\r|\n/g); | |
l_TLDs.shift(); | |
m_TLDs = l_TLDs; | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment