Discord.js Advertising Filter

Welcome everyone to the third discord.js guide. In this post, I will be showing you how you can create an advertising filter for your bot.

This bot will delete any messages that contain https, http or discord.gg and will post a log message to your server logs

To begin you will need to add the following into your message event

        if (message.content.includes("https://" || "http://" || "discord.gg/")) {
            // If the website is one of my sites the bot will ignore it
            if (message.content.includes("bencey.co.nz")) return;
            // If the filter is not enabled the bot will not filter messages
            
            // If you are not using per guild settings uncomment the lines below
            
            // let filterEnabled = True
            // if (filterEnabled == "false") return;
            
            // If you are not using per guild settings comment out the line below
            if (message.settings.filterEnabled == "false") return;

            // If the user has manage message perms (staff member) They will not be affected by the advertisement filter
            if (message.member.hasPermission('MANAGE_MESSAGES')) return
            
            // Creates an embed to alert staff that someone attempted to advertise
            let embed = new Discord.RichEmbed()
                .setDescription("Attempted Advertisement")
                .setColor(`#f28621`) // Sets the color of the embed
                .setAuthor(message.author.tag, message.author.avatarURL) // Sets the author of the embed which is the user who advertised
                .addField("Message", message.content) // Adds what the advertisement message was
                .setTimestamp()
                .setFooter("Advertisement Filter")

            let channel = message.guild.channels.find(c => c.name === message.settings.modLogChannel)
            //if it cannot find a logs channel the bot will return
            if (!channel) {
                message.channel.bulkDelete(1)
                return message.channel.send(`${message.author.tag} Attempted to advertise`)
            }

            channel.send(embed)

            message.channel.bulkDelete(1)

        }

The first thing the bot checks for is if the message contains https, http or discord.gg (Discord invites) If the message does contain these then it will check to see if it is one of my domains, after that if the filter is enabled it will post a log message to the server log channel and delete the user’s message. It will also see if the user is a member of staff and if they are it will not remove their message

Hope this guide helps you to add a filter for your discord bot :)

Bencey