'Delete messages that have any links
I'm working on a new project and I need to have a link filter, were pretty much anyone that doesn't have the permission ADMINISTRATOR
can't send links, if they do, the bot will automatically delete the message.
My code:
client.on('message', message =>{
if (message.content.includes(`http://`)) {
if (message.member.permissions.has('ADMINISTRATOR')){
return
}
message.delete()
.then(message.channel.send(`You are not allowed to send links ${message.author}`))
}
})
The code doesn't error, but whenever someone send's a link or just sends the text http://
the bot doesn't do anything.
Solution 1:[1]
Best solution will be this, if this fails then you are missing client intents
const { Client, Intents } = require(‘discord.js’)
// other constants
const client = new Client({
intents: [/*intent flags here*/]
// intent flags are enabled by putting Intents.FLAGS.IntentNameHere, Intents.FLAGS.NextIntentNameHere. Etc
// example: intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
// or use intents: 131071 for all intents
})
// other code
client.on(‘messageCreate’ async message => {
if (/(https?)/gi.test(message.content)) {
if (message.member.permissions.has('ADMINISTRATOR')) {
return
} else {
message.delete()
message.channel.send(`You are not allowed to send links ${message.author}`)
}
}
})
// rest of code
UPDATE intents: 32767 no longer includes all intents, all intents is now 131071
Solution 2:[2]
if (message.content.toLowerCase().includes("http://discord.gg")) { //upper or lower cases included
message.delete(); //delete the message
message.channel.send(`No links here, ${user}`) //send message after deletion
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | æ–°Acesyyy |