I'm new to discord.js. I'm trying to check if a message contains a link like "Hi, I'm from discord.gg/xxxxx and now I'll spam my link".
How can I check if the message contain the link?
I'm new to discord.js. I'm trying to check if a message contains a link like "Hi, I'm from discord.gg/xxxxx and now I'll spam my link".
How can I check if the message contain the link?
6 Answers
Reset to default 8I am unsure if you want to check for discord invite links specifically, or if you want to check for all links. Either way, you can use message.content.includes
.
Example:
bot.on('message', (message) => { //whenever a message is sent
if (message.content.includes('discord.gg/'||'discordapp.com/invite/')) { //if it contains an invite link
message.delete() //delete the message
.then(message.channel.send('Link Deleted:\n**Invite links are not permitted on this server**'))
}
})
I find this is the best:
let regx = /^((?:https?:)?\/\/)?((?:www|m)\.)? ((?:discord\.gg|discordapp\.com))/g
let cdu = regx.test(message.content.toLowerCase().replace(/\s+/g, ''))
Tell me if it works!
What you are doing works but you don't need .then()
just leave the message.channel.send()
as it is.
You can check this using Regular Expressions (RegEX)
Example:
// The message to check for a Discord link
var message = "Hi, please join discord.gg/a2dsc for cool conversations";
// The message will be tested on "discord.gg/{any character or digit}"
var containsDiscordUrl = message.test(/discord.gg\/\w*\d*);
// If the test has found a URL..
if (containsDiscordUrl) { // ... Do something }
You can try this:
bot.on(`message`, async message => {
const bannedWords = [`discord.gg`, `.gg/`, `.gg /`, `. gg /`, `. gg/`, `discord .gg /`, `discord.gg /`, `discord .gg/`, `discord .gg`, `discord . gg`, `discord. gg`, `discord gg`, `discordgg`, `discord gg /`]
try {
if (bannedWords.some(word => message.content.toLowerCase().includes(word))) {
if (message.author.id === message.guild.ownerID) return;
await message.delete();
await message.channel.send(`You cannot send invites to other Discord servers`);
}
} catch (e) {
console.log(e);
}
});
(There was a missing ")")
let regexp = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
I think this is helping you.
Edit:
(?:(?:https?|ftp):\/\/)?
- http, https, or ftp
(?:(?! ... )(?! ... )(?! ... ))
- These are negative lookup statements, used to ensure that the IP address does not fall into specific reserved ranges.
(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])
| (?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}
| (?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))
- check IPv4
(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)
- check domain name
(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*
- check sub domain
(?:\.(?:[a-z\u00a1-\uffff]{2,}))
- check top-level domain
(?::\d{2,5})?
- cehck port number
(?:\/\S*)?$
- check optional path
Use this regex for discord invite check
\b(?:https?://)?(?:www\.)?(?:discord\.gg|discord\.com/invite)/([a-zA-Z0-9-]+)\b