How can i prevent swearwords on the server on

I am trying to block messages which contain swearwords.Swearwords in a text file btw.So,I got stuck because whenever I write swearwords,bot doesn’t response my message.This is my code:

async def on_message(message):   

    global swearword_count
    global badwords
    if == client.user:  
        await client.process_commands(message)

        msg = message.content
        for x in msg:


                if x in badwords:
                    if exception_counter[str(] == 0:
                        await"Please do not use this word ever")
                        swearword_count[str(] += 1
                    if swearword_count[str(] > 5 and exception_counter == 1:
                        await"You've been banned due to bad words that you used.")
                    elif swearword_count[str(] > 5 and exception_counter == 0:
                        await"You've been banned due to bad words that you used.")

                    await"Don't use this word")


            except KeyError:
                exception_handling[str(] = 1
                swearword_count[str(] = 0
                await"If you use this word,you will get banned.")
        await client.process_commands(message)

async def on_member_join(member): 
    channel = discord.utils.get(member.guild.text_channels, name="welcome")
    await channel.send(f"{},welcome :)")
    global swearword_count
    swearword_count[str(] = 0
    exception_handling[str(] = 0
    dm_channell = await member.create_dm()
    await dm_channell.send(f"{member},welcome mate :)")

I’ve used dictionaries to store the data of users.So how can i fix this problem?If you have any advice,I’d love to see it

PS:My commands work properly.


It’s because for x in msg: splits your message into single letters. You have to slice it for example by spaces: for x in msg.split(" "):, but this way if someone writes “badword” without spaces (ex. “badwordIlikeapples) this won’t work. So try this:

for x in badwords:
        if x in msg:

This will catch all the “badwords”, but if you write a word that contains the swear word inside it this will also be caught.