1

I am making a Discord bot that blocks swear words. It can be bypassed by using underscores and/or other symbols attached to the swear word.

How do I get the script to still block these words but not delete characters in general?

Here is my code:

import discord
import random
import string
from discord.ext import commands

client = commands.Bot(command_prefix = 'as-')

with open("badwords2.txt") as file:
    bad_words = file.read().splitlines()

def check_if_allowed(text):

    allowed_characters = string.ascii_uppercase + string.ascii_lowercase 
    
    if character not in allowed_characters: 
        return False

    return True
    
@client.event
async def on_message(message):    
    for bad_word in bad_words:
        if bad_word in message.content.lower().split(" "):
            t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:   Please do not swear. Swearing can result in a mute or ban.")
            t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
            await message.channel.send(embed=t)
            await message.delete()
            return

    if not check_if_allowed(message.content):
        t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:  Please do not swear. Swearing can result in a mute or ban.")
        t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
        await message.channel.send(embed=t)
        await message.delete()
        return


client.run('TOKEN')
Yves Gurcan
  • 1,096
  • 1
  • 10
  • 25

2 Answers2

0

If my message is "hey guys @#@@_badWord@@ what's up?" then, message.content.lower().split(" ") is ["hey", "guys", "@#@@_badWord@@", "what's", "up?"]

So you can be creative with this list that you are checking.

Maybe do

[re.sub(r'\W+', ' ', word) for word in content.message.content.lower().split(" ")]

which will create a new list (using list comprehension) that has each word with the special characters removed (using RegEx substitution)

See: How to remove special characters except space from a file in python?

I also recommend making this into a function so you don't have to write the code twice:

t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:   Please do not swear. Swearing can result in a mute or ban.")
t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
effprime
  • 578
  • 3
  • 10
0

You can try and remove all special character from the words with the re.sub() function from the re module:

    clearMessage = re.sub('[^A-Za-z0-9]+', '', message.content))

And remove the spaces:

    clearMessage = clearMessage.replace(" ","")

If you put it in your code it would look something like this:

import random
import string
import re
from discord.ext import commands

client = commands.Bot(command_prefix = 'as-')

with open("badwords2.txt") as file:
    bad_words = file.read().splitlines()

def check_if_allowed(text):

    allowed_characters = string.ascii_uppercase + string.ascii_lowercase #creates a string containing all upper- and lower- case letters.

    if character not in allowed_characters: #checks if the character is in the set of allowed characters, and returns false if not
        return False

    return True #returns true if every character in the input is allowed
    
@client.event
async def on_message(message):    
    clearMessage = re.sub('[^A-Za-z0-9]+', '', message.content))
    clearMessage = clearMessage.replace(" ","")

    for bad_word in bad_words:
        if bad_word in clearMessage:
            t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:   Please do not swear. Swearing can result in a mute or ban.")
            t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
            await message.channel.send(embed=t)
            await message.delete()
            return

    if not check_if_allowed(message.content):
        t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:  Please do not swear. Swearing can result in a mute or ban.")
        t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
        await message.channel.send(embed=t)
        await message.delete()
        return

You don't necessarily need to split your message into all words. The if something in something statement can also search raw strings

Timinator
  • 52
  • 6
  • it works, but if there is an uppercase letter in the message it does not block it. Is there something wrong with the code? I looked over it, but from what I saw there wasn't anything wrong with it. – TheSuperRobert Jan 17 '21 at 02:09
  • then please replace `clearMessage = re.sub('[^A-Za-z0-9]+', '', message.content))` with `clearMessage = re.sub('[^A-Za-z0-9]+', '', message.content)).lower()`. – Timinator Jan 17 '21 at 08:34