How can i prevent swearwords on the server on discord.py

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:

@client.event
async def on_message(message):   

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

    else:
        msg = message.content
        for x in msg:

            try:

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

                    await message.channel.send("Don't use this word")


                else:
                    pass

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

@client.event
async def on_member_join(member): 
    channel = discord.utils.get(member.guild.text_channels, name="welcome")
    await channel.send(f"{member.name},welcome :)")
    global swearword_count
    swearword_count[str(member.id)] = 0
    exception_handling[str(member.id)] = 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.

Answer

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:
    try:
        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.